예제 #1
0
 protected function tagOptions(array $options = array(), $escape = false)
 {
     $opts = array();
     if (isset($options['allowBlankAttrs'])) {
         $allowBlankAttrs = true;
         unset($options['allowBlankAttrs']);
     } else {
         $allowBlankAttrs = false;
     }
     foreach ($options as $opt => $val) {
         # "class" attribute allows array.
         if ($opt == 'class' && is_array($val)) {
             $val = implode(' ', \Rails\Toolbox\ArrayTools::flatten($val));
         }
         if (is_array($val)) {
             $val = implode(' ', $val);
         }
         if ((string) $val === '' && !$allowBlankAttrs) {
             continue;
         }
         if (is_int($opt)) {
             $opts[] = $val;
         } else {
             $escape && ($val = htmlentities($val));
             $opts[] = $opt . '="' . $val . '"';
         }
     }
     return implode(' ', $opts);
 }
예제 #2
0
 /**
  * $query->where("foo = ? AND bar = ?", $foo, $bar);
  * $query->where("foo = ? AND bar = ?", $foo, [$bar, $baz]);
  * $query->where("foo = :foo AND bar = :bar", ['foo' => $foo, 'bar' => $bar]);
  * $query->where("foo = true");
  * 
  * Following 2 methods are basically the same, first is recommended over the second:
  * $query->where(["foo" => $foo, "bar" => $bar]);
  * $query->where("foo", true, "bar", $bar);
  *
  * Can't do:
  * $query->where("foo = ? AND bar = ?", [$foo, $bar]);
  * $query->where(["foo = ? AND bar = ?", $foo, $bar]);
  * $query->where(["foo", $foo]);
  */
 public function where()
 {
     $args = func_get_args();
     $count = count($args);
     if ($count == 1) {
         if (is_array($args[0])) {
             # This is expected to be a column => value associated array.
             # In this case, the array is stored as is.
             $this->where[] = $args[0];
         } else {
             # This is expected to be a string.
             $this->where[] = $args[0];
         }
     } elseif ($count) {
         # Case: $query->where("foo", true, "bar_baz", $bar);
         if ($count >= 2 && is_int($count / 2) && (!is_array($args[1]) || Toolbox\ArrayTools::isIndexed($args[1])) && is_bool(strpos($args[0], ' '))) {
             $where = [];
             foreach ($args as $key => $value) {
                 $key++;
                 if ($key && !($key % 2)) {
                     $where[$next_key] = $value;
                 } else {
                     $next_key = $value;
                 }
             }
             $this->where[] = $where;
         } else {
             $this->where[] = array_shift($args);
             # Case: $query->where('foo => :foo', ['foo' => $foo]);
             if ($count == 2 && is_array($args[0]) && !Toolbox\ArrayTools::isIndexed($args[0])) {
                 $args = $args[0];
             }
             if ($args) {
                 $this->where_params = array_merge($this->where_params, $args);
             }
         }
     }
     return $this;
 }
예제 #3
0
 /**
  * Note: For options to recognize the $tag_value, it must be identical to the option's value.
  */
 public function optionsForSelect($options, $tag_value = null)
 {
     # New feature: accept anonymous functions that will return options.
     if ($options instanceof Closure) {
         $options = $options();
         /**
          * New feature: accept collection as option in index 0, in index 1 the option name and in index 2 the value
          * which are the properties of the models that will be used.
          * Example: [ Category::all(), 'name', 'id' ]
          * The second and third indexes can be either:
          *  - An attribute name
          *  - A public property
          *  - A method of the model that will return the value for the option/name
          */
     } elseif (is_array($options) && count($options) == 3 && ArrayTools::isIndexed($options) && $options[0] instanceof \Rails\ActiveModel\Collection) {
         list($models, $optionName, $valueName) = $options;
         $options = [];
         if ($models->any()) {
             $modelClass = get_class($models[0]);
             foreach ($models as $m) {
                 if ($modelClass::isAttribute($optionName)) {
                     $option = $m->getAttribute($optionName);
                 } elseif ($modelClass::hasPublicProperty($optionName)) {
                     $option = $m->{$optionName};
                 } else {
                     # We assume it's a method.
                     $option = $m->{$optionName}();
                 }
                 if ($modelClass::isAttribute($valueName)) {
                     $value = $m->getAttribute($valueName);
                 } elseif ($modelClass::hasPublicProperty($optionName)) {
                     $option = $m->{$optionName};
                 } else {
                     # We assume it's a method.
                     $value = $m->{$valueName}();
                 }
                 $options[$option] = $value;
             }
         }
     }
     $tag_value = (string) $tag_value;
     $tags = [];
     foreach ($options as $name => $value) {
         $value = (string) $value;
         $tags[] = $this->_form_field_tag('option', null, $name, array('selected' => $value === $tag_value ? '1' : '', 'id' => '', 'value' => $value), true);
     }
     return implode("\n", $tags);
 }
예제 #4
0
 private function addNestedParams(&$url, array &$params)
 {
     if ($this->scopeParams) {
         $scopeParams = [];
         foreach ($this->scopeParams as $k => $param) {
             $scopeParams = array_merge($scopeParams, $param);
         }
         $params = array_merge($scopeParams, $params);
     }
     if ($this->modules) {
         if (!isset($params['module'])) {
             $params['module'] = [];
         } else {
             $params['module'] = explode('/', $params['module']);
         }
         $params['modules'] = array_filter(array_merge($this->modules, $params['module']));
         unset($params['module']);
     }
     if ($this->paths) {
         if (!isset($params['path'])) {
             $params['path'] = [];
         } else {
             $params['path'] = explode('/', $params['path']);
         }
         $params['paths'] = array_filter(array_merge(Toolbox\ArrayTools::flatten($this->paths), $params['path']));
         unset($params['path']);
     }
 }