Пример #1
0
 public function setElement($element)
 {
     if (!is_null($element['header']) && Validate::isString($element['header'])) {
         $element['header'] = Container::create(array('class' => 'panel-title', 'elements' => $element['header']));
     }
     return $element;
 }
Пример #2
0
 /**
  * Validate fields values
  *
  * @return bool
  */
 protected function validate()
 {
     foreach ($this->getTableColumns() as $name => $attr) {
         // Get field label
         $label = $this->getFieldLabel($name);
         if (is_null($this->{$name}) || '' === $this->{$name}) {
             // Check required
             if (true === $attr['required']) {
                 if (null !== $attr['default']) {
                     $this->{$name} = $attr['default'];
                 } else {
                     $this->setError($name, I18n::__('The field %s is required.', '<strong>' . $label . '</strong>'));
                 }
             }
         }
         if (!is_null($this->{$name}) && '' !== $this->{$name}) {
             // Check length
             if (!\sJo\Data\Validate::is($attr['type'], $this->{$name}, $attr['length'], $attr['values'])) {
                 $this->setError($name, I18n::__('The field %s must be %s type and have maximum size %s.', '<strong>' . $label . '</strong>', '<strong>' . $attr['type'] . '</strong>', '<strong>' . $attr['length'] . '</strong>'));
             }
             // Check unique
             if (true === $attr['unique']) {
                 $exists = $this->db()->value($this->getPrimaryKey(), array($this->getPrimaryKey() => array('value' => $this->getPrimaryValue(), 'operator' => '!='), $name => $this->{$name}));
                 if ($exists) {
                     $this->setError($name, I18n::__('This %s already exists.', '<strong>' . $label . '</strong>'));
                 }
             }
         }
     }
     return !$this->hasErrors();
 }
Пример #3
0
 function __set($name, $data)
 {
     if (Validate::isCallable($data)) {
         $this->methods[$name] = $data;
     } else {
         $this->properties[$name] = $data;
     }
     return $this;
 }
Пример #4
0
 protected final function buildElements($elements)
 {
     foreach ($elements as $name => &$element) {
         if (in_array($name, $this->wrappers) && is_array($element)) {
             foreach ($element as &$el) {
                 if (is_object($el) && method_exists($el, 'html') && in_array(__CLASS__, class_parents($el))) {
                     $el = $el->html();
                 } elseif (Validate::isCallable($el) && in_array(__CLASS__, class_parents($el))) {
                     $el = call_user_func($el);
                 } elseif (is_array($el)) {
                     $el = $this->buildElements($el);
                 }
             }
         }
     }
     return $elements;
 }
Пример #5
0
 public function setElement($element)
 {
     $head = array();
     $instance = null;
     if (is_object($element['tbody']) && method_exists($element['tbody'], 'db')) {
         $instance = $element['tbody'];
         $element['tbody'] = $instance->db()->results();
     }
     if ($element['thead'] === null && is_array($element['tbody'])) {
         $thead = array();
         foreach ($element['tbody'] as $line) {
             foreach ($line as $name => $value) {
                 if (!in_array($name, $thead)) {
                     $thead[] = $name;
                 }
             }
         }
         $element['thead'] = $thead;
         unset($thead);
     }
     if (is_array($element['thead'])) {
         $arrayThead = array();
         foreach ($element['thead'] as $key => &$thead) {
             if (!is_array($thead)) {
                 if (!Validate::isInt($key)) {
                     $thead = array('name' => $key, 'label' => $thead);
                 } else {
                     $thead = array('name' => $thead);
                 }
             }
             $thead = Lib\Arr::extend(array('name' => $key, 'callback' => null, 'align' => null, 'label' => ucfirst(isset($thead['name']) ? $thead['name'] : $key)), $thead);
             $head[] = $thead['name'];
             $arrayThead[$thead['name']] = $thead;
         }
         $element['thead'] = $arrayThead;
     }
     if (is_array($element['tbody'])) {
         if ($element['actions']) {
             if (is_array($element['thead'])) {
                 $element['thead']['actions'] = array('name' => 'actions', 'label' => Lib\I18n::__('Actions'), 'align' => 'right');
             }
             foreach ($element['tbody'] as &$line) {
                 $line->actions = '';
                 foreach ($element['actions'] as $action => $options) {
                     if (!is_array($options)) {
                         $action = $options;
                         $options = array();
                     }
                     $options = Lib\Arr::extend(array('interface' => Router::$interface, 'controller' => null, 'href' => null, 'icon' => null), $options);
                     if (!$options['href']) {
                         $options['href'] = Router::link($options['interface'], $options['controller'], array('method' => $action, $instance->getPrimaryKey() => $line->{$instance->getPrimaryKey()}));
                     }
                     if (!$options['icon']) {
                         switch ($action) {
                             case 'delete':
                                 $options['icon'] = 'trash';
                                 break;
                             case 'update':
                                 $options['icon'] = 'edit';
                                 break;
                             case 'create':
                                 $options['icon'] = 'plus';
                                 break;
                             default:
                                 $options['icon'] = $action;
                                 break;
                         }
                     }
                     $line->actions .= self::createStatic('Link', array_merge(array('icon' => $options['icon']), $options))->html();
                 }
             }
         }
         if (count($head)) {
             foreach ($element['tbody'] as &$line) {
                 foreach ($line as $name => $value) {
                     if (!in_array($name, $head)) {
                         unset($line->{$name});
                         continue;
                     }
                     if (isset($element['thead'][$name]['callback'])) {
                         $line->{$name} = call_user_func($element['thead'][$name]['callback'], $line->{$name});
                     }
                 }
             }
         }
     }
     return $element;
 }
Пример #6
0
 /**
  * @return bool
  */
 protected function validateForm()
 {
     foreach ($this->getFormFields() as $name => $attr) {
         // Get field label
         $label = $this->getFieldLabel($name);
         if (isset($attr['validate'])) {
             if (Validate::isCallable($attr['validate'])) {
                 call_user_func($attr['validate'], $attr, $name, $label);
             } elseif (preg_match("#^:([[:alnum:]]+)\$#", $attr['validate'], $match)) {
                 if (Request::env('POST')->{$name}->val() !== Request::env('POST')->{$match[1]}->val()) {
                     $this->setError($name, I18n::__('The field %s must be identical to %s.', '<strong>' . $label . '</strong>', '<strong>' . $this->getFieldLabel($match[1]) . '</strong>'));
                 }
             }
         }
     }
     return !$this->hasErrors();
 }