/**
  * If an object ID is set, add the object to the list
  *
  * @param GridField $gridField
  * @param SS_List $dataList
  * @return SS_List
  */
 public function getManipulatedData(GridField $gridField, SS_List $dataList)
 {
     if (!$gridField->State->GridFieldAddRelation) {
         return $dataList;
     }
     $objectID = Convert::raw2sql($gridField->State->GridFieldAddRelation);
     if ($objectID) {
         $object = DataObject::get_by_id($dataList->dataclass(), $objectID);
         if ($object) {
             if ($this->_item_limit > 0 && $dataList->count() + 1 > $this->_item_limit) {
                 $gridField->getForm()->getController()->getResponse()->addHeader('X-Status', _t('LimitedRelationsGridField.ITEM_LIMIT_REACHED', '_You cannot add any more items, you can only add {count} items. Please remove one then try again.', array('count' => $this->_item_limit)));
             } else {
                 $dataList->add($object);
             }
         }
     }
     $gridField->State->GridFieldAddRelation = null;
     return $dataList;
 }
 /**
  * Takes a list of groups and members and return a list of unique member.
  *
  * @param SS_List $groups
  * @param SS_List $members
  *
  * @return ArrayList
  */
 public static function merge_owners(SS_List $groups, SS_List $members)
 {
     $contentReviewOwners = new ArrayList();
     if ($groups->count()) {
         $groupIDs = array();
         foreach ($groups as $group) {
             $familyIDs = $group->collateFamilyIDs();
             if (is_array($familyIDs)) {
                 $groupIDs = array_merge($groupIDs, array_values($familyIDs));
             }
         }
         array_unique($groupIDs);
         if (count($groupIDs)) {
             $groupMembers = DataObject::get("Member")->where("\"Group\".\"ID\" IN (" . implode(",", $groupIDs) . ")")->leftJoin("Group_Members", "\"Member\".\"ID\" = \"Group_Members\".\"MemberID\"")->leftJoin("Group", "\"Group_Members\".\"GroupID\" = \"Group\".\"ID\"");
             $contentReviewOwners->merge($groupMembers);
         }
     }
     $contentReviewOwners->merge($members);
     $contentReviewOwners->removeDuplicates();
     return $contentReviewOwners;
 }
 /**
  * This is super-slow. I'm assuming if you're using facets you
  * probably also ought to be using Solr or something else. Or
  * maybe you have unlimited time and can refactor this feature
  * and submit a pull request...
  *
  * TODO: If this is going to be used for categories we're going
  * to have to really clean it up and speed it up.
  * Suggestion:
  *  - option to turn off counts
  *  - switch order of nested array so we don't go through results unless needed
  *  - if not doing counts, min/max and link facets can be handled w/ queries
  *  - separate that bit out into a new function
  * NOTE: This is partially done with the "faster_faceting" config
  * option but more could be done, particularly by covering link facets as well.
  *
  * Output - list of ArrayData in the format:
  *   Label - name of the facet
  *   Source - field name of the facet
  *   Type - one of the ShopSearch::FACET_TYPE_XXXX constants
  *   Values - SS_List of possible values for this facet
  *
  * @param SS_List $matches
  * @param array $facetSpec
  * @param bool $autoFacetAttributes [optional]
  * @return ArrayList
  */
 public function buildFacets(SS_List $matches, array $facetSpec, $autoFacetAttributes = false)
 {
     $facets = $this->expandFacetSpec($facetSpec);
     if (!$autoFacetAttributes && (empty($facets) || !$matches || !$matches->count())) {
         return new ArrayList();
     }
     $fasterMethod = (bool) $this->config()->faster_faceting;
     // fill them in
     foreach ($facets as $field => &$facet) {
         if (preg_match(self::config()->attribute_facet_regex, $field, $m)) {
             $this->buildAttributeFacet($matches, $facet, $m[1]);
             continue;
         }
         // NOTE: using this method range and checkbox facets don't get counts
         if ($fasterMethod && $facet['Type'] != ShopSearch::FACET_TYPE_LINK) {
             if ($facet['Type'] == ShopSearch::FACET_TYPE_RANGE) {
                 if (isset($facet['RangeMin'])) {
                     $facet['MinValue'] = $facet['RangeMin'];
                 }
                 if (isset($facet['RangeMax'])) {
                     $facet['MaxValue'] = $facet['RangeMax'];
                 }
             }
             continue;
         }
         foreach ($matches as $rec) {
             // If it's a range facet, set up the min/max
             if ($facet['Type'] == ShopSearch::FACET_TYPE_RANGE) {
                 if (isset($facet['RangeMin'])) {
                     $facet['MinValue'] = $facet['RangeMin'];
                 }
                 if (isset($facet['RangeMax'])) {
                     $facet['MaxValue'] = $facet['RangeMax'];
                 }
             }
             // If the field is accessible via normal methods, including
             // a user-defined getter, prefer that
             $fieldValue = $rec->relObject($field);
             if (is_null($fieldValue) && $rec->hasMethod($meth = "get{$field}")) {
                 $fieldValue = $rec->{$meth}();
             }
             // If not, look for a VFI field
             if (!$fieldValue && $rec->hasExtension('VirtualFieldIndex')) {
                 $fieldValue = $rec->getVFI($field);
             }
             // If we found something, process it
             if (!empty($fieldValue)) {
                 // normalize so that it's iterable
                 if (!is_array($fieldValue) && !$fieldValue instanceof SS_List) {
                     $fieldValue = array($fieldValue);
                 }
                 foreach ($fieldValue as $obj) {
                     if (empty($obj)) {
                         continue;
                     }
                     // figure out the right label
                     if (is_object($obj) && $obj->hasMethod('Nice')) {
                         $lbl = $obj->Nice();
                     } elseif (is_object($obj) && !empty($obj->Title)) {
                         $lbl = $obj->Title;
                     } elseif (is_numeric($obj) && !empty($facet['LabelFormat']) && $facet['LabelFormat'] === 'Currency' && $facet['Type'] !== ShopSearch::FACET_TYPE_RANGE) {
                         $tmp = Currency::create($field);
                         $tmp->setValue($obj);
                         $lbl = $tmp->Nice();
                     } else {
                         $lbl = (string) $obj;
                     }
                     // figure out the value for sorting
                     if (is_object($obj) && $obj->hasMethod('getAmount')) {
                         $val = $obj->getAmount();
                     } elseif (is_object($obj) && !empty($obj->ID)) {
                         $val = $obj->ID;
                     } else {
                         $val = (string) $obj;
                     }
                     // if it's a range facet, calculate the min and max
                     if ($facet['Type'] == ShopSearch::FACET_TYPE_RANGE) {
                         if (!isset($facet['MinValue']) || $val < $facet['MinValue']) {
                             $facet['MinValue'] = $val;
                             $facet['MinLabel'] = $lbl;
                         }
                         if (!isset($facet['RangeMin']) || $val < $facet['RangeMin']) {
                             $facet['RangeMin'] = $val;
                         }
                         if (!isset($facet['MaxValue']) || $val > $facet['MaxValue']) {
                             $facet['MaxValue'] = $val;
                             $facet['MaxLabel'] = $lbl;
                         }
                         if (!isset($facet['RangeMax']) || $val > $facet['RangeMax']) {
                             $facet['RangeMax'] = $val;
                         }
                     }
                     // Tally the value in the facets
                     if (!isset($facet['Values'][$val])) {
                         $facet['Values'][$val] = new ArrayData(array('Label' => $lbl, 'Value' => $val, 'Count' => 1));
                     } elseif ($facet['Values'][$val]) {
                         $facet['Values'][$val]->Count++;
                     }
                 }
             }
         }
     }
     // if we're auto-building the facets based on attributes,
     if ($autoFacetAttributes) {
         $facets = array_merge($this->buildAllAttributeFacets($matches), $facets);
     }
     // convert values to arraylist
     $out = new ArrayList();
     $sortValues = self::config()->sort_facet_values;
     foreach ($facets as $f) {
         if ($sortValues) {
             ksort($f['Values']);
         }
         $f['Values'] = new ArrayList($f['Values']);
         $out->push(new ArrayData($f));
     }
     return $out;
 }
 /**
  * turns full list into paginated list
  * @param SS_List
  * @return PaginatedList
  */
 protected function paginateList(SS_List $list)
 {
     if ($list && $list->count()) {
         if ($this->IsShowFullList()) {
             $obj = PaginatedList::create($list, $this->request);
             $obj->setPageLength(EcommerceConfig::get("ProductGroup", "maximum_number_of_products_to_list") + 1);
             return $obj;
         } else {
             $obj = PaginatedList::create($list, $this->request);
             $obj->setPageLength($this->MyNumberOfProductsPerPage());
             return $obj;
         }
     }
 }
 /**
  * Gets list of safe template variables and their values which can be used
  * in both the static and editable templates.
  *
  * {@see ContentReviewAdminHelp.ss}
  *
  * @param Member     $recipient
  * @param SiteConfig $config
  * @param SS_List    $pages
  * @param string     $type
  *
  * @return array
  */
 protected function getTemplateVariables($recipient = null, $config, $pages)
 {
     if ($recipient != null) {
         return array('Subject' => $config->ReviewSubject, 'PagesCount' => $pages->count(), 'FromEmail' => $config->ReviewFrom, 'ToFirstName' => $recipient->FirstName, 'ToSurname' => $recipient->Surname, 'ToEmail' => $recipient->Email);
     } else {
         return array('Subject' => $config->ReviewSubjectReminder, 'FirstReminderPagesCount' => $pages->count(), 'SecondReminderPagesCount' => $pages->count(), 'FromEmail' => $config->ReviewFrom, 'ToEmail' => $config->ReviewReminderEmail);
     }
 }