/application/controllers/admin/formbuilder.php
PHP | 837 lines | 693 code | 111 blank | 33 comment | 53 complexity | edd9f264cead1d00519129f0820bb4b2 MD5 | raw file
Possible License(s): LGPL-2.1
- <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
- /**
- * Formbuilder Controller
- *
- * @link http://www.raremethod.com
- */
- class Formbuilder extends CI_Controller
- {
- function __construct()
- {
- parent::__construct();
-
- $this->load->database();
- $this->load->library(array('tank_auth'));
- $this->load->helper(array('url','admin/admin', 'content/content'));
- $this->load->helper('form');
-
- ## Get the user's ID and add it to the config array
- $config = array('userID'=>$this->tank_auth->get_user_id());
-
- ## Load the ACL library and pass it the config array
- $this->load->library('acl',$config);
-
- if(!$this->acl->hasPermission('admin_access'))
- {
- $this->session->set_flashdata('prev_url', $this->uri->uri_string());
- redirect('auth/login');
- }
-
- ## If the user does not have permission either in 'acl_user_perms' or 'acl_role_perms' redirect to login, or restricted, etc
- if(!$this->acl->hasPermission('templates'))
- {
- $this->session->set_flashdata('error_message', 'Sorry, you do not have access to Templates.');
- redirect($this->config->item('admin_url').'/dashboard');
- }
- }
-
- function preload()
- {
- $template_id = (int)$this->uri->segment(4);
-
- $fields_query = $this->db->query("SELECT * FROM rm_template_fields WHERE field_template_id = $template_id ORDER BY field_order ASC");
-
- if($fields_query->num_rows()>0)
- {
- foreach($fields_query->result() as $f)
- {
- $data = array(
- 'type' => $f->field_type,
- 'id' => 'element_'.$f->field_id,
- 'label' => $f->field_label,
- 'value' => $f->field_default_value,
- 'field_name' => $f->field_name,
- 'field_operations'=> $f->field_operations,
- 'required' => $f->field_required,
- 'field_options' => $f->field_options,
- 'description' => $f->field_description,
- 'template_id' => $template_id
- );
-
- $this->element($data);
- }
- }
- }
-
- function preload_settings()
- {
- $fields_query = $this->db->query("SELECT * FROM rm_settings ORDER BY field_order ASC");
-
- if($fields_query->num_rows()>0)
- {
- foreach($fields_query->result() as $f)
- {
- $data = array(
- 'type' => $f->field_type,
- 'id' => 'element_'.$f->setting_id,
- 'label' => $f->setting_label,
- 'value' => $f->field_default_value,
- 'field_name' => $f->setting_name,
- 'field_operations'=> $f->field_operations,
- 'required' => $f->field_required,
- 'field_options' => $f->field_options,
- 'description' => $f->field_description
- );
-
- $this->element($data);
- }
- }
- }
-
- // Used to generate an HTML preview of the form in a popup
- function preview()
- {
- $data['items'] = $this->build($_POST);
-
- if (!empty($data['items']))
- {
- $this->load->view('admin/formbuilder_preview', $data);
- }
- else
- {
- echo '<div class="warning">No elements in form!</div>';
- }
- }
-
- // Build is used to output the forms data as an HTML form
- // $data is an array generated on post from the builder.
- // By default, this is only ever used for the preview window
- function build($data)
- {
- if (!isset($data['properties'])) return false;
- $elements = $data['properties'];
-
- foreach ($elements as $k => $val)
- {
- if (!isset($data[$k])) $data[$k] = NULL;
- if (!isset($val['values'])) $val['values'] = NULL;
-
- $elements[$k]['content'] = $data[$k];
-
- $name = $k;
-
- switch ($val['type'])
- {
- case 'text': $elements[$k]['html'] = $data[$k]; break;
- case 'textarea':
- $elements[$k]['html'] = form_textarea(array(
- 'name' => $name,
- 'rows' => 5,
- 'cols' => 50,
- 'value' => $data[$k],
- 'class' => ((isset($val['required']))?'required'.((isset($val['required_vars']))?'{'.$val['required_vars'].'}':null):null)
- ));
- break;
- case 'textbox':
- $elements[$k]['html'] = form_input(array(
- 'name' =>$name,
- 'value' =>$data[$k],
- 'class' => ((isset($val['required']))?'required'.((isset($val['required_vars']))?'{'.$val['required_vars'].'}':null):null)
- ));
- break;
- case 'dropdown':
- if (!$val['values']) { unset($elements[$k]); break; }
- $options = explode(';',$val['values']);
- if (empty($options)) { unset($elements[$k]); break; }
-
- $elements[$k]['html'] = form_dropdown($name,$options);
- break;
- case 'checkbox':
- $input = null;
-
- if (!$val['values']) { unset($elements[$k]); break; }
- $options = explode(';',$val['values']);
- if (empty($options)) { unset($elements[$k]); break; }
-
- foreach ($options as $option) {
- $input .= form_checkbox($name.'[]', $option).' '.$option.'<br/>';
- }
- $elements[$k]['html'] = $input;
- break;
- case 'radio':
- $input = null;
-
- if (!$val['values']) { unset($elements[$k]); break; }
- $options = explode(';',$val['values']);
- if (empty($options)) { unset($elements[$k]); break; }
-
- foreach ($options as $option) {
- $input .= form_radio($name.'[]', $option).' '.$option.'<br/>';
- }
- $elements[$k]['html'] = $input;
- break;
- case 'datetime':
- $elements[$k]['html'] = form_input(array(
- 'name'=>$name,
- 'value'=>$data[$k],
- 'class' => 'datepicker '.((isset($val['required']))?'required'.((isset($val['required_vars']))?'{'.$val['required_vars'].'}':null):null)
- ));
- break;
- case 'fileupload':
- $elements[$k]['html'] = form_upload(array(
- 'name'=>$name,
- 'class' => ((isset($val['required']))?'required'.((isset($val['required_vars']))?'{'.$val['required_vars'].'}':null):null)
- ));
- break;
- case 'button':
- $elements[$k]['html'] = form_input(array(
- 'name'=>$name,
- 'value'=>((isset($val['value']))?$val['value']:'Button'),
- 'type'=>'button'
- ));
- break;
- }
- }
- return $elements;
- }
-
- /*
- Element is generated and spat onscreen
- */
- function element_settings($data = NULL)
- {
-
-
- if(is_array($data))
- {
- foreach($data as $var=>$column_value)
- {
- $$var = $column_value;
- }
- }
-
- if(empty($type))
- {
- $type = $this->uri->segment(4);
- }
-
-
- ## IF there is no ID, they are generating a new field, so insert a placeholder field_id until they customize the field
- if(empty($id))
- {
- $field_query = $this->db->query("SELECT MAX(setting_id) AS max_field_value FROM rm_settings");
- $field_query = $field_query->row();
- $field_id = $field_query->max_field_value+1;
-
- $sort_query = $this->db->query("SELECT MAX(field_order) AS max_sort FROM rm_settings");
- $sort_query = $sort_query->row();
- $sort_order = $sort_query->max_sort+1;
-
-
- $this->db->query("INSERT INTO rm_settings
- (
- setting_id,
- field_order,
- setting_label,
- setting_name,
- setting_value,
- field_options,
- field_type,
- field_operations
- )
- VALUES
- (
- $field_id,
- $sort_order,
- '',
- '',
- '',
- '',
- 'textbox',
- ''
- )
- ");
-
- $id = 'element_'.$field_id;
- }
-
-
- if(empty($label))
- {
- $label = 'No Label';
- }
-
- if(empty($value))
- {
- $value = '';
- }
-
- switch($type)
- {
- case 'text':
- $element = form_textarea(array(
- 'class' => 'wysiwyg',
- 'id' => $id,
- 'name' => $id,
- 'rows' => 5,
- 'cols' => 50
- ));
- break;
- case 'textarea':
- $element = form_textarea(array(
- 'name' => $id,
- 'rows' => 5,
- 'cols' => 50
- ));
- break;
- case 'textbox' : $element = form_input(array('name' => $id, 'value' => $value)); break;
- case 'dropdown' :
-
- if(empty($field_options) && empty($field_operations))
- {
- $element = form_dropdown($id,array(''=>'No Content'));
- }
- elseif(!empty($field_operations))
- {
- $element = '<select name="'.$id.'">';
-
- $element .= '<option value="">—</option>';
- $post_type = $field_operations;
- $post_results = $this->db->query("SELECT post_id, post_title FROM rm_posts WHERE post_type = '$post_type' ORDER BY post_title ASC");
-
- foreach($post_results->result() as $p)
- {
- $element .= '<option value="'.$p->post_id .'">'.$p->post_title.'</option>';
- }
- $element .= '</select>';
- }
- else
- {
- $field_options_array = explode(';', $field_options);
- $element = form_dropdown($id,$field_options_array);
- }
- break;
-
- case 'dropdown_multi' :
-
- if(empty($field_options) && empty($field_operations))
- {
- $element = form_multiselect($id,array(''=>'No Content'), '');
- }
- elseif(!empty($field_operations))
- {
- $element = '<select name="'.$id.'" multiple="multiple" style="height:150px;">';
-
- $element .= '<option value="">—</option>';
- $post_type = $field_operations;
- $post_results = $this->db->query("SELECT post_id, post_title FROM rm_posts WHERE post_type = '$post_type' ORDER BY post_title ASC");
-
- foreach($post_results->result() as $p)
- {
- $element .= '<option value="'.$p->post_id .'">'.$p->post_title.'</option>';
- }
- $element .= '</select>';
- }
- else
- {
- $field_options_array = explode(';', $field_options);
- $element = form_multiselect($id,$field_options_array);
- }
- break;
- case 'checkbox' : $element = '<span class="values '.$id.'"><input type="checkbox"></span>'; break;
- case 'radio' : $element = '<span class="values '.$id.'"><input type="radio"></span>'; break;
- case 'datetime' : $element = form_input(array('name'=>$id,'class'=>'datepicker')); break;
- case 'fileupload' : $element = form_upload($id); break;
- case 'button' : $element = form_input(array('name'=>$id,'value'=>'No Content','type'=>'button')); break;
- default : $element = null; break;
- }
-
- // Basic output list element.
- $output = "
- <li id='loaded".$id."'>
- <label for='".$id."'><a href='#' rel='".$type."' class='properties tooltip' title='Edit'>".$label."</a></label>
- <div class='block'>
- <div class='handle'><span class='icon move'>Move</span></div>
- ".$element."
- <span class='note ".$id."'>".@$description."</span>
- </div>
- <div class='clear'></div>
- <div class='attrs clear ".$id."'>
- <input type='hidden' name='properties[".$id."][type]' value='".$type."'/>
- <input type='hidden' name='properties[".$id."][label]' class='label' value='".$label."' />";
-
- if(!empty($description))
- {
- $output .= "<input type='hidden' name='properties[".$id."][description]' class='description' value='".$description."' />";
- }
-
- if(!empty($required))
- {
- $output .= "<input type='hidden' name='properties[".$id."][required]' class='required' value='".$required."' />
- ";
- }
-
- if(!empty($field_options))
- {
- $output .= "<input type='hidden' name='properties[".$id."][values]' class='values' value='".$field_options."' />
- ";
- }
-
- if(!empty($field_operations))
- {
- $output .= "<input type='hidden' name='properties[".$id."][field_operations]' class='field_operations' value='".$field_operations."' />
- ";
- }
-
- $output .= " </div>
- </li>
- ";
-
- if ($element)
- {
- // Set output to AJAX
- echo $output;
- }
- else
- {
- echo 'Could not determine the element!';
- }
- }
-
- /*
- Element is generated and spat onscreen
- */
- function element($data = NULL)
- {
- if(is_array($data))
- {
- foreach($data as $var=>$column_value)
- {
- $$var = $column_value;
- }
- }
-
- if(empty($type))
- {
- $type = $this->uri->segment(4);
- }
-
- if(empty($template_id))
- {
- $template_id = $this->uri->segment(5);
- }
-
- ## IF there is no ID, they are generating a new field, so insert a placeholder field_id until they customize the field
- if(empty($id))
- {
- $field_query = $this->db->query("SELECT MAX(field_id) AS max_field_value FROM rm_template_fields");
- $field_query = $field_query->row();
- $field_id = $field_query->max_field_value+1;
-
- $sort_query = $this->db->query("SELECT MAX(field_order) AS max_sort FROM rm_template_fields WHERE field_template_id = $template_id");
- $sort_query = $sort_query->row();
- $sort_order = $sort_query->max_sort+1;
-
- $this->db->query("INSERT INTO rm_template_fields
- (
- field_id,
- field_template_id,
- field_label,
- field_name,
- field_options,
- field_order,
- field_type,
- field_validation,
- field_operations,
- field_description,
- field_default_value,
- field_required
- )
- VALUES
- (
- $field_id,
- $template_id,
- '',
- '',
- '',
- $sort_order,
- 'textbox',
- 'text',
- '',
- '',
- '',
- 0)
- ");
-
- $id = 'element_'.$field_id;
- }
-
- if(empty($label))
- {
- $label = 'No Label';
- }
-
- if(empty($value))
- {
- $value = '';
- }
-
- switch($type)
- {
- case 'text':
- $element = form_textarea(array(
- 'class' => 'wysiwyg',
- 'id' => $id,
- 'name' => $id,
- 'rows' => 5,
- 'cols' => 50
- ));
- break;
- case 'textarea':
- $element = form_textarea(array(
- 'name' => $id,
- 'rows' => 5,
- 'cols' => 50
- ));
- break;
- case 'textbox' : $element = form_input(array('name' => $id, 'value' => $value)); break;
- case 'dropdown' :
-
- if(empty($field_options) && empty($field_operations))
- {
- $element = form_dropdown($id,array(''=>'No Content'));
- }
- elseif(!empty($field_operations))
- {
- $element = '<select name="'.$id.'">';
-
- $element .= '<option value="">—</option>';
- $post_type = $field_operations;
- $post_results = $this->db->query("SELECT post_id, post_title FROM rm_posts WHERE post_type = '$post_type' ORDER BY post_title ASC");
-
- foreach($post_results->result() as $p)
- {
- $element .= '<option value="'.$p->post_id .'">'.$p->post_title.'</option>';
- }
- $element .= '</select>';
- }
- else
- {
- $field_options_array = explode(';', $field_options);
- $element = form_dropdown($id,$field_options_array);
- }
- break;
-
- case 'dropdown_multi' :
-
- if(empty($field_options) && empty($field_operations))
- {
- $element = form_multiselect($id,array(''=>'No Content'), '');
- }
- elseif(!empty($field_operations))
- {
- $element = '<select name="'.$id.'" multiple="multiple" style="height:150px;">';
-
- $element .= '<option value="">—</option>';
- $post_type = $field_operations;
- $post_results = $this->db->query("SELECT post_id, post_title FROM rm_posts WHERE post_type = '$post_type' ORDER BY post_title ASC");
-
- foreach($post_results->result() as $p)
- {
- $element .= '<option value="'.$p->post_id .'">'.$p->post_title.'</option>';
- }
- $element .= '</select>';
- }
- else
- {
- $field_options_array = explode(';', $field_options);
- $element = form_multiselect($id,$field_options_array);
- }
- break;
- case 'checkbox' : $element = '<span class="values '.$id.'"><input type="checkbox"></span>'; break;
- case 'radio' : $element = '<span class="values '.$id.'"><input type="radio"></span>'; break;
- case 'datetime' : $element = form_input(array('name'=>$id,'class'=>'datepicker')); break;
- case 'fileupload' : $element = form_upload($id); break;
- case 'button' : $element = form_input(array('name'=>$id,'value'=>'No Content','type'=>'button')); break;
- default : $element = null; break;
- }
-
- // Basic output list element.
- $output = "
- <li id='loaded".$id."'>
- <label for='".$id."'><a href='#' rel='".$type."' class='properties tooltip' title='Edit'>".$label."</a></label>
- <div class='block'>
- <div class='handle'><span class='icon move'>Move</span></div>
- ".$element."
- <span class='note ".$id."'>".@$description."</span>
- </div>
- <div class='clear'></div>
- <div class='attrs clear ".$id."'>
- <input type='hidden' name='properties[".$id."][type]' value='".$type."'/>
- <input type='hidden' name='properties[".$id."][label]' class='label' value='".$label."' />";
-
- if(!empty($description))
- {
- $output .= "<input type='hidden' name='properties[".$id."][description]' class='description' value='".$description."' />";
- }
-
- if(!empty($required))
- {
- $output .= "<input type='hidden' name='properties[".$id."][required]' class='required' value='".$required."' />
- ";
- }
-
- if(!empty($field_options))
- {
- $output .= "<input type='hidden' name='properties[".$id."][values]' class='values' value='".$field_options."' />
- ";
- }
-
- if(!empty($field_operations))
- {
- $output .= "<input type='hidden' name='properties[".$id."][field_operations]' class='field_operations' value='".$field_operations."' />
- ";
- }
-
- $output .= " </div>
- </li>
- ";
-
- if ($element)
- {
- // Set output to AJAX
- echo $output;
- }
- else
- {
- echo 'Could not determine the element!';
- }
- }
-
- /*
- Builds a list of properties for the builder to display.
- */
- function properties()
- {
- $type = $this->uri->segment(4);
- $id = $this->uri->segment(5);
-
- $output = null;
-
- //basic options
- $options = array(
- 'Delete' => form_input(
- array(
- 'rel' => $id,
- 'name' => 'remove',
- 'value' => 'Delete Element',
- 'type' => 'button',
- 'onclick' => 'formbuilder.remove(this);'
- )
- ),
- 'Label' => form_input(
- array(
- 'name' => 'label',
- 'rel' => 'label[for='.$id.'] a'
- )
- ),
- 'Required' => array(
- 'Yes' => form_checkbox('required','1'),
- 'Type' => form_dropdown('required_vars', array(
- '' => 'Text',
- 'email' => 'Email',
- 'number' => 'Number'
- )
- )
- ),
- 'Description' => form_input(
- array(
- 'name' => 'description',
- 'rel' => '.note[class~='.$id.']'
- )
- )
- );
-
- $options_help = '<br class="clr" /><span class="icon tooltip" title="Seperate multiple values with a semicolon;<br/>Eg: test;something;here">Help</span>';
- $dropdown_help = '<br class="clr" /><span class="icon tooltip" title="Can be any type of post. Will pull a list of posts as options.">Help</span>';
-
- //specific options
- switch($type)
- {
- case 'dropdown':
- $options['Options'] = form_input(
- array(
- 'name'=>'values',
- 'class'=>'dropdown',
- 'rel'=>'select[name='.$id.']'
- )
- ).$options_help;
-
- $posts_query = $this->db->query("SELECT post_type FROM rm_post_types");
-
- $posts_config[''] = '—';
- foreach($posts_query->result() as $r)
- {
- $val = $r->post_type;
- $posts_config[$val] = $val;
- }
-
- $options['Reference Posts'] = form_dropdown('field_operations',
- $posts_config
- ).$dropdown_help;
- break;
-
-
- break;
- case 'radio':
- $options['Options'] = form_input(array('name'=>'values','class'=>'radio','rel'=>'span.values[class~='.$id.']')).$options_help;
- break;
- case 'checkbox':
- $options['Options'] = form_input(array('name'=>'values','class'=>'checkbox','rel'=>'span.values[class~='.$id.']')).$options_help;
-
- $posts_query = $this->db->query("SELECT post_type FROM rm_post_types");
-
- $posts_config[''] = '—';
- foreach($posts_query->result() as $r)
- {
- $val = $r->post_type;
- $posts_config[$val] = $val;
- }
-
- $options['Reference Posts'] = form_dropdown('field_operations',
- $posts_config
- ).$dropdown_help;
- break;
- case 'button':
- $options['Value'] = form_input(array('name'=>'value','class'=>'button','rel'=>'input[name='.$id.']'));
- unset($options['Required']); //useless
- break;
- case 'text':
- //unset($options['Label']); //useless
- unset($options['Description']); //useless
- break;
- case 'dropdown_multi' :
- $options['Options'] = form_input(
- array(
- 'name'=>'values',
- 'class'=>'dropdown',
- 'rel'=>'select[name='.$id.']'
- )
- ).$options_help;
-
- $posts_query = $this->db->query("SELECT post_type FROM rm_post_types");
-
- $posts_config[''] = '—';
- foreach($posts_query->result() as $r)
- {
- $val = $r->post_type;
- $posts_config[$val] = $val;
- }
-
- $options['Reference Posts'] = form_dropdown('field_operations',
- $posts_config
- ).$dropdown_help;
- break;
- case 'fileupload' :
- $this->load->helper('file');
-
- $rm_config = read_file('./site/config/rm_config.php');
- preg_match_all('/\$config\[\'(.*)\'\]/', $rm_config, $config_result, PREG_SET_ORDER);
-
- $image_config[''] = '—';
-
- foreach($config_result as $r)
- {
- $val = $r[1];
- $image_config[$val] = $val;
- }
-
- $options['Image Config'] = form_dropdown('field_operations',
- $image_config
- ).'<br class="clr" /><span class="icon tooltip" title="Set your image options in rm_config.php">Help</span>';
- break;
- default: break;
- }
-
- // Spit out the options for ajax
- foreach ($options as $k => $option) {
- $output .= '<li class="'.$id.'">';
- $output .= '<b>'.$k.'</b>: ';
- $output .= '<ul>';
- if (is_array($option)) {
- foreach ($option as $sk => $sub) {
- $output .= '<li class="sub"><b>'.$sk.'</b>: '.$sub.'</li>';
- }
- } else {
- $output .= '<li class="sub">'.$option.'</li>';
- }
- $output .= '</ul>';
- $output .= '</li>';
- }
- echo $output;
- }
-
- function delete_field()
- {
- $element_id = $_POST['data'];
- $element_id = explode('_', $element_id);
- $field_id = $element_id[1];
-
- $fields_query = $this->db->query("SELECT * FROM rm_template_fields LEFT JOIN rm_template_fields_data ON rm_template_fields.field_id = rm_template_fields_data.field_id WHERE rm_template_fields.field_id = $field_id LIMIT 1");
-
- if($fields_query->num_rows()>0)
- {
- $field_info = $fields_query->row();
- $field_type = $field_info->field_type;
- $field_value = $field_info->field_content;
-
- if($field_type == 'fileupload')
- {
- $this->load->library('file_functions');
- ## Delete the selected file
- $this->file_functions->delete_files($field_value);
- }
- }
-
- $this->db->query("DELETE FROM rm_template_fields WHERE field_id = $field_id LIMIT 1");
- $this->db->query("DELETE FROM rm_template_fields_data WHERE field_id = $field_id");
-
- $data = array('success'=>TRUE);
-
- echo json_encode($data);
- }
-
- function delete_setting()
- {
- $element_id = $_POST['data'];
- $element_id = explode('_', $element_id);
- $field_id = $element_id[1];
-
- $setting_query = $this->db->query("SELECT * FROM rm_settings WHERE setting_id = '$field_id' LIMIT 1");
-
- if($setting_query->num_rows()>0)
- {
- $setting_info = $setting_query->row();
- $field_type = $setting_info->field_type;
- $setting_value = $setting_info->setting_value;
-
- if($field_type == 'fileupload')
- {
- $this->load->library('file_functions');
- ## Delete the selected file
- $this->file_functions->delete_files($setting_value);
- }
-
- $this->db->query("DELETE FROM rm_settings WHERE setting_id = '$field_id' LIMIT 1");
-
- $data = array('success'=>TRUE);
- }
- else
- {
- $data = array('success'=>FALSE);
- }
-
- echo json_encode($data);
- }
- }
- ?>