PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 682 lines | 575 code | 82 blank | 25 comment | 100 complexity | 987e296862002b9915316dd7eaa2b6ef MD5 | raw file
  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. public function saveValue($value) {
  206. $db = Loader::db();
  207. $this->load();
  208. $options = array();
  209. if (is_array($value) && $this->akSelectAllowMultipleValues) {
  210. foreach($value as $v) {
  211. $opt = SelectAttributeTypeOption::getByValue($v, $this->attributeKey);
  212. if (is_object($opt)) {
  213. $options[] = $opt;
  214. }
  215. }
  216. } else {
  217. if (is_array($value)) {
  218. $value = $value[0];
  219. }
  220. $opt = SelectAttributeTypeOption::getByValue($value, $this->attributeKey);
  221. if (is_object($opt)) {
  222. $options[] = $opt;
  223. }
  224. }
  225. $db->Execute('delete from atSelectOptionsSelected where avID = ?', array($this->getAttributeValueID()));
  226. if (count($options) > 0) {
  227. foreach($options as $opt) {
  228. $db->Execute('insert into atSelectOptionsSelected (avID, atSelectOptionID) values (?, ?)', array($this->getAttributeValueID(), $opt->getSelectAttributeOptionID()));
  229. if ($this->akSelectAllowMultipleValues == false) {
  230. break;
  231. }
  232. }
  233. }
  234. }
  235. public function getDisplayValue() {
  236. $list = $this->getSelectedOptions();
  237. $html = '';
  238. foreach($list as $l) {
  239. $html .= $l . '<br/>';
  240. }
  241. return $html;
  242. }
  243. public function getDisplaySanitizedValue() {
  244. $list = $this->getSelectedOptions();
  245. $html = '';
  246. foreach($list as $l) {
  247. $html .= $l->getSelectAttributeOptionValue() . '<br/>';
  248. }
  249. return $html;
  250. }
  251. public function validateForm($p) {
  252. $this->load();
  253. $options = $this->request('atSelectOptionID');
  254. if ($this->akSelectAllowOtherValues) {
  255. $options = array_filter((Array) $this->request('atSelectNewOption'));
  256. if (is_array($options) && count($options) > 0) {
  257. return true;
  258. } else if (array_shift($this->request('atSelectOptionID')) != null) {
  259. return true;
  260. }
  261. }
  262. if ($this->akSelectAllowMultipleValues) {
  263. return count($options) > 0;
  264. } else {
  265. if ($options[0] != false) {
  266. return $options[0] > 0;
  267. }
  268. }
  269. return false;
  270. }
  271. public function searchForm($list) {
  272. $options = $this->request('atSelectOptionID');
  273. $optionText = array();
  274. $db = Loader::db();
  275. $tbl = $this->attributeKey->getIndexedSearchTable();
  276. if (!is_array($options)) {
  277. return $list;
  278. }
  279. foreach($options as $id) {
  280. if ($id > 0) {
  281. $opt = SelectAttributeTypeOption::getByID($id);
  282. if (is_object($opt)) {
  283. $optionText[] = $opt->getSelectAttributeOptionValue(true);
  284. $optionQuery[] = $opt->getSelectAttributeOptionValue(false);
  285. }
  286. }
  287. }
  288. if (count($optionText) == 0) {
  289. return false;
  290. }
  291. $i = 0;
  292. foreach($optionQuery as $val) {
  293. $val = $db->quote('%||' . $val . '||%');
  294. $multiString .= 'REPLACE(' . $tbl . '.ak_' . $this->attributeKey->getAttributeKeyHandle() . ', "\n", "||") like ' . $val . ' ';
  295. if (($i + 1) < count($optionQuery)) {
  296. $multiString .= 'OR ';
  297. }
  298. $i++;
  299. }
  300. $list->filter(false, '(' . $multiString . ')');
  301. return $list;
  302. }
  303. public function getValue() {
  304. $list = $this->getSelectedOptions();
  305. return $list;
  306. }
  307. public function getSearchIndexValue() {
  308. $str = "\n";
  309. $list = $this->getSelectedOptions();
  310. foreach($list as $l) {
  311. $l = (is_object($l) && method_exists($l,'__toString')) ? $l->__toString() : $l;
  312. $str .= $l . "\n";
  313. }
  314. // remove line break for empty list
  315. if ($str == "\n") {
  316. return '';
  317. }
  318. return $str;
  319. }
  320. public function getSelectedOptions() {
  321. if (!isset($this->akSelectOptionDisplayOrder)) {
  322. $this->load();
  323. }
  324. $db = Loader::db();
  325. switch($this->akSelectOptionDisplayOrder) {
  326. case 'popularity_desc':
  327. $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()));
  328. break;
  329. case 'alpha_asc':
  330. $options = $db->GetAll("select ID, value, displayOrder from atSelectOptionsSelected inner join atSelectOptions on atSelectOptionsSelected.atSelectOptionID = atSelectOptions.ID where avID = ? order by value asc", array($this->getAttributeValueID()));
  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. return $list;
  343. }
  344. public function action_load_autocomplete_values() {
  345. $this->load();
  346. $values = array();
  347. // now, if the current instance of the attribute key allows us to do autocomplete, we return all the values
  348. if ($this->akSelectAllowMultipleValues && $this->akSelectAllowOtherValues) {
  349. $options = $this->getOptions($_GET['term'] . '%');
  350. foreach($options as $opt) {
  351. $values[] = $opt->getSelectAttributeOptionValue(false);
  352. }
  353. }
  354. print Loader::helper('json')->encode($values);
  355. }
  356. public function getOptionUsageArray($parentPage = false, $limit = 9999) {
  357. $db = Loader::db();
  358. $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 = ? ";
  359. $v = array($this->attributeKey->getAttributeKeyID());
  360. if (is_object($parentPage)) {
  361. $v[] = $parentPage->getCollectionID();
  362. $q .= "and cParentID = ?";
  363. }
  364. $q .= " group by atSelectOptionID order by total desc limit " . $limit;
  365. $r = $db->Execute($q, $v);
  366. $list = new SelectAttributeTypeOptionList();
  367. $i = 0;
  368. while ($row = $r->FetchRow()) {
  369. $opt = new SelectAttributeTypeOption($row['atSelectOptionID'], $row['value'], $i, $row['total']);
  370. $list->add($opt);
  371. $i++;
  372. }
  373. return $list;
  374. }
  375. /**
  376. * returns a list of available options optionally filtered by an sql $like statement ex: startswith%
  377. * @param string $like
  378. * @return SelectAttributeTypeOptionList
  379. */
  380. public function getOptions($like = NULL) {
  381. if (!isset($this->akSelectOptionDisplayOrder)) {
  382. $this->load();
  383. }
  384. $db = Loader::db();
  385. switch($this->akSelectOptionDisplayOrder) {
  386. case 'popularity_desc':
  387. if(isset($like) && strlen($like)) {
  388. $r = $db->Execute('select ID, value, displayOrder, count(atSelectOptionsSelected.atSelectOptionID) as total
  389. from atSelectOptions left join atSelectOptionsSelected on (atSelectOptions.ID = atSelectOptionsSelected.atSelectOptionID)
  390. where akID = ? AND atSelectOptions.value LIKE ? group by ID order by total desc, value asc', array($this->attributeKey->getAttributeKeyID(),$like));
  391. } else {
  392. $r = $db->Execute('select ID, value, displayOrder, count(atSelectOptionsSelected.atSelectOptionID) as total
  393. from atSelectOptions left join atSelectOptionsSelected on (atSelectOptions.ID = atSelectOptionsSelected.atSelectOptionID)
  394. where akID = ? group by ID order by total desc, value asc', array($this->attributeKey->getAttributeKeyID()));
  395. }
  396. break;
  397. case 'alpha_asc':
  398. if(isset($like) && strlen($like)) {
  399. $r = $db->Execute('select ID, value, displayOrder from atSelectOptions where akID = ? AND atSelectOptions.value LIKE ? order by value asc', array($this->attributeKey->getAttributeKeyID(),$like));
  400. } else {
  401. $r = $db->Execute('select ID, value, displayOrder from atSelectOptions where akID = ? order by value asc', array($this->attributeKey->getAttributeKeyID()));
  402. }
  403. break;
  404. default:
  405. if(isset($like) && strlen($like)) {
  406. $r = $db->Execute('select ID, value, displayOrder from atSelectOptions where akID = ? AND atSelectOptions.value LIKE ? order by displayOrder asc', array($this->attributeKey->getAttributeKeyID(),$like));
  407. } else {
  408. $r = $db->Execute('select ID, value, displayOrder from atSelectOptions where akID = ? order by displayOrder asc', array($this->attributeKey->getAttributeKeyID()));
  409. }
  410. break;
  411. }
  412. $options = new SelectAttributeTypeOptionList();
  413. while ($row = $r->FetchRow()) {
  414. $opt = new SelectAttributeTypeOption($row['ID'], $row['value'], $row['displayOrder']);
  415. $options->add($opt);
  416. }
  417. return $options;
  418. }
  419. public function saveKey($data) {
  420. $ak = $this->getAttributeKey();
  421. $db = Loader::db();
  422. $initialOptionSet = $this->getOptions();
  423. $selectedPostValues = $this->getSelectValuesFromPost();
  424. $akSelectAllowMultipleValues = $data['akSelectAllowMultipleValues'];
  425. $akSelectAllowOtherValues = $data['akSelectAllowOtherValues'];
  426. $akSelectOptionDisplayOrder = $data['akSelectOptionDisplayOrder'];
  427. if ($data['akSelectAllowMultipleValues'] != 1) {
  428. $akSelectAllowMultipleValues = 0;
  429. }
  430. if ($data['akSelectAllowOtherValues'] != 1) {
  431. $akSelectAllowOtherValues = 0;
  432. }
  433. if (!in_array($data['akSelectOptionDisplayOrder'], array('display_asc', 'alpha_asc', 'popularity_desc'))) {
  434. $akSelectOptionDisplayOrder = 'display_asc';
  435. }
  436. // now we have a collection attribute key object above.
  437. $db->Replace('atSelectSettings', array(
  438. 'akID' => $ak->getAttributeKeyID(),
  439. 'akSelectAllowMultipleValues' => $akSelectAllowMultipleValues,
  440. 'akSelectAllowOtherValues' => $akSelectAllowOtherValues,
  441. 'akSelectOptionDisplayOrder' => $akSelectOptionDisplayOrder
  442. ), array('akID'), true);
  443. // Now we add the options
  444. $newOptionSet = new SelectAttributeTypeOptionList();
  445. $displayOrder = 0;
  446. foreach($selectedPostValues as $option) {
  447. $opt = $option->saveOrCreate($ak);
  448. if ($akSelectOptionDisplayOrder == 'display_asc') {
  449. $opt->setDisplayOrder($displayOrder);
  450. }
  451. $newOptionSet->add($opt);
  452. $displayOrder++;
  453. }
  454. // Now we remove all options that appear in the
  455. // old values list but not in the new
  456. foreach($initialOptionSet as $iopt) {
  457. if (!$newOptionSet->contains($iopt)) {
  458. $iopt->delete();
  459. }
  460. }
  461. }
  462. /**
  463. * Convenience methods to retrieve a select attribute key's settings
  464. */
  465. public function getAllowMultipleValues() {
  466. if (is_null($this->akSelectAllowMultipleValues)) {
  467. $this->load();
  468. }
  469. return $this->akSelectAllowMultipleValues;
  470. }
  471. public function getAllowOtherValues() {
  472. if (is_null($this->akSelectAllowOtherValues)) {
  473. $this->load();
  474. }
  475. return $this->akSelectAllowOtherValues;
  476. }
  477. public function getOptionDisplayOrder() {
  478. if (is_null($this->akSelectOptionDisplayOrder)) {
  479. $this->load();
  480. }
  481. return $this->akSelectOptionDisplayOrder;
  482. }
  483. }
  484. class Concrete5_Model_SelectAttributeTypeOption extends Object {
  485. public function __construct($ID, $value, $displayOrder, $usageCount = false) {
  486. $this->ID = $ID;
  487. $this->value = $value;
  488. $this->th = Loader::helper('text');
  489. $this->displayOrder = $displayOrder;
  490. $this->usageCount = $usageCount;
  491. }
  492. public function getSelectAttributeOptionID() {return $this->ID;}
  493. public function getSelectAttributeOptionUsageCount() {return $this->usageCount;}
  494. public function getSelectAttributeOptionValue($sanitize = true) {
  495. if (!$sanitize) {
  496. return $this->value;
  497. } else {
  498. return $this->th->specialchars($this->value);
  499. }
  500. }
  501. public function getSelectAttributeOptionDisplayOrder() {return $this->displayOrder;}
  502. public function getSelectAttributeOptionTemporaryID() {return $this->tempID;}
  503. public function __toString() {return $this->value;}
  504. public static function add($ak, $option, $isEndUserAdded = 0) {
  505. $db = Loader::db();
  506. $th = Loader::helper('text');
  507. // 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.
  508. $displayOrder = $db->GetOne('select count(ID) from atSelectOptions where akID = ?', array($ak->getAttributeKeyID()));
  509. $v = array($ak->getAttributeKeyID(), $displayOrder, $th->sanitize($option), $isEndUserAdded);
  510. $db->Execute('insert into atSelectOptions (akID, displayOrder, value, isEndUserAdded) values (?, ?, ?, ?)', $v);
  511. return SelectAttributeTypeOption::getByID($db->Insert_ID());
  512. }
  513. public function setDisplayOrder($num) {
  514. $db = Loader::db();
  515. $db->Execute('update atSelectOptions set displayOrder = ? where ID = ?', array($num, $this->ID));
  516. }
  517. public static function getByID($id) {
  518. $db = Loader::db();
  519. $row = $db->GetRow("select ID, displayOrder, value from atSelectOptions where ID = ?", array($id));
  520. if (isset($row['ID'])) {
  521. $obj = new SelectAttributeTypeOption($row['ID'], $row['value'], $row['displayOrder']);
  522. return $obj;
  523. }
  524. }
  525. public static function getByValue($value, $ak = false) {
  526. $db = Loader::db();
  527. if (is_object($ak)) {
  528. $row = $db->GetRow("select ID, displayOrder, value from atSelectOptions where value = ? and akID = ?", array($value, $ak->getAttributeKeyID()));
  529. } else {
  530. $row = $db->GetRow("select ID, displayOrder, value from atSelectOptions where value = ?", array($value));
  531. }
  532. if (isset($row['ID'])) {
  533. $obj = new SelectAttributeTypeOption($row['ID'], $row['value'], $row['displayOrder']);
  534. return $obj;
  535. }
  536. }
  537. public function delete() {
  538. $db = Loader::db();
  539. $db->Execute('delete from atSelectOptions where ID = ?', array($this->ID));
  540. $db->Execute('delete from atSelectOptionsSelected where atSelectOptionID = ?', array($this->ID));
  541. }
  542. public function saveOrCreate($ak) {
  543. if ($this->tempID != false || $this->ID==0) {
  544. return SelectAttributeTypeOption::add($ak, $this->value);
  545. } else {
  546. $db = Loader::db();
  547. $th = Loader::helper('text');
  548. $db->Execute('update atSelectOptions set value = ? where ID = ?', array($th->sanitize($this->value), $this->ID));
  549. return SelectAttributeTypeOption::getByID($this->ID);
  550. }
  551. }
  552. }
  553. class Concrete5_Model_SelectAttributeTypeOptionList extends Object implements Iterator {
  554. private $options = array();
  555. public function add(SelectAttributeTypeOption $opt) {
  556. $this->options[] = $opt;
  557. }
  558. public function rewind() {
  559. reset($this->options);
  560. }
  561. public function current() {
  562. return current($this->options);
  563. }
  564. public function key() {
  565. return key($this->options);
  566. }
  567. public function next() {
  568. next($this->options);
  569. }
  570. public function valid() {
  571. return $this->current() !== false;
  572. }
  573. public function count() {return count($this->options);}
  574. public function contains(SelectAttributeTypeOption $opt) {
  575. foreach($this->options as $o) {
  576. if ($o->getSelectAttributeOptionID() == $opt->getSelectAttributeOptionID()) {
  577. return true;
  578. }
  579. }
  580. return false;
  581. }
  582. public function get($index) {
  583. return $this->options[$index];
  584. }
  585. public function getOptions() {
  586. return $this->options;
  587. }
  588. public function __toString() {
  589. $str = '';
  590. $i = 0;
  591. foreach($this->options as $opt) {
  592. $str .= $opt->getSelectAttributeOptionValue();
  593. $i++;
  594. if ($i < count($this->options)) {
  595. $str .= "\n";
  596. }
  597. }
  598. return $str;
  599. }
  600. }