Ejemplo n.º 1
0
 /**
  * Creates a condition based on column-value pairs.
  * @param array $condition the condition specification.
  * @return string the generated
  */
 public function buildHashCondition($condition)
 {
     $parts = [];
     foreach ($condition as $column => $value) {
         if (ArrayHelper::isTraversable($value) || $value instanceof Query) {
             // IN condition
             $parts[] = $this->buildInCondition('IN', [$column, $value]);
         } else {
             if ($value === null) {
                 $parts[] = "{$column} IS NULL";
             } else {
                 if ($column === 'dn') {
                     $parts[] = LdapUtils::getRdnFromDn($value);
                 } else {
                     $parts[] = "{$column}={$value}";
                 }
             }
         }
     }
     return count($parts) === 1 ? '(' . $parts[0] . ')' : '$(' . implode(') (', $parts) . ')';
 }
Ejemplo n.º 2
0
 /**
  * Creates a condition based on column-value pairs.
  * @param array $condition the condition specification.
  * @param array $params the binding parameters to be populated
  * @return string the generated SQL expression
  */
 public function buildHashCondition($condition, &$params)
 {
     $parts = [];
     foreach ($condition as $column => $value) {
         if (ArrayHelper::isTraversable($value) || $value instanceof Query) {
             // IN condition
             $parts[] = $this->buildInCondition('IN', [$column, $value], $params);
         } else {
             if (strpos($column, '(') === false) {
                 $column = $this->db->quoteColumnName($column);
             }
             if ($value === null) {
                 $parts[] = "{$column} IS NULL";
             } elseif ($value instanceof Expression) {
                 $parts[] = "{$column}=" . $value->expression;
                 foreach ($value->params as $n => $v) {
                     $params[$n] = $v;
                 }
             } else {
                 $phName = self::PARAM_PREFIX . count($params);
                 $parts[] = "{$column}={$phName}";
                 $params[$phName] = $value;
             }
         }
     }
     return count($parts) === 1 ? $parts[0] : '(' . implode(') AND (', $parts) . ')';
 }
Ejemplo n.º 3
0
 /**
  * Renders the option tags that can be used by [[dropDownList()]] and [[listBox()]].
  * @param string|array|null $selection the selected value(s). String for single or array for multiple selection(s).
  * @param array $items the option data items. The array keys are option values, and the array values
  * are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too).
  * For each sub-array, an option group will be generated whose label is the key associated with the sub-array.
  * If you have a list of data models, you may convert them into the format described above using
  * [[\yii\helpers\ArrayHelper::map()]].
  *
  * Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in
  * the labels will also be HTML-encoded.
  * @param array $tagOptions the $options parameter that is passed to the [[dropDownList()]] or [[listBox()]] call.
  * This method will take out these elements, if any: "prompt", "options" and "groups". See more details
  * in [[dropDownList()]] for the explanation of these elements.
  *
  * @return string the generated list options
  */
 public static function renderSelectOptions($selection, $items, &$tagOptions = [])
 {
     $lines = [];
     $encodeSpaces = ArrayHelper::remove($tagOptions, 'encodeSpaces', false);
     $encode = ArrayHelper::remove($tagOptions, 'encode', true);
     if (isset($tagOptions['prompt'])) {
         $prompt = $encode ? static::encode($tagOptions['prompt']) : $tagOptions['prompt'];
         if ($encodeSpaces) {
             $prompt = str_replace(' ', ' ', $prompt);
         }
         $lines[] = static::tag('option', $prompt, ['value' => '']);
     }
     $options = isset($tagOptions['options']) ? $tagOptions['options'] : [];
     $groups = isset($tagOptions['groups']) ? $tagOptions['groups'] : [];
     unset($tagOptions['prompt'], $tagOptions['options'], $tagOptions['groups']);
     $options['encodeSpaces'] = ArrayHelper::getValue($options, 'encodeSpaces', $encodeSpaces);
     $options['encode'] = ArrayHelper::getValue($options, 'encode', $encode);
     foreach ($items as $key => $value) {
         if (is_array($value)) {
             $groupAttrs = isset($groups[$key]) ? $groups[$key] : [];
             if (!isset($groupAttrs['label'])) {
                 $groupAttrs['label'] = $key;
             }
             $attrs = ['options' => $options, 'groups' => $groups, 'encodeSpaces' => $encodeSpaces, 'encode' => $encode];
             $content = static::renderSelectOptions($selection, $value, $attrs);
             $lines[] = static::tag('optgroup', "\n" . $content . "\n", $groupAttrs);
         } else {
             $attrs = isset($options[$key]) ? $options[$key] : [];
             $attrs['value'] = (string) $key;
             if (!array_key_exists('selected', $attrs)) {
                 $attrs['selected'] = $selection !== null && (!ArrayHelper::isTraversable($selection) && !strcmp($key, $selection) || ArrayHelper::isTraversable($selection) && ArrayHelper::isIn($key, $selection));
             }
             $text = $encode ? static::encode($value) : $value;
             if ($encodeSpaces) {
                 $text = str_replace(' ', ' ', $text);
             }
             $lines[] = static::tag('option', $text, $attrs);
         }
     }
     return implode("\n", $lines);
 }