Permalink
Browse files

Fixing synform object access

  • Loading branch information...
1 parent 1ef0975 commit b72a1847fc2675119b697b007616a67a788da71d @areida areida committed May 16, 2012
View
8 classes/Synapse/Synform.php
@@ -201,11 +201,7 @@ public function name()
*/
public function open($action = NULL, array $attributes = array())
{
- // Create the form element before rendering it
- $this->_form = $this->form($this->_name);
-
-
- return $this->_form->open($action, $attributes);
+ return Form::open($action, $attributes);
}
/**
@@ -228,7 +224,7 @@ public function open_multipart($action = NULL, array $attributes = array())
*/
public function close()
{
- return $this->_form->close();
+ return Form::close();
}
}
View
73 classes/Synapse/Synform/Element.php
@@ -31,9 +31,7 @@
*
* @var array
*/
- protected $_attributes = array(
- 'class' => array(),
- );
+ protected $_attributes = array();
/**
* An array of messages organized into groups
@@ -285,7 +283,7 @@ public function set_label($label)
return $this;
}
- public function label()
+ public function get_label()
{
if ( ! $this->_has_label)
return FALSE;
@@ -317,15 +315,78 @@ public function label()
public function __toString()
{
- return '';
+ try
+ {
+ return $this->render();
+ }
+ catch (Exception $e)
+ {
+ // Display the exception message
+ Kohana_Exception::handler($e);
+
+ return '';
+ }
}
/**
* Renders the View for this element
*
* @return string
*/
- public function render() {}
+ public function render()
+ {
+ return '';
+ }
+
+ public function input()
+ {
+ $method = new ReflectionMethod('Form', $this->_attributes['type']);
+ $element = $method->invokeArgs(null, array($this->_attributes['name'], $this->value(), $this->_attributes));
+
+ return $element;
+ }
+
+ public function label()
+ {
+ $label = $this->_label;
+
+ if ($required = Arr::get($this->_attributes, 'required'))
+ {
+ $label = $label.($required === TRUE ? '*' : $required);
+ }
+
+ return Form::label($this->_attributes['name'], $label);
+ }
+
+ public function container()
+ {
+ $attributes = array(
+ 'class' => 'form-item control-group '.$this->_attributes['type'],
+ 'id' => $this->_name.'-'.'container',
+ );
+
+ if (Arr::get($this->_messages, 'errors'))
+ {
+ $attributes['class'] = $attributes['class'].' error';
+ }
+
+ if (Arr::get($this->_attributes, 'required'))
+ {
+ $attributes['class'] = $attributes['class'].' required';
+ }
+
+ return HTML::attributes($attributes);
+ }
+
+ public function value()
+ {
+ return ($this->_settings->get_value($this->_path) ?: NULL);
+ }
+
+ public function errors()
+ {
+ return Arr::get($this->_messages, 'errors', NULL);
+ }
/**
* Returns the full path to the View file
View
2 classes/Synapse/Synform/Field/Button.php
@@ -1,6 +1,6 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
-class Synapse_Synform_Field_Button extends Synapse_Synform_Element {
+class Synapse_Synform_Field_Button extends Synform_Element {
protected $_view = 'button/button';
View
7 classes/Synapse/Synform/Field/Checkbox.php
@@ -1,6 +1,6 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
-class Synapse_Synform_Field_Checkbox extends Synapse_Synform_Element {
+class Synapse_Synform_Field_Checkbox extends Synform_Element {
protected $_view = 'input/checkbox';
@@ -27,4 +27,9 @@ public function set_value($value)
return $this;
}
+ public function input()
+ {
+ return Form::checkbox($this->_attributes['name'], $this->_options, $this->value(), $this->_attributes);
+ }
+
}
View
7 classes/Synapse/Synform/Field/File.php
@@ -1,6 +1,6 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
-class Synapse_Synform_Field_File extends Synapse_Synform_Element {
+class Synapse_Synform_Field_File extends Synform_Element {
protected $_view = 'input/upload';
@@ -11,4 +11,9 @@ public function __construct($name)
$this->set_attribute('type', 'file');
}
+ public function input()
+ {
+ return Form::file($this->_attributes['name'], $this->_attributes);
+ }
+
}
View
2 classes/Synapse/Synform/Field/Form.php
@@ -1,6 +1,6 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
-class Synapse_Synform_Field_Form extends Synapse_Synform_Element {
+class Synapse_Synform_Field_Form extends Synform_Element {
protected $_has_label = FALSE;
View
77 classes/Synapse/Synform/Field/Group.php
@@ -0,0 +1,77 @@
+<?php defined('SYSPATH') OR die('No direct access allowed.');
+/**
+ * @package YurikoCMS
+ * @author Lorenzo Pisani - Zeelot
+ * @copyright (c) 2008-2010 Lorenzo Pisani
+ * @license http://yurikocms.com/license
+ */
+class Synapse_Synform_Field_Group extends Synform_Element {
+
+ protected $_has_label = FALSE;
+
+ protected $_view = 'group';
+
+ /**
+ * Returns an instance of an element
+ * Uses the YForm instance set in the settings
+ * Will prepend the name of this group
+ * ex: user[username] when this group is called `user` and the field is called `username`
+ *
+ * @param string $method
+ * @param array $args
+ * @return object
+ */
+ public function __call($method, $args)
+ {
+ // Element name is always first
+ $name = array_shift($args);
+
+ if (preg_match('#\[([^\[\]]++)\]#', $name))
+ {
+ // This already has brackets so we want foo[bar][moo] not foo[bar[moo]]
+ $pos = strpos($name, '[');
+ $name = $this->_name.'['.substr($name, 0, $pos).']'.substr($name, $pos);
+ }
+ else
+ {
+ $name = $this->_name.'['.$name.']';
+ }
+
+ // Push the new name back onto the front of $args
+ array_unshift($args, $name);
+
+ return ($this->_settings !== NULL)
+ // Use the above settings object to create our element
+ ? $this->_settings->__call($method, $args)
+ // We are creating elements without a form object
+ : YForm::create_element($method, $args);
+ }
+
+ /**
+ * Renders the view for opening a form group
+ *
+ * @return string
+ */
+ public function open($action = NULL, array $attributes = NULL)
+ {
+ return View::factory($this->view())
+ ->set('object', $this)
+ ->set('attributes', $attributes)
+ ->set('open', TRUE)
+ ->render();
+ }
+
+ /**
+ * Renders the view for closing a form group
+ *
+ * @return string
+ */
+ public function close()
+ {
+ return View::factory($this->view())
+ ->set('object', $this)
+ ->set('open', FALSE)
+ ->render();
+ }
+
+}
View
2 classes/Synapse/Synform/Field/Hidden.php
@@ -1,6 +1,6 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
-class Synapse_Synform_Field_Hidden extends Synapse_Synform_Element {
+class Synapse_Synform_Field_Hidden extends Synform_Element {
protected $_has_label = FALSE;
View
2 classes/Synapse/Synform/Field/Password.php
@@ -1,6 +1,6 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
-class Synapse_Synform_Field_Password extends Synapse_Synform_Element {
+class Synapse_Synform_Field_Password extends Synform_Element {
protected $_view = 'input/password';
View
7 classes/Synapse/Synform/Field/Radio.php
@@ -1,6 +1,6 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
-class Synapse_Synform_Field_Radio extends Synapse_Synform_Element {
+class Synapse_Synform_Field_Radio extends Synform_Element {
protected $_view = 'input/radio';
@@ -34,4 +34,9 @@ public function set_value($value)
return $this;
}
+ public function input()
+ {
+ return Form::radio($this->_attributes['name'], $this->_options, $this->value(), $this->_attributes);
+ }
+
}
View
53 classes/Synapse/Synform/Field/Select.php
@@ -0,0 +1,53 @@
+<?php defined('SYSPATH') OR die('No direct access allowed.');
+
+class Synapse_Synform_Field_Select extends Synform_Element {
+
+ protected $_has_label = TRUE;
+
+ protected $_view = 'choice/select';
+
+ protected $_options = array();
+ protected $_option_attributes = array();
+
+ protected $_settings;
+
+ public function __construct($name)
+ {
+ parent::__construct($name);
+
+ $this->_attributes['type'] = 'select';
+ }
+
+ public function add_option($value, $name, array $attributes = array())
+ {
+ $this->_options[$value] = $name;
+ $this->_option_attributes[$value] = $attributes;
+
+ return $this;
+ }
+
+ public function add_options(array $options)
+ {
+ foreach ($options as $value => $name)
+ {
+ $this->add_option($value, $name);
+ }
+ return $this;
+ }
+
+ public function options()
+ {
+ return $this->_options;
+ }
+
+ public function option_attributes($value)
+ {
+ return Arr::get($this->_option_attributes, $value, array());
+ }
+
+ public function input()
+ {
+ return Form::select($this->_attributes['name'], $this->_options, $this->_attributes['value'], $this->_attributes);
+ }
+
+}
View
6 classes/Synapse/Synform/Field/Submit.php
@@ -1,6 +1,6 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
-class Synapse_Synform_Field_Submit extends Synapse_Synform_Element {
+class Synapse_Synform_Field_Submit extends Synform_Element {
protected $_view = 'button/submit';
@@ -11,4 +11,8 @@ public function __construct($name)
$this->set_attribute('type', 'submit');
}
+ public function input()
+ {
+ return Form::submit($this->_attributes['name'], $this->_label, $this->_attributes);
+ }
}
View
6 classes/Synapse/Synform/Field/Text.php
@@ -1,6 +1,6 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
-class Synapse_Synform_Field_Text extends Synapse_Synform_Element {
+class Synapse_Synform_Field_Text extends Synform_Element {
protected $_view = 'input/text';
@@ -11,4 +11,8 @@ public function __construct($name)
$this->set_attribute('type', 'text');
}
+ public function input()
+ {
+ return Form::input($this->_attributes['name'], $this->value(), $this->_attributes);
+ }
}
View
2 classes/Synapse/Synform/Field/Textarea.php
@@ -1,6 +1,6 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
-class Synapse_Synform_Field_Textarea extends Synapse_Synform_Element {
+class Synapse_Synform_Field_Textarea extends Synform_Element {
protected $_view = 'input/textarea';

0 comments on commit b72a184

Please sign in to comment.