public function getOptionUsageArray($parentPage = false, $limit = 9999) { $db = Loader::db(); $q = "select atSelectOptions.value, atSelectOptionID, count(atSelectOptionID) as total from Pages inner join CollectionVersions on (Pages.cID = CollectionVersions.cID and CollectionVersions.cvIsApproved = 1) inner join CollectionAttributeValues on (CollectionVersions.cID = CollectionAttributeValues.cID and CollectionVersions.cvID = CollectionAttributeValues.cvID) inner join atSelectOptionsSelected on (atSelectOptionsSelected.avID = CollectionAttributeValues.avID) inner join atSelectOptions on atSelectOptionsSelected.atSelectOptionID = atSelectOptions.ID"; if (is_object($parentPage)) { $q .= " inner join PagePaths on Pages.cID=PagePaths.cID"; } $q .= " where Pages.cIsActive = 1 and CollectionAttributeValues.akID = ? "; $v = array($this->attributeKey->getAttributeKeyID()); if (is_object($parentPage)) { $path = $parentPage->getCollectionPath(); $q .= "and PagePaths.cPath LIKE '{$path}%'"; } $q .= " group by atSelectOptionID order by total desc limit " . $limit; $r = $db->Execute($q, $v); $list = new OptionList(); $i = 0; while ($row = $r->FetchRow()) { $opt = new Option($row['atSelectOptionID'], $row['value'], $i, $row['total']); $list->add($opt); $i++; } return $list; }
public function saveKey($data) { $ak = $this->getAttributeKey(); $db = Database::get(); $initialOptionSet = $this->getOptions(); $selectedPostValues = $this->getSelectValuesFromPost(); if (isset($data['akSelectAllowMultipleValues']) && $data['akSelectAllowMultipleValues'] == 1) { $akSelectAllowMultipleValues = 1; } else { $akSelectAllowMultipleValues = 0; } if (isset($data['akSelectAllowOtherValues']) && $data['akSelectAllowOtherValues'] == 1) { $akSelectAllowOtherValues = 1; } else { $akSelectAllowOtherValues = 0; } if (isset($data['akSelectOptionDisplayOrder']) && in_array($data['akSelectOptionDisplayOrder'], array('display_asc', 'alpha_asc', 'popularity_desc'))) { $akSelectOptionDisplayOrder = $data['akSelectOptionDisplayOrder']; } else { $akSelectOptionDisplayOrder = 'display_asc'; } // now we have a collection attribute key object above. $db->Replace('atSelectSettings', array('akID' => $ak->getAttributeKeyID(), 'akSelectAllowMultipleValues' => $akSelectAllowMultipleValues, 'akSelectAllowOtherValues' => $akSelectAllowOtherValues, 'akSelectOptionDisplayOrder' => $akSelectOptionDisplayOrder), array('akID'), true); // Now we add the options $newOptionSet = new OptionList(); $displayOrder = 0; foreach ($selectedPostValues as $option) { $opt = $option->saveOrCreate($ak); if ($akSelectOptionDisplayOrder == 'display_asc') { $opt->setDisplayOrder($displayOrder); } $newOptionSet->add($opt); $displayOrder++; } // Now we remove all options that appear in the // old values list but not in the new foreach ($initialOptionSet as $iopt) { if (!$newOptionSet->contains($iopt)) { $iopt->delete(); } } }