Пример #1
0
 /**
  * Renders and returns the body of an ElementSelectorModal.
  *
  * @return null
  */
 public function actionGetModalBody()
 {
     $sourceKeys = craft()->request->getParam('sources');
     $elementType = $this->getElementType();
     $context = $this->getContext();
     if (is_array($sourceKeys)) {
         $sources = array();
         foreach ($sourceKeys as $key) {
             $source = $elementType->getSource($key, $context);
             if ($source) {
                 $sources[$key] = $source;
             }
         }
     } else {
         $sources = craft()->elementIndexes->getSources($elementType->getClassHandle(), $context);
     }
     $source = ArrayHelper::getFirstValue($sources);
     $this->renderTemplate('_elements/modalbody', array('context' => $context, 'elementType' => $elementType, 'sources' => $sources, 'showSidebar' => count($sources) > 1 || $sources && !empty($source['nested'])));
 }
Пример #2
0
 /**
  * Parses a service param value to a DbCommand where condition.
  *
  * @param string       $key
  * @param string|array $values
  * @param array        &$params
  *
  * @return mixed
  */
 public static function parseParam($key, $values, &$params)
 {
     // Need to do a strict check here in case $values = true
     if ($values === 'not ') {
         return '';
     }
     $conditions = array();
     $values = ArrayHelper::stringToArray($values);
     if (!count($values)) {
         return '';
     }
     $firstVal = StringHelper::toLowerCase(ArrayHelper::getFirstValue($values));
     if ($firstVal == 'and' || $firstVal == 'or') {
         $join = array_shift($values);
     } else {
         $join = 'or';
     }
     foreach ($values as $value) {
         if ($value === null) {
             $value = ':empty:';
         } else {
             if (StringHelper::toLowerCase($value) == ':notempty:') {
                 $value = 'not :empty:';
             }
         }
         $operator = static::_parseParamOperator($value);
         if (StringHelper::toLowerCase($value) == ':empty:') {
             if ($operator == '=') {
                 $conditions[] = array('or', $key . ' is null', $key . ' = ""');
             } else {
                 $conditions[] = array('and', $key . ' is not null', $key . ' != ""');
             }
         } else {
             // Find a unique param name
             $paramKey = ':' . str_replace('.', '', $key);
             $i = 1;
             while (isset($params[$paramKey . $i])) {
                 $i++;
             }
             $param = $paramKey . $i;
             $params[$param] = trim($value);
             $conditions[] = $key . $operator . $param;
         }
     }
     if (count($conditions) == 1) {
         return $conditions[0];
     } else {
         array_unshift($conditions, $join);
         return $conditions;
     }
 }
 /**
  * Loads the configurator and field manupulator code in all the
  * core supported contexts as well as providing a hook for
  * third-party contexts.
  */
 public function loader()
 {
     // Check the conditions are right to run
     if (craft()->request->isCpRequest() && craft()->userSession->isLoggedIn() && !craft()->request->isAjaxRequest()) {
         $segments = craft()->request->getSegments();
         /**
          * Work out the context for the block type groups configuration
          */
         // Entry types
         if (count($segments) == 5 && $segments[0] == 'settings' && $segments[1] == 'sections' && $segments[3] == 'entrytypes' && $segments[4] != 'new') {
             $this->loadConfigurator('#fieldlayoutform', 'entrytype:' . $segments[4]);
         }
         // Category groups
         if (count($segments) == 3 && $segments[0] == 'settings' && $segments[1] == 'categories' && $segments[2] != 'new') {
             $this->loadConfigurator('#fieldlayoutform', 'categorygroup:' . $segments[2]);
         }
         // Global sets
         if (count($segments) == 3 && $segments[0] == 'settings' && $segments[1] == 'globals' && $segments[2] != 'new') {
             $this->loadConfigurator('#fieldlayoutform', 'globalset:' . $segments[2]);
         }
         // Users
         if (count($segments) == 3 && $segments[0] == 'settings' && $segments[1] == 'users' && $segments[2] == 'fields') {
             $this->loadConfigurator('#fieldlayoutform', 'users');
         }
         // Call a hook to allow plugins to add their own configurator
         $hookedConfigurators = craft()->plugins->call('loadPimpMyMatrixConfigurator');
         foreach ($hookedConfigurators as $configurator) {
             if (isset($configurator['container']) && isset($configurator['context'])) {
                 $this->loadConfigurator($configurator['container'], $configurator['context']);
             }
         }
         /**
          * Work out the context for the Matrix field manipulation
          */
         // Global
         $context = 'global';
         // Entry types
         if (count($segments) == 3 && $segments[0] == 'entries') {
             if ($segments[2] == 'new') {
                 $section = craft()->sections->getSectionByHandle($segments[1]);
                 $sectionEntryTypes = $section->getEntryTypes();
                 $entryType = ArrayHelper::getFirstValue($sectionEntryTypes);
             } else {
                 $entryId = explode('-', $segments[2])[0];
                 $entry = craft()->entries->getEntryById($entryId);
                 if ($entry) {
                     $entryType = $entry->type;
                 }
             }
             $context = 'entrytype:' . $entryType->id;
         } else {
             if (count($segments) == 3 && $segments[0] == 'categories') {
                 $group = craft()->categories->getGroupByHandle($segments[1]);
                 if ($group) {
                     $context = 'categorygroup:' . $group->id;
                 }
             } else {
                 if (count($segments) == 2 && $segments[0] == 'globals') {
                     $set = craft()->globals->getSetByHandle($segments[1]);
                     if ($set) {
                         $context = 'globalset:' . $set->id;
                     }
                 } else {
                     if (count($segments) == 1 && $segments[0] == 'myaccount' || count($segments) == 2 && $segments[0] == 'users') {
                         $context = 'users';
                     }
                 }
             }
         }
         // Call a hook to allow plugins to add their own field manipulators
         $hookedFieldManipulators = craft()->plugins->call('loadPimpMyMatrixFieldManipulator');
         foreach ($hookedFieldManipulators as $hookedContext) {
             if (is_string($hookedContext)) {
                 $context = $hookedContext;
             }
         }
         // Run the field manipulation code
         $this->loadFieldManipulator($context);
     }
 }
Пример #4
0
 /**
  * Deletes caches that include a given element(s).
  *
  * @param BaseElementModel|BaseElementModel[] $elements The element(s) whose caches should be deleted.
  *
  * @return bool
  */
 public function deleteCachesByElement($elements)
 {
     if ($this->_deletedAllCaches || !$this->_isTemplateCachingEnabled()) {
         return false;
     }
     if (!$elements) {
         return false;
     }
     if (is_array($elements)) {
         $firstElement = ArrayHelper::getFirstValue($elements);
     } else {
         $firstElement = $elements;
         $elements = array($elements);
     }
     $deleteQueryCaches = empty($this->_deletedCachesByElementType[$firstElement->getElementType()]);
     $elementIds = array();
     foreach ($elements as $element) {
         $elementIds[] = $element->id;
     }
     return $this->deleteCachesByElementId($elementIds, $deleteQueryCaches);
 }
Пример #5
0
 /**
  * Returns the type of entry.
  *
  * @return EntryTypeModel|null
  */
 public function getType()
 {
     $section = $this->getSection();
     if ($section) {
         $sectionEntryTypes = $section->getEntryTypes('id');
         if ($sectionEntryTypes) {
             if ($this->typeId && isset($sectionEntryTypes[$this->typeId])) {
                 return $sectionEntryTypes[$this->typeId];
             } else {
                 // Just return the first one
                 return ArrayHelper::getFirstValue($sectionEntryTypes);
             }
         }
     }
 }
Пример #6
0
 /**
  * Parses a query param value and returns a {@link \CDbCommand::where()}-compatible condition.
  *
  * If the `$value` is a string, it will automatically be converted to an array, split on any commas within the
  * string (via {@link ArrayHelper::stringToArray()}). If that is not desired behavior, you can escape the comma
  * with a backslash before it.
  *
  * The first value can be set to either `'and'` or `'or'` to define whether *all* of the values must match, or
  * *any*. If it’s neither `'and'` nor `'or'`, then `'or'` will be assumed.
  *
  * Values can begin with the operators `'not '`, `'!='`, `'<='`, `'>='`, `'<'`, `'>'`, or `'='`. If they don’t,
  * `'='` will be assumed.
  *
  * Values can also be set to either `':empty:'` or `':notempty:'` if you want to search for empty or non-empty
  * database values. (An “empty” value is either NULL or an empty string of text).
  *
  * @param string       $column  The database column that the param is targeting.
  * @param string|array $value   The param value(s).
  * @param array        &$params The {@link \CDbCommand::$params} array.
  *
  * @return mixed
  */
 public static function parseParam($column, $value, &$params)
 {
     // Need to do a strict check here in case $value = true
     if ($value === 'not ') {
         return '';
     }
     $conditions = array();
     $value = ArrayHelper::stringToArray($value);
     if (!count($value)) {
         return '';
     }
     $firstVal = StringHelper::toLowerCase(ArrayHelper::getFirstValue($value));
     if ($firstVal == 'and' || $firstVal == 'or') {
         $join = array_shift($value);
     } else {
         $join = 'or';
     }
     foreach ($value as $val) {
         static::_normalizeEmptyValue($val);
         $operator = static::_parseParamOperator($val);
         if (StringHelper::toLowerCase($val) == ':empty:') {
             if ($operator == '=') {
                 $conditions[] = array('or', $column . ' is null', $column . ' = ""');
             } else {
                 $conditions[] = array('and', $column . ' is not null', $column . ' != ""');
             }
         } else {
             // Trim any whitespace from the value
             $val = trim($val);
             // This could be a LIKE condition
             if ($operator == '=' || $operator == '!=') {
                 $val = preg_replace('/^\\*|(?<!\\\\)\\*$/', '%', $val, -1, $count);
                 $like = (bool) $count;
             } else {
                 $like = false;
             }
             // Unescape any asterisks
             $val = str_replace('\\*', '*', $val);
             if ($like) {
                 $conditions[] = array($operator == '=' ? 'like' : 'not like', $column, $val);
             } else {
                 // Find a unique param name
                 $paramKey = ':' . str_replace('.', '', $column);
                 $i = 1;
                 while (isset($params[$paramKey . $i])) {
                     $i++;
                 }
                 $param = $paramKey . $i;
                 $params[$param] = $val;
                 $conditions[] = $column . $operator . $param;
             }
         }
     }
     if (count($conditions) == 1) {
         return $conditions[0];
     } else {
         array_unshift($conditions, $join);
         return $conditions;
     }
 }