/**
  * @param DataList $list
  * @param array    $filters
  * @param DataObject|string $sing - just a singleton object we can get information off of
  * @return DataList
  */
 public function addFiltersToDataList($list, array $filters, $sing = null)
 {
     if (!$sing) {
         $sing = singleton($list->dataClass());
     }
     if (is_string($sing)) {
         $sing = singleton($sing);
     }
     if (!empty($filters)) {
         foreach ($filters as $filterField => $filterVal) {
             if ($sing->hasExtension('HasStaticAttributes') && preg_match(self::config()->attribute_facet_regex, $filterField, $matches)) {
                 //					$sav = $sing->StaticAttributeValues();
                 //					Debug::log("sav = {$sav->getJoinTable()}, {$sav->getLocalKey()}, {$sav->getForeignKey()}");
                 //					$list = $list
                 //						->innerJoin($sav->getJoinTable(), "\"{$sing->baseTable()}\".\"ID\" = \"{$sav->getJoinTable()}\".\"{$sav->getLocalKey()}\"")
                 //						->filter("\"{$sav->getJoinTable()}\".\"{$sav->getForeignKey()}\"", $filterVal)
                 //					;
                 // TODO: This logic should be something like the above, but I don't know
                 // how to get the join table from a singleton (which returns an UnsavedRelationList
                 // instead of a ManyManyList). I've got a deadline to meet, though, so this
                 // will catch the majority of cases as long as the extension is applied to the
                 // Product class instead of a subclass.
                 $list = $list->innerJoin('Product_StaticAttributeTypes', "\"SiteTree\".\"ID\" = \"Product_StaticAttributeTypes\".\"ProductID\"")->innerJoin('ProductAttributeValue', "\"Product_StaticAttributeTypes\".\"ProductAttributeTypeID\" = \"ProductAttributeValue\".\"TypeID\"")->innerJoin('Product_StaticAttributeValues', "\"SiteTree\".\"ID\" = \"Product_StaticAttributeValues\".\"ProductID\" AND \"ProductAttributeValue\".\"ID\" = \"Product_StaticAttributeValues\".\"ProductAttributeValueID\"")->filter("Product_StaticAttributeValues.ProductAttributeValueID", $filterVal);
             } else {
                 $list = $list->filter($this->processFilterField($sing, $filterField, $filterVal));
             }
         }
     }
     return $list;
 }
 public function getManipulatedData(GridField $gridField, SS_List $list)
 {
     if (!$list instanceof RelationList) {
         user_error('GridFieldManyRelationHandler requires the GridField to have a RelationList. Got a ' . get_class($list) . ' instead.', E_USER_WARNING);
     }
     $state = $this->getState($gridField);
     // We don't use setupState() as we need the list
     if ($state->FirstTime) {
         $state->RelationVal = array_values($list->getIdList()) ?: array();
     }
     if (!$state->ShowingRelation && $this->useToggle) {
         return $list;
     }
     $query = clone $list->dataQuery();
     try {
         $query->removeFilterOn($this->cheatList->getForeignIDFilter($list));
     } catch (InvalidArgumentException $e) {
         /* NOP */
     }
     $orgList = $list;
     $list = new DataList($list->dataClass());
     $list = $list->setDataQuery($query);
     if ($orgList instanceof ManyManyList) {
         $joinTable = $this->cheatManyList->getJoinTable($orgList);
         $baseClass = ClassInfo::baseDataClass($list->dataClass());
         $localKey = $this->cheatManyList->getLocalKey($orgList);
         $query->leftJoin($joinTable, "\"{$joinTable}\".\"{$localKey}\" = \"{$baseClass}\".\"ID\"");
         $list = $list->setDataQuery($query);
     }
     return $list;
 }
 public function __construct($controller, $name, DataList $list)
 {
     $this->list = $list;
     $class = $list->dataClass();
     $singleton = singleton($class);
     $fields = $singleton->getFrontEndFields();
     $fields->unshift(new ListEditField("EditList", $this->list));
     $fields->push(new HiddenField("ID", "ID"));
     $actions = new FieldList(new FormAction("savenew", "Save " . $singleton->i18n_singular_name()));
     parent::__construct($controller, $name, $fields, $actions);
     $this->setupEditingMode();
     $singleton->extend('updateListEditForm', $this);
     //all fields are required
     if (!$this->validator) {
         $this->setValidator(new RequiredFields(array_keys($fields->saveableFields())));
     }
 }
 /**
  * Gets the table which contains the sort field.
  *
  * @param DataList $list
  * @return string
  */
 public function getSortTable(DataList $list)
 {
     $field = $this->getSortField();
     if ($list instanceof ManyManyList) {
         $extra = $list->getExtraFields();
         $table = $list->getJoinTable();
         if ($extra && array_key_exists($field, $extra)) {
             return $table;
         }
     }
     $classes = ClassInfo::dataClassesFor($list->dataClass());
     foreach ($classes as $class) {
         if (singleton($class)->hasOwnTableDatabaseField($field)) {
             return $class;
         }
     }
     throw new Exception("Couldn't find the sort field '{$field}'");
 }
 public function __construct(DataList $list)
 {
     $this->list = $list;
     //TODO: user error if list is null
     parent::__construct($this->list->dataClass());
 }
Example #6
0
 /**
  * Returns a SQL object representing the search context for the given
  * list of query parameters.
  *
  * @param array $searchParams Map of search criteria, mostly taked from $_REQUEST.
  *  If a filter is applied to a relationship in dot notation,
  *  the parameter name should have the dots replaced with double underscores,
  *  for example "Comments__Name" instead of the filter name "Comments.Name".
  * @param string|array $sort Database column to sort on.
  *  Falls back to {@link DataObject::$default_sort} if not provided.
  * @param string|array $limit
  * @param DataList $existingQuery
  * @return DataList
  */
 public function getQuery($searchParams, $sort = false, $limit = false, $existingQuery = null)
 {
     if ($existingQuery) {
         if (!$existingQuery instanceof DataList) {
             throw new InvalidArgumentException("existingQuery must be DataList");
         }
         if ($existingQuery->dataClass() != $this->modelClass) {
             throw new InvalidArgumentException("existingQuery's dataClass is " . $existingQuery->dataClass() . ", {$this->modelClass} expected.");
         }
         $query = $existingQuery;
     } else {
         $query = DataList::create($this->modelClass);
     }
     if (is_array($limit)) {
         $query = $query->limit(isset($limit['limit']) ? $limit['limit'] : null, isset($limit['start']) ? $limit['start'] : null);
     } else {
         $query = $query->limit($limit);
     }
     $query = $query->sort($sort);
     // hack to work with $searchParems when it's an Object
     $searchParamArray = array();
     if (is_object($searchParams)) {
         $searchParamArray = $searchParams->getVars();
     } else {
         $searchParamArray = $searchParams;
     }
     foreach ($searchParamArray as $key => $value) {
         $key = str_replace('__', '.', $key);
         if ($filter = $this->getFilter($key)) {
             $filter->setModel($this->modelClass);
             $filter->setValue($value);
             if (!$filter->isEmpty()) {
                 $query = $query->alterDataQuery(array($filter, 'apply'));
             }
         }
     }
     if ($this->connective != "AND") {
         throw new Exception("SearchContext connective '{$this->connective}' not supported after ORM-rewrite.");
     }
     return $query;
 }