Ejemplo n.º 1
0
 public function layout_fields($layout, $id = NULL, $lang = NULL, $_context = NULL, $_name = NULL)
 {
     // add back in slash
     $layout = str_replace(':', '/', $layout);
     // check to make sure there is no conflict between page columns and layout vars
     $layout = $this->fuel->layouts->get($layout, 'block');
     if (!$layout) {
         return;
     }
     // sort of kludgy but not wanting to encode/unencode brackets
     if (empty($_context)) {
         $_context = $this->input->get('context', TRUE);
     }
     if (empty($_name)) {
         $_name = $this->input->get('name', TRUE);
     }
     if (empty($_name)) {
         $_name = $_context;
     }
     if (!empty($_context)) {
         $layout->set_context($_context);
     }
     if (!empty($id)) {
         $model = $layout->model();
         if (empty($model)) {
             $this->load->module_model(FUEL_FOLDER, 'fuel_pagevariables_model');
             $page_vars = $this->fuel_pagevariables_model->find_all_by_page_id($id, $lang);
             // the following will pre-populate fields of a different language to the default values
             if (empty($page_vars) and $this->fuel->language->has_multiple() and $lang != $this->fuel->language->default_option()) {
                 $page_vars = $this->fuel_pagevariables_model->find_all_by_page_id($id, $this->fuel->language->default_option());
             }
         } else {
             if (is_array($model)) {
                 $module = key($model);
                 $model = current($model);
                 $this->load->module_model($module, $model);
             } else {
                 $this->load->model($model);
             }
             $method = $layout->method();
             if (empty($method)) {
                 $method = 'find_one_array';
             }
             $key_field = $this->{$model}->key_field();
             $where[$key_field] = $id;
             $page_vars = $this->{$model}->{$method}($where);
             if (!empty($page_vars) and is_array($page_vars)) {
                 if (is_json_str($page_vars)) {
                     $page_vars = json_decode($page_vars, TRUE);
                 }
             }
         }
         // extract variables
         extract($page_vars);
         $_name = end(explode('--', $_name));
         $_name_var = str_replace(array('[', ']'), array('["', '"]'), $_name);
         if (!empty($_name_var)) {
             $_name_var_eval = '@$_name = (isset($' . $_name_var . ')) ? $' . $_name_var . ' : "";';
             @eval($_name_var_eval);
         }
         if (isset($_name)) {
             $block_vars = $_name;
             $layout->set_field_values($block_vars);
         }
     }
     $fields = $layout->fields();
     $this->load->library('form_builder');
     $this->form_builder->load_custom_fields(APPPATH . 'config/custom_fields.php');
     $this->form_builder->question_keys = array();
     $this->form_builder->submit_value = '';
     $this->form_builder->cancel_value = '';
     $this->form_builder->use_form_tag = FALSE;
     //$this->form_builder->name_prefix = 'vars';
     $this->form_builder->set_fields($fields);
     $this->form_builder->display_errors = FALSE;
     if (isset($block_vars)) {
         $this->form_builder->set_field_values($block_vars);
     }
     $form = $this->form_builder->render();
     $this->output->set_output($form);
 }
 /**
  * Returns information about the anti SPAM method to use for the form.
  * 
  * @access	protected
  * @return	array 
  */
 protected function get_antispam_params()
 {
     if (is_json_str($this->anti_spam_method)) {
         return json_decode($this->anti_spam_method, TRUE);
     }
     if (is_string($this->anti_spam_method)) {
         return array('method' => $this->anti_spam_method);
     }
     return $this->anti_spam_method;
 }
    /**
     * Creates a key / value associative array
     *
     * @access	public
     * @param	array Fields parameters
     * @return	string
     */
    public function keyval($params)
    {
        $form_builder =& $params['instance'];
        if (!isset($params['delimiter'])) {
            $params['delimiter'] = ":";
        }
        if (!isset($params['allow_numeric_indexes'])) {
            $params['allow_numeric_indexes'] = FALSE;
        }
        if (!isset($params['allow_empty_values'])) {
            $params['allow_empty_values'] = FALSE;
        }
        $split_delimiter = "\\s*" . $params['delimiter'] . "\\s*";
        $process_key = isset($params['subkey']) ? $params['subkey'] : $params['key'];
        // create an array with the key being the image name and the value being the caption (if it exists... otherwise the image name is used again)
        $func_str = '
			if (is_array($value))
			{
				foreach($value as $key => $val)
				{
					if (isset($val["' . $process_key . '"]))
					{
						if (is_string($val["' . $process_key . '"]))
						{
							$z = $val["' . $process_key . '"];
						}
						else if (is_array($val["' . $process_key . '"]) AND isset($val["' . $process_key . '"]["' . $params['name'] . '"]))
						{
							$z = $val["' . $process_key . '"]["' . $params['name'] . '"];
						}

						$json = array();
						$rows = preg_split("#\\s*\\n|,\\s*#", $z);
						foreach($rows as $r)
						{
							$vals = preg_split("#' . $split_delimiter . '#", $r);
							if (isset($vals[1]))
							{
								$v = $vals[1];
								$k = $vals[0];
								$json[$k] = $v;
							}
							else
							{
								$json[] = $vals[0];
							}
						}
						$first_item = current($json);
						if (is_string($val["' . $process_key . '"]))
						{
							$value[$key]["' . $process_key . '"] = (!empty($first_item)) ? json_encode($json) : "";
						}
						else if (is_array($val["' . $process_key . '"]) AND isset($val["' . $process_key . '"]["' . $params['name'] . '"]))
						{
							$value[$key]["' . $process_key . '"]["' . $params['name'] . '"] = (!empty($first_item)) ? json_encode($json) : "";
						}
					}
				}
				return $value;
			}
			else
			{
				$json = array();

				if (is_string($value))
				{
					$rows = preg_split("#\\s*\\n|,\\s*#", $value);
					foreach($rows as $r)
					{
						if (is_string($r))
						{
							$vals = preg_split("#' . $split_delimiter . '#", $r);
							if (isset($vals[1]))
							{
								$val = $vals[1];
								$key = $vals[0];
								$json[$key] = $val;
							}
							else
							{
								$json[] = $vals[0];
							}
						}
					}
					$first_item = current($json);
					return  (!empty($first_item)) ? json_encode($json) : "";
				}
				
			}
			';
        $func = create_function('$value', $func_str);
        $form_builder->set_post_process($params['key'], $func);
        if (!empty($params['value'])) {
            if (is_json_str($params['value'])) {
                $params['value'] = json_decode($params['value'], TRUE);
            }
            if (is_array($params['value'])) {
                $new_value = array();
                foreach ($params['value'] as $key => $val) {
                    if (!empty($val) or $params['allow_empty_values'] === TRUE and empty($val)) {
                        if (is_numeric($key) and $params['allow_numeric_indexes'] === FALSE) {
                            $new_value[] = $val;
                        } else {
                            $new_value[] = $key . $params['delimiter'] . $val;
                        }
                    }
                }
                if (!empty($new_value)) {
                    $params['value'] = implode("\n", $new_value);
                } else {
                    $params['value'] = '';
                }
            }
        } else {
            $params['value'] = '';
        }
        $params['class'] = 'no_editor';
        return $form_builder->create_textarea($params);
    }
