Skip to content

Instantly share code, notes, and snippets.

@davidortinau
Created November 28, 2011 18:01
Show Gist options
  • Save davidortinau/1401319 to your computer and use it in GitHub Desktop.
Save davidortinau/1401319 to your computer and use it in GitHub Desktop.
Freeform Field Generator
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* ExpressionEngine - by EllisLab
*
* @package ExpressionEngine
* @author ExpressionEngine Dev Team
* @copyright Copyright (c) 2003 - 2011, EllisLab, Inc.
* @license http://expressionengine.com/user_guide/license.html
* @link http://expressionengine.com
* @since Version 2.0
* @filesource
*/
// ------------------------------------------------------------------------
/**
* Freeform Field Generator Extension
*
* @package ExpressionEngine
* @subpackage Addons
* @category Extension
* @author David Ortinau
* @link http://davidortinau.com
*/
class Freeform_field_generator_ext {
public $settings = array();
public $description = 'Generates fields in Freeform based on Matrix entries';
public $docs_url = '';
public $name = 'Life Pointe Freeform Field Generator';
public $settings_exist = 'n';
public $version = '1.0';
private $EE;
/**
* Constructor
*
* @param mixed Settings array or empty string if none exist.
*/
public function __construct($settings = '')
{
$this->EE =& get_instance();
$this->settings = $settings;
}// ----------------------------------------------------------------------
/**
* Activate Extension
*
* This function enters the extension into the exp_extensions table
*
* @see http://codeigniter.com/user_guide/database/index.html for
* more information on the db class.
*
* @return void
*/
public function activate_extension()
{
// Setup custom settings in this array.
$this->settings = array();
$data = array(
'class' => __CLASS__,
'method' => 'gen_fields',
'hook' => 'entry_submission_end',
'settings' => serialize($this->settings),
'version' => $this->version,
'enabled' => 'y'
);
$this->EE->db->insert('extensions', $data);
}
// ----------------------------------------------------------------------
/**
* gen_fields
*
* @param
* @return
*/
public function gen_fields($id, $meta)
{
$this->EE->load->dbforge();
if($meta['channel_id'] != '10')
return;
$sql = 'SELECT entry_id, col_id_31, col_id_33 FROM exp_matrix_data WHERE field_id = 42 AND entry_id = ' . $id;
$formFields = $this->EE->db->query($sql);
foreach ($formFields->result() as $row)
{
$existsSql = "SELECT name FROM exp_freeform_fields WHERE name = ?";
$exists = $this->EE->db->query($existsSql, array($row->col_id_33));
if($exists->num_rows() <= 0)
{
$insertFieldSql = "INSERT INTO exp_freeform_fields (name, label) VALUES (?, ?)";
$this->EE->db->query($insertFieldSql, array($row->col_id_33, $row->col_id_31));
$fields = array(
$row->col_id_33 => array('type' => 'TEXT')
);
$this->EE->dbforge->add_column('freeform_entries', $fields);
}
}
}
// ----------------------------------------------------------------------
/**
* Disable Extension
*
* This method removes information from the exp_extensions table
*
* @return void
*/
function disable_extension()
{
$this->EE->db->where('class', __CLASS__);
$this->EE->db->delete('extensions');
}
// ----------------------------------------------------------------------
/**
* Update Extension
*
* This function performs any necessary db updates when the extension
* page is visited
*
* @return mixed void on update / false if none
*/
function update_extension($current = '')
{
if ($current == '' OR $current == $this->version)
{
return FALSE;
}
}
// ----------------------------------------------------------------------
}
/* End of file ext.freeform_field_generator.php */
/* Location: /system/expressionengine/third_party/freeform_field_generator/ext.freeform_field_generator.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment