PageRenderTime 122ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/web/concrete/core/models/attribute/types/select.php

https://github.com/glockops/concrete5
PHP | 714 lines | 592 code | 83 blank | 39 comment | 102 complexity | adae88948690f8bd96168eadb1b3b954 MD5 | raw file
Possible License(s): MIT, LGPL-2.1, BSD-3-Clause
  1. <?php defined('C5_EXECUTE') or die("Access Denied.");
  2. class Concrete5_Controller_AttributeType_Select extends AttributeTypeController {
  3. private $akSelectAllowMultipleValues;
  4. private $akSelectAllowOtherValues;
  5. private $akSelectOptionDisplayOrder;
  6. protected $searchIndexFieldDefinition = 'X NULL';
  7. public function type_form() {
  8. $path1 = $this->getView()->getAttributeTypeURL('type_form.js');
  9. $path2 = $this->getView()->getAttributeTypeURL('type_form.css');
  10. $this->addHeaderItem(Loader::helper('html')->javascript($path1));
  11. $this->addHeaderItem(Loader::helper('html')->css($path2));
  12. $this->set('form', Loader::helper('form'));
  13. $this->load();
  14. //$akSelectValues = $this->getSelectValuesFromPost();
  15. //$this->set('akSelectValues', $akSelectValues);
  16. if ($this->isPost()) {
  17. $akSelectValues = $this->getSelectValuesFromPost();
  18. $this->set('akSelectValues', $akSelectValues);
  19. } else if (isset($this->attributeKey)) {
  20. $options = $this->getOptions();
  21. $this->set('akSelectValues', $options);
  22. } else {
  23. $this->set('akSelectValues', array());
  24. }
  25. }
  26. protected function load() {
  27. $ak = $this->getAttributeKey();
  28. if (!is_object($ak)) {
  29. return false;
  30. }
  31. $db = Loader::db();
  32. $row = $db->GetRow('select akSelectAllowMultipleValues, akSelectOptionDisplayOrder, akSelectAllowOtherValues from atSelectSettings where akID = ?', $ak->getAttributeKeyID());
  33. $this->akSelectAllowMultipleValues = $row['akSelectAllowMultipleValues'];
  34. $this->akSelectAllowOtherValues = $row['akSelectAllowOtherValues'];
  35. $this->akSelectOptionDisplayOrder = $row['akSelectOptionDisplayOrder'];
  36. $this->set('akSelectAllowMultipleValues', $this->akSelectAllowMultipleValues);
  37. $this->set('akSelectAllowOtherValues', $this->akSelectAllowOtherValues);
  38. $this->set('akSelectOptionDisplayOrder', $this->akSelectOptionDisplayOrder);
  39. }
  40. public function duplicateKey($newAK) {
  41. $this->load();
  42. $db = Loader::db();
  43. $db->Execute('insert into atSelectSettings (akID, akSelectAllowMultipleValues, akSelectOptionDisplayOrder, akSelectAllowOtherValues) values (?, ?, ?, ?)', array($newAK->getAttributeKeyID(), $this->akSelectAllowMultipleValues, $this->akSelectOptionDisplayOrder, $this->akSelectAllowOtherValues));
  44. $r = $db->Execute('select value, displayOrder, isEndUserAdded from atSelectOptions where akID = ?', $this->getAttributeKey()->getAttributeKeyID());
  45. while ($row = $r->FetchRow()) {
  46. $db->Execute('insert into atSelectOptions (akID, value, displayOrder, isEndUserAdded) values (?, ?, ?, ?)', array(
  47. $newAK->getAttributeKeyID(),
  48. $row['value'],
  49. $row['displayOrder'],
  50. $row['isEndUserAdded']
  51. ));
  52. }
  53. }
  54. public function exportKey($akey) {
  55. $this->load();
  56. $db = Loader::db();
  57. $type = $akey->addChild('type');
  58. $type->addAttribute('allow-multiple-values', $this->akSelectAllowMultipleValues);
  59. $type->addAttribute('display-order', $this->akSelectOptionDisplayOrder);
  60. $type->addAttribute('allow-other-values', $this->akSelectAllowOtherValues);
  61. $r = $db->Execute('select value, displayOrder, isEndUserAdded from atSelectOptions where akID = ? order by displayOrder asc', $this->getAttributeKey()->getAttributeKeyID());
  62. $options = $type->addChild('options');
  63. while ($row = $r->FetchRow()) {
  64. $opt = $options->addChild('option');
  65. $opt->addAttribute('value', $row['value']);
  66. $opt->addAttribute('is-end-user-added', $row['isEndUserAdded']);
  67. }
  68. return $akey;
  69. }
  70. public function exportValue($akn) {
  71. $list = $this->getSelectedOptions();
  72. if ($list->count() > 0) {
  73. $av = $akn->addChild('value');
  74. foreach($list as $l) {
  75. $av->addChild('option', (string) $l);
  76. }
  77. }
  78. }
  79. public function importValue(SimpleXMLElement $akv) {
  80. if (isset($akv->value)) {
  81. $vals = array();
  82. foreach($akv->value->children() as $ch) {
  83. $vals[] = (string) $ch;
  84. }
  85. return $vals;
  86. }
  87. }
  88. public function importKey($akey) {
  89. if (isset($akey->type)) {
  90. $akSelectAllowMultipleValues = $akey->type['allow-multiple-values'];
  91. $akSelectOptionDisplayOrder = $akey->type['display-order'];
  92. $akSelectAllowOtherValues = $akey->type['allow-other-values'];
  93. $db = Loader::db();
  94. $db->Replace('atSelectSettings', array(
  95. 'akID' => $this->attributeKey->getAttributeKeyID(),
  96. 'akSelectAllowMultipleValues' => $akSelectAllowMultipleValues,
  97. 'akSelectAllowOtherValues' => $akSelectAllowOtherValues,
  98. 'akSelectOptionDisplayOrder' => $akSelectOptionDisplayOrder
  99. ), array('akID'), true);
  100. if (isset($akey->type->options)) {
  101. foreach($akey->type->options->children() as $option) {
  102. SelectAttributeTypeOption::add($this->attributeKey, $option['value'], $option['is-end-user-added']);
  103. }
  104. }
  105. }
  106. }
  107. private function getSelectValuesFromPost() {
  108. $options = new SelectAttributeTypeOptionList();
  109. $displayOrder = 0;
  110. foreach($_POST as $key => $value) {
  111. if( !strstr($key,'akSelectValue_') || $value=='TEMPLATE' ) continue;
  112. $opt = false;
  113. // strip off the prefix to get the ID
  114. $id = substr($key, 14);
  115. // now we determine from the post whether this is a new option
  116. // or an existing. New ones have this value from in the akSelectValueNewOption_ post field
  117. if ($_POST['akSelectValueNewOption_' . $id] == $id) {
  118. $opt = new SelectAttributeTypeOption(0, $value, $displayOrder);
  119. $opt->tempID = $id;
  120. } else if ($_POST['akSelectValueExistingOption_' . $id] == $id) {
  121. $opt = new SelectAttributeTypeOption($id, $value, $displayOrder);
  122. }
  123. if (is_object($opt)) {
  124. $options->add($opt);
  125. $displayOrder++;
  126. }
  127. }
  128. return $options;
  129. }
  130. public function form() {
  131. $this->load();
  132. $options = $this->getSelectedOptions();
  133. $selectedOptions = array();
  134. foreach($options as $opt) {
  135. $selectedOptions[] = $opt->getSelectAttributeOptionID();
  136. $selectedOptionValues[$opt->getSelectAttributeOptionID()] = $opt->getSelectAttributeOptionValue();
  137. }
  138. $this->set('selectedOptionValues',$selectedOptionValues);
  139. $this->set('selectedOptions', $selectedOptions);
  140. $this->addFooterItem(Loader::helper('html')->javascript('jquery.ui.js'));
  141. $this->addHeaderItem(Loader::helper('html')->css('jquery.ui.css'));
  142. }
  143. public function search() {
  144. $this->load();
  145. $selectedOptions = $this->request('atSelectOptionID');
  146. if (!is_array($selectedOptions)) {
  147. $selectedOptions = array();
  148. }
  149. $this->set('selectedOptions', $selectedOptions);
  150. }
  151. public function deleteValue() {
  152. $db = Loader::db();
  153. $db->Execute('delete from atSelectOptionsSelected where avID = ?', array($this->getAttributeValueID()));
  154. }
  155. public function deleteKey() {
  156. $db = Loader::db();
  157. $db->Execute('delete from atSelectSettings where akID = ?', array($this->attributeKey->getAttributeKeyID()));
  158. $r = $db->Execute('select ID from atSelectOptions where akID = ?', array($this->attributeKey->getAttributeKeyID()));
  159. while ($row = $r->FetchRow()) {
  160. $db->Execute('delete from atSelectOptionsSelected where atSelectOptionID = ?', array($row['ID']));
  161. }
  162. $db->Execute('delete from atSelectOptions where akID = ?', array($this->attributeKey->getAttributeKeyID()));
  163. }
  164. public function saveForm($data) {
  165. $this->load();
  166. if ($this->akSelectAllowOtherValues && is_array($data['atSelectNewOption'])) {
  167. $options = $this->getOptions();
  168. foreach($data['atSelectNewOption'] as $newoption) {
  169. // check for duplicates
  170. $existing = false;
  171. foreach($options as $opt) {
  172. if(strtolower(trim($newoption)) == strtolower(trim($opt->getSelectAttributeOptionValue(false)))) {
  173. $existing = $opt;
  174. break;
  175. }
  176. }
  177. if($existing instanceof SelectAttributeTypeOption) {
  178. $data['atSelectOptionID'][] = $existing->getSelectAttributeOptionID();
  179. } else {
  180. $optobj = SelectAttributeTypeOption::add($this->attributeKey, $newoption, 1);
  181. $data['atSelectOptionID'][] = $optobj->getSelectAttributeOptionID();
  182. }
  183. }
  184. }
  185. if(is_array($data['atSelectOptionID'])) {
  186. $data['atSelectOptionID'] = array_unique($data['atSelectOptionID']);
  187. }
  188. $db = Loader::db();
  189. $db->Execute('delete from atSelectOptionsSelected where avID = ?', array($this->getAttributeValueID()));
  190. if (is_array($data['atSelectOptionID'])) {
  191. foreach($data['atSelectOptionID'] as $optID) {
  192. if ($optID > 0) {
  193. $db->Execute('insert into atSelectOptionsSelected (avID, atSelectOptionID) values (?, ?)', array($this->getAttributeValueID(), $optID));
  194. if ($this->akSelectAllowMultipleValues == false) {
  195. break;
  196. }
  197. }
  198. }
  199. }
  200. }
  201. // Sets select options for a particular attribute
  202. // If the $value == string, then 1 item is selected
  203. // if array, then multiple, but only if the attribute in question is a select multiple
  204. // Note, items CANNOT be added to the pool (even if the attribute allows it) through this process.
  205. // Items should now be added to the database if they don't exist already & if the allow checkbox is checked under the attribute settings
  206. // Code from this bug - http://www.concrete5.org/index.php?cID=595692
  207. public function saveValue($value) {
  208. $db = Loader::db();
  209. $this->load();
  210. $options = array();
  211. if (is_array($value) && $this->akSelectAllowMultipleValues) {
  212. foreach($value as $v) {
  213. $opt = SelectAttributeTypeOption::getByValue($v, $this->attributeKey);
  214. if (is_object($opt)) {
  215. $options[] = $opt;
  216. }else if ($this->akSelectAllowOtherValues) {
  217. $options[] = SelectAttributeTypeOption::add($this->attributeKey, $v, true);
  218. }
  219. }
  220. } else {
  221. if (is_array($value)) {
  222. $value = $value[0];
  223. }
  224. $opt = SelectAttributeTypeOption::getByValue($value, $this->attributeKey);
  225. if (is_object($opt)) {
  226. $options[] = $opt;
  227. }
  228. }
  229. $db->Execute('delete from atSelectOptionsSelected where avID = ?', array($this->getAttributeValueID()));
  230. if (count($options) > 0) {
  231. foreach($options as $opt) {
  232. $db->Execute('insert into atSelectOptionsSelected (avID, atSelectOptionID) values (?, ?)', array($this->getAttributeValueID(), $opt->getSelectAttributeOptionID()));
  233. if ($this->akSelectAllowMultipleValues == false) {
  234. break;
  235. }
  236. }
  237. }
  238. }
  239. public function getDisplayValue() {
  240. $list = $this->getSelectedOptions();
  241. $html = '';
  242. foreach($list as $l) {
  243. $html .= $l->getSelectAttributeOptionDisplayValue() . '<br/>';
  244. }
  245. return $html;
  246. }
  247. public function getDisplaySanitizedValue() {
  248. return $this->getDisplayValue();
  249. }
  250. public function validateForm($p) {
  251. $this->load();
  252. $options = $this->request('atSelectOptionID');
  253. if ($this->akSelectAllowOtherValues) {
  254. $options = array_filter((Array) $this->request('atSelectNewOption'));
  255. if (is_array($options) && count($options) > 0) {
  256. return true;
  257. } else if (array_shift($this->request('atSelectOptionID')) != null) {
  258. return true;
  259. }
  260. }
  261. if ($this->akSelectAllowMultipleValues) {
  262. return count($options) > 0;
  263. } else {
  264. if ($options[0] != false) {
  265. return $options[0] > 0;
  266. }
  267. }
  268. return false;
  269. }
  270. public function searchForm($list) {
  271. $options = $this->request('atSelectOptionID');
  272. $db = Loader::db();
  273. $tbl = $this->attributeKey->getIndexedSearchTable();
  274. if (!is_array($options)) {
  275. return $list;
  276. }
  277. $optionQuery = array();
  278. foreach($options as $id) {
  279. if ($id > 0) {
  280. $opt = SelectAttributeTypeOption::getByID($id);
  281. if (is_object($opt)) {
  282. $optionQuery[] = $opt->getSelectAttributeOptionValue(false);
  283. }
  284. }
  285. }
  286. if (count($optionQuery) == 0) {
  287. return false;
  288. }
  289. $i = 0;
  290. foreach($optionQuery as $val) {
  291. $val = $db->quote('%||' . $val . '||%');
  292. $multiString .= 'REPLACE(' . $tbl . '.ak_' . $this->attributeKey->getAttributeKeyHandle() . ', "\n", "||") like ' . $val . ' ';
  293. if (($i + 1) < count($optionQuery)) {
  294. $multiString .= 'OR ';
  295. }
  296. $i++;
  297. }
  298. $list->filter(false, '(' . $multiString . ')');
  299. return $list;
  300. }
  301. public function getValue() {
  302. $list = $this->getSelectedOptions();
  303. return $list;
  304. }
  305. public function getSearchIndexValue() {
  306. $str = "\n";
  307. $list = $this->getSelectedOptions();
  308. foreach($list as $l) {
  309. $l = (is_object($l) && method_exists($l,'__toString')) ? $l->__toString() : $l;
  310. $str .= $l . "\n";
  311. }
  312. // remove line break for empty list
  313. if ($str == "\n") {
  314. return '';
  315. }
  316. return $str;
  317. }
  318. public function getSelectedOptions() {
  319. if (!isset($this->akSelectOptionDisplayOrder)) {
  320. $this->load();
  321. }
  322. $db = Loader::db();
  323. $sortByDisplayName = false;
  324. switch($this->akSelectOptionDisplayOrder) {
  325. case 'popularity_desc':
  326. $options = $db->GetAll("select ID, value, displayOrder, (select count(s2.atSelectOptionID) from atSelectOptionsSelected s2 where s2.atSelectOptionID = ID) as total from atSelectOptionsSelected inner join atSelectOptions on atSelectOptionsSelected.atSelectOptionID = atSelectOptions.ID where avID = ? order by total desc, value asc", array($this->getAttributeValueID()));
  327. break;
  328. case 'alpha_asc':
  329. $options = $db->GetAll("select ID, value, displayOrder from atSelectOptionsSelected inner join atSelectOptions on atSelectOptionsSelected.atSelectOptionID = atSelectOptions.ID where avID = ?", array($this->getAttributeValueID()));
  330. $sortByDisplayName = true;
  331. break;
  332. default:
  333. $options = $db->GetAll("select ID, value, displayOrder from atSelectOptionsSelected inner join atSelectOptions on atSelectOptionsSelected.atSelectOptionID = atSelectOptions.ID where avID = ? order by displayOrder asc", array($this->getAttributeValueID()));
  334. break;
  335. }
  336. $db = Loader::db();
  337. $list = new SelectAttributeTypeOptionList();
  338. foreach($options as $row) {
  339. $opt = new SelectAttributeTypeOption($row['ID'], $row['value'], $row['displayOrder']);
  340. $list->add($opt);
  341. }
  342. if($sortByDisplayName) {
  343. $list->sortByDisplayName();
  344. }
  345. return $list;
  346. }
  347. public function action_load_autocomplete_values() {
  348. $this->load();
  349. $values = array();
  350. // now, if the current instance of the attribute key allows us to do autocomplete, we return all the values
  351. if ($this->akSelectAllowMultipleValues && $this->akSelectAllowOtherValues) {
  352. $options = $this->getOptions($_GET['term'] . '%');
  353. foreach($options as $opt) {
  354. $values[] = $opt->getSelectAttributeOptionValue(false);
  355. }
  356. }
  357. print Loader::helper('json')->encode($values);
  358. }
  359. public function getOptionUsageArray($parentPage = false, $limit = 9999) {
  360. $db = Loader::db();
  361. $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 where Pages.cIsActive = 1 and CollectionAttributeValues.akID = ? ";
  362. $v = array($this->attributeKey->getAttributeKeyID());
  363. if (is_object($parentPage)) {
  364. $v[] = $parentPage->getCollectionID();
  365. $q .= "and cParentID = ?";
  366. }
  367. $q .= " group by atSelectOptionID order by total desc limit " . $limit;
  368. $r = $db->Execute($q, $v);
  369. $list = new SelectAttributeTypeOptionList();
  370. $i = 0;
  371. while ($row = $r->FetchRow()) {
  372. $opt = new SelectAttributeTypeOption($row['atSelectOptionID'], $row['value'], $i, $row['total']);
  373. $list->add($opt);
  374. $i++;
  375. }
  376. return $list;
  377. }
  378. /**
  379. * returns a list of available options optionally filtered by an sql $like statement ex: startswith%
  380. * @param string $like
  381. * @return SelectAttributeTypeOptionList
  382. */
  383. public function getOptions($like = NULL) {
  384. if (!isset($this->akSelectOptionDisplayOrder)) {
  385. $this->load();
  386. }
  387. $db = Loader::db();
  388. switch($this->akSelectOptionDisplayOrder) {
  389. case 'popularity_desc':
  390. if(isset($like) && strlen($like)) {
  391. $r = $db->Execute('select ID, value, displayOrder, count(atSelectOptionsSelected.atSelectOptionID) as total
  392. from atSelectOptions left join atSelectOptionsSelected on (atSelectOptions.ID = atSelectOptionsSelected.atSelectOptionID)
  393. where akID = ? AND atSelectOptions.value LIKE ? group by ID order by total desc, value asc', array($this->attributeKey->getAttributeKeyID(),$like));
  394. } else {
  395. $r = $db->Execute('select ID, value, displayOrder, count(atSelectOptionsSelected.atSelectOptionID) as total
  396. from atSelectOptions left join atSelectOptionsSelected on (atSelectOptions.ID = atSelectOptionsSelected.atSelectOptionID)
  397. where akID = ? group by ID order by total desc, value asc', array($this->attributeKey->getAttributeKeyID()));
  398. }
  399. break;
  400. case 'alpha_asc':
  401. if(isset($like) && strlen($like)) {
  402. $r = $db->Execute('select ID, value, displayOrder from atSelectOptions where akID = ? AND atSelectOptions.value LIKE ? order by value asc', array($this->attributeKey->getAttributeKeyID(),$like));
  403. } else {
  404. $r = $db->Execute('select ID, value, displayOrder from atSelectOptions where akID = ? order by value asc', array($this->attributeKey->getAttributeKeyID()));
  405. }
  406. break;
  407. default:
  408. if(isset($like) && strlen($like)) {
  409. $r = $db->Execute('select ID, value, displayOrder from atSelectOptions where akID = ? AND atSelectOptions.value LIKE ? order by displayOrder asc', array($this->attributeKey->getAttributeKeyID(),$like));
  410. } else {
  411. $r = $db->Execute('select ID, value, displayOrder from atSelectOptions where akID = ? order by displayOrder asc', array($this->attributeKey->getAttributeKeyID()));
  412. }
  413. break;
  414. }
  415. $options = new SelectAttributeTypeOptionList();
  416. while ($row = $r->FetchRow()) {
  417. $opt = new SelectAttributeTypeOption($row['ID'], $row['value'], $row['displayOrder']);
  418. $options->add($opt);
  419. }
  420. return $options;
  421. }
  422. public function saveKey($data) {
  423. $ak = $this->getAttributeKey();
  424. $db = Loader::db();
  425. $initialOptionSet = $this->getOptions();
  426. $selectedPostValues = $this->getSelectValuesFromPost();
  427. $akSelectAllowMultipleValues = $data['akSelectAllowMultipleValues'];
  428. $akSelectAllowOtherValues = $data['akSelectAllowOtherValues'];
  429. $akSelectOptionDisplayOrder = $data['akSelectOptionDisplayOrder'];
  430. if ($data['akSelectAllowMultipleValues'] != 1) {
  431. $akSelectAllowMultipleValues = 0;
  432. }
  433. if ($data['akSelectAllowOtherValues'] != 1) {
  434. $akSelectAllowOtherValues = 0;
  435. }
  436. if (!in_array($data['akSelectOptionDisplayOrder'], array('display_asc', 'alpha_asc', 'popularity_desc'))) {
  437. $akSelectOptionDisplayOrder = 'display_asc';
  438. }
  439. // now we have a collection attribute key object above.
  440. $db->Replace('atSelectSettings', array(
  441. 'akID' => $ak->getAttributeKeyID(),
  442. 'akSelectAllowMultipleValues' => $akSelectAllowMultipleValues,
  443. 'akSelectAllowOtherValues' => $akSelectAllowOtherValues,
  444. 'akSelectOptionDisplayOrder' => $akSelectOptionDisplayOrder
  445. ), array('akID'), true);
  446. // Now we add the options
  447. $newOptionSet = new SelectAttributeTypeOptionList();
  448. $displayOrder = 0;
  449. foreach($selectedPostValues as $option) {
  450. $opt = $option->saveOrCreate($ak);
  451. if ($akSelectOptionDisplayOrder == 'display_asc') {
  452. $opt->setDisplayOrder($displayOrder);
  453. }
  454. $newOptionSet->add($opt);
  455. $displayOrder++;
  456. }
  457. // Now we remove all options that appear in the
  458. // old values list but not in the new
  459. foreach($initialOptionSet as $iopt) {
  460. if (!$newOptionSet->contains($iopt)) {
  461. $iopt->delete();
  462. }
  463. }
  464. }
  465. /**
  466. * Convenience methods to retrieve a select attribute key's settings
  467. */
  468. public function getAllowMultipleValues() {
  469. if (is_null($this->akSelectAllowMultipleValues)) {
  470. $this->load();
  471. }
  472. return $this->akSelectAllowMultipleValues;
  473. }
  474. public function getAllowOtherValues() {
  475. if (is_null($this->akSelectAllowOtherValues)) {
  476. $this->load();
  477. }
  478. return $this->akSelectAllowOtherValues;
  479. }
  480. public function getOptionDisplayOrder() {
  481. if (is_null($this->akSelectOptionDisplayOrder)) {
  482. $this->load();
  483. }
  484. return $this->akSelectOptionDisplayOrder;
  485. }
  486. }
  487. class Concrete5_Model_SelectAttributeTypeOption extends Object {
  488. public function __construct($ID, $value, $displayOrder, $usageCount = false) {
  489. $this->ID = $ID;
  490. $this->value = $value;
  491. $this->th = Loader::helper('text');
  492. $this->displayOrder = $displayOrder;
  493. $this->usageCount = $usageCount;
  494. }
  495. public function getSelectAttributeOptionID() {return $this->ID;}
  496. public function getSelectAttributeOptionUsageCount() {return $this->usageCount;}
  497. public function getSelectAttributeOptionValue($sanitize = true) {
  498. if (!$sanitize) {
  499. return $this->value;
  500. } else {
  501. return $this->th->specialchars($this->value);
  502. }
  503. }
  504. /** Returns the display name for this select option value (localized and escaped accordingly to $format)
  505. * @param string $format = 'html'
  506. * Escape the result in html format (if $format is 'html').
  507. * If $format is 'text' or any other value, the display name won't be escaped.
  508. * @return string
  509. */
  510. public function getSelectAttributeOptionDisplayValue($format = 'html') {
  511. $value = tc('SelectAttributeValue', $this->getSelectAttributeOptionValue(false));
  512. switch($format) {
  513. case 'html':
  514. return h($value);
  515. case 'text':
  516. default:
  517. return $value;
  518. }
  519. }
  520. public function getSelectAttributeOptionDisplayOrder() {return $this->displayOrder;}
  521. public function getSelectAttributeOptionTemporaryID() {return $this->tempID;}
  522. public function __toString() {return $this->value;}
  523. public static function add($ak, $option, $isEndUserAdded = 0) {
  524. $db = Loader::db();
  525. $th = Loader::helper('text');
  526. // this works because displayorder starts at zero. So if there are three items, for example, the display order of the NEXT item will be 3.
  527. $displayOrder = $db->GetOne('select count(ID) from atSelectOptions where akID = ?', array($ak->getAttributeKeyID()));
  528. $v = array($ak->getAttributeKeyID(), $displayOrder, $th->sanitize($option), $isEndUserAdded);
  529. $db->Execute('insert into atSelectOptions (akID, displayOrder, value, isEndUserAdded) values (?, ?, ?, ?)', $v);
  530. return SelectAttributeTypeOption::getByID($db->Insert_ID());
  531. }
  532. public function setDisplayOrder($num) {
  533. $db = Loader::db();
  534. $db->Execute('update atSelectOptions set displayOrder = ? where ID = ?', array($num, $this->ID));
  535. }
  536. public static function getByID($id) {
  537. $db = Loader::db();
  538. $row = $db->GetRow("select ID, displayOrder, value from atSelectOptions where ID = ?", array($id));
  539. if (isset($row['ID'])) {
  540. $obj = new SelectAttributeTypeOption($row['ID'], $row['value'], $row['displayOrder']);
  541. return $obj;
  542. }
  543. }
  544. public static function getByValue($value, $ak = false) {
  545. $db = Loader::db();
  546. if (is_object($ak)) {
  547. $row = $db->GetRow("select ID, displayOrder, value from atSelectOptions where value = ? and akID = ?", array($value, $ak->getAttributeKeyID()));
  548. } else {
  549. $row = $db->GetRow("select ID, displayOrder, value from atSelectOptions where value = ?", array($value));
  550. }
  551. if (isset($row['ID'])) {
  552. $obj = new SelectAttributeTypeOption($row['ID'], $row['value'], $row['displayOrder']);
  553. return $obj;
  554. }
  555. }
  556. public function delete() {
  557. $db = Loader::db();
  558. $db->Execute('delete from atSelectOptions where ID = ?', array($this->ID));
  559. $db->Execute('delete from atSelectOptionsSelected where atSelectOptionID = ?', array($this->ID));
  560. }
  561. public function saveOrCreate($ak) {
  562. if ($this->tempID != false || $this->ID==0) {
  563. return SelectAttributeTypeOption::add($ak, $this->value);
  564. } else {
  565. $db = Loader::db();
  566. $th = Loader::helper('text');
  567. $db->Execute('update atSelectOptions set value = ? where ID = ?', array($th->sanitize($this->value), $this->ID));
  568. return SelectAttributeTypeOption::getByID($this->ID);
  569. }
  570. }
  571. }
  572. class Concrete5_Model_SelectAttributeTypeOptionList extends Object implements Iterator {
  573. private $options = array();
  574. public function add(SelectAttributeTypeOption $opt) {
  575. $this->options[] = $opt;
  576. }
  577. public function rewind() {
  578. reset($this->options);
  579. }
  580. public function current() {
  581. return current($this->options);
  582. }
  583. public function key() {
  584. return key($this->options);
  585. }
  586. public function next() {
  587. next($this->options);
  588. }
  589. public function valid() {
  590. return $this->current() !== false;
  591. }
  592. public function count() {return count($this->options);}
  593. public function contains(SelectAttributeTypeOption $opt) {
  594. foreach($this->options as $o) {
  595. if ($o->getSelectAttributeOptionID() == $opt->getSelectAttributeOptionID()) {
  596. return true;
  597. }
  598. }
  599. return false;
  600. }
  601. public function get($index) {
  602. return $this->options[$index];
  603. }
  604. public function getOptions() {
  605. return $this->options;
  606. }
  607. /** Sort the options by their display value. */
  608. public function sortByDisplayName() {
  609. usort($this->options, array(__CLASS__, 'displayValueSorter'));
  610. }
  611. /**
  612. * @param SelectAttributeTypeOption $a
  613. * @param SelectAttributeTypeOption $b
  614. * @return int
  615. */
  616. protected static function displayValueSorter($a, $b) {
  617. return strcasecmp($a->getSelectAttributeOptionDisplayValue('text'), $b->getSelectAttributeOptionDisplayValue('text'));
  618. }
  619. public function __toString() {
  620. $str = '';
  621. $i = 0;
  622. foreach($this->options as $opt) {
  623. $str .= $opt->getSelectAttributeOptionValue();
  624. $i++;
  625. if ($i < count($this->options)) {
  626. $str .= "\n";
  627. }
  628. }
  629. return $str;
  630. }
  631. }