Ejemplo n.º 4
0
 /**
  * Unserialize a saved value
  *
  * @access	public
  * @param	string	the array value to unserialize
  * @param	string	the unserialization method. If none is specified it will default to the default setting of the model which is 'json' 
  * @return	array
  */
 public function unserialize_value($val, $method = NULL)
 {
     // if not a valid field name, then we look for a key value with that name in the serialized_fields array
     $invalid_field_names = array('json', 'serialize');
     if (!in_array($method, $invalid_field_names)) {
         foreach ($this->serialized_fields as $m => $field) {
             if ($field == $method) {
                 $method = $m;
                 break;
             }
         }
     }
     if (empty($method)) {
         $method = $this->default_serialization_method;
     }
     if ($method == 'serialize') {
         if (is_serialized_str($val)) {
             $val = unserialize($val);
         }
     } else {
         if (is_json_str($val)) {
             $val = json_decode($val, TRUE);
         }
     }
     return $val;
 }
 public function get_form_fields($field = NULL)
 {
     $fields = array();
     if (!empty($this->inputs)) {
         foreach ($this->inputs as $key => $input) {
             if (is_array($input)) {
                 if (isset($input['field']['block_name'])) {
                     $input['type'] = $input['field']['block_name'];
                 } elseif (empty($input['type'])) {
                     $input['type'] = 'text';
                 }
                 $fields[$input['name']] = new Form_field();
                 // merge in extra properties
                 if (!empty($input['field'])) {
                     if (is_array($input['field'])) {
                         $input = array_merge($input, $input['field']);
                     }
                     unset($input['field']);
                 }
                 // decode json strings automatically
                 foreach ($input as $k => $v) {
                     if (is_json_str($v)) {
                         $input[$k] = json_decode($v, TRUE);
                     }
                 }
                 $fields[$input['name']]->initialize($input);
             } elseif ($input instanceof Form_field) {
                 $fields[$key] = $input;
             }
         }
     }
     if (!empty($field)) {
         if (array_key_exists($field, $fields)) {
             return $fields[$field];
         }
         return FALSE;
     }
     return $fields;
 }