PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/symphony/lib/toolkit/fields/field.select.php

https://github.com/scottkf/nicholscommunications
PHP | 401 lines | 281 code | 117 blank | 3 comment | 57 complexity | 9d25d6a8d6a332106c3a649ffd55fb3b MD5 | raw file
  1. <?php
  2. Class fieldSelect extends Field {
  3. function __construct(&$parent){
  4. parent::__construct($parent);
  5. $this->_name = __('Select Box');
  6. // Set default
  7. $this->set('show_column', 'no');
  8. }
  9. function canToggle(){
  10. return ($this->get('allow_multiple_selection') == 'yes' ? false : true);
  11. }
  12. function allowDatasourceOutputGrouping(){
  13. ## Grouping follows the same rule as toggling.
  14. return $this->canToggle();
  15. }
  16. function allowDatasourceParamOutput(){
  17. return true;
  18. }
  19. function canFilter(){
  20. return true;
  21. }
  22. public function canImport(){
  23. return true;
  24. }
  25. function canPrePopulate(){
  26. return true;
  27. }
  28. function isSortable(){
  29. return true;
  30. }
  31. public function appendFormattedElement(&$wrapper, $data, $encode = false) {
  32. if (!is_array($data) or empty($data)) return;
  33. $list = new XMLElement($this->get('element_name'));
  34. if (!is_array($data['handle']) and !is_array($data['value'])) {
  35. $data = array(
  36. 'handle' => array($data['handle']),
  37. 'value' => array($data['value'])
  38. );
  39. }
  40. foreach ($data['value'] as $index => $value) {
  41. $list->appendChild(new XMLElement(
  42. 'item',
  43. General::sanitize($value),
  44. array(
  45. 'handle' => $data['handle'][$index]
  46. )
  47. ));
  48. }
  49. $wrapper->appendChild($list);
  50. }
  51. function fetchAssociatedEntrySearchValue($data){
  52. if(!is_array($data)) return $data;
  53. return $data['value'];
  54. }
  55. function fetchAssociatedEntryCount($value){
  56. return Symphony::Database()->fetchVar('count', 0, "SELECT count(*) AS `count` FROM `tbl_entries_data_".$this->get('id')."` WHERE `value` = '".Symphony::Database()->cleanValue($value)."'");
  57. }
  58. function fetchAssociatedEntryIDs($value){
  59. return Symphony::Database()->fetchCol('entry_id', "SELECT `entry_id` FROM `tbl_entries_data_".$this->get('id')."` WHERE `value` = '".Symphony::Database()->cleanValue($value)."'");
  60. }
  61. public function getParameterPoolValue($data){
  62. return $data['handle'];
  63. }
  64. public function getToggleStates() {
  65. $values = preg_split('/,\s*/i', $this->get('static_options'), -1, PREG_SPLIT_NO_EMPTY);
  66. if ($this->get('dynamic_options') != '') $this->findAndAddDynamicOptions($values);
  67. $values = array_map('trim', $values);
  68. $states = array();
  69. foreach ($values as $value) {
  70. $value = $value;
  71. $states[$value] = $value;
  72. }
  73. return $states;
  74. }
  75. function toggleFieldData($data, $newState){
  76. $data['value'] = $newState;
  77. $data['handle'] = Lang::createHandle($newState);
  78. return $data;
  79. }
  80. function displayPublishPanel(&$wrapper, $data=NULL, $flagWithError=NULL, $fieldnamePrefix=NULL, $fieldnamePostfix=NULL){
  81. $states = $this->getToggleStates();
  82. natsort($states);
  83. if(!is_array($data['value'])) $data['value'] = array($data['value']);
  84. $options = array();
  85. foreach($states as $handle => $v){
  86. $options[] = array(General::sanitize($v), in_array($v, $data['value']), $v);
  87. }
  88. $fieldname = 'fields'.$fieldnamePrefix.'['.$this->get('element_name').']'.$fieldnamePostfix;
  89. if($this->get('allow_multiple_selection') == 'yes') $fieldname .= '[]';
  90. $label = Widget::Label($this->get('label'));
  91. $label->appendChild(Widget::Select($fieldname, $options, ($this->get('allow_multiple_selection') == 'yes' ? array('multiple' => 'multiple') : NULL)));
  92. if($flagWithError != NULL) $wrapper->appendChild(Widget::wrapFormElementWithError($label, $flagWithError));
  93. else $wrapper->appendChild($label);
  94. }
  95. function displayDatasourceFilterPanel(&$wrapper, $data=NULL, $errors=NULL, $fieldnamePrefix=NULL, $fieldnamePostfix=NULL){
  96. parent::displayDatasourceFilterPanel($wrapper, $data, $errors, $fieldnamePrefix, $fieldnamePostfix);
  97. $data = preg_split('/,\s*/i', $data);
  98. $data = array_map('trim', $data);
  99. $existing_options = $this->getToggleStates();
  100. if(is_array($existing_options) && !empty($existing_options)){
  101. $optionlist = new XMLElement('ul');
  102. $optionlist->setAttribute('class', 'tags');
  103. foreach($existing_options as $option) $optionlist->appendChild(new XMLElement('li', $option));
  104. $wrapper->appendChild($optionlist);
  105. }
  106. }
  107. function findAndAddDynamicOptions(&$values){
  108. if(!is_array($values)) $values = array();
  109. $sql = "SELECT DISTINCT `value` FROM `tbl_entries_data_".$this->get('dynamic_options')."`
  110. ORDER BY `value` DESC";
  111. if($results = $this->Database->fetchCol('value', $sql)) $values = array_merge($values, $results);
  112. }
  113. function prepareTableValue($data, XMLElement $link=NULL){
  114. $value = $data['value'];
  115. if(!is_array($value)) $value = array($value);
  116. return parent::prepareTableValue(array('value' => @implode(', ', $value)), $link);
  117. }
  118. public function processRawFieldData($data, &$status, $simulate=false, $entry_id=NULL){
  119. $status = self::__OK__;
  120. if(!is_array($data)) return array('value' => $data, 'handle' => Lang::createHandle($data));
  121. if(empty($data)) return NULL;
  122. $result = array('value' => array(), 'handle' => array());
  123. foreach($data as $value){
  124. $result['value'][] = $value;
  125. $result['handle'][] = Lang::createHandle($value);
  126. }
  127. return $result;
  128. }
  129. public function buildDSRetrivalSQL($data, &$joins, &$where, $andOperation = false) {
  130. $field_id = $this->get('id');
  131. if (self::isFilterRegex($data[0])) {
  132. $this->_key++;
  133. $pattern = str_replace('regexp:', '', $this->cleanValue($data[0]));
  134. $joins .= "
  135. LEFT JOIN
  136. `tbl_entries_data_{$field_id}` AS t{$field_id}_{$this->_key}
  137. ON (e.id = t{$field_id}_{$this->_key}.entry_id)
  138. ";
  139. $where .= "
  140. AND (
  141. t{$field_id}_{$this->_key}.value REGEXP '{$pattern}'
  142. OR t{$field_id}_{$this->_key}.handle REGEXP '{$pattern}'
  143. )
  144. ";
  145. } elseif ($andOperation) {
  146. foreach ($data as $value) {
  147. $this->_key++;
  148. $value = $this->cleanValue($value);
  149. $joins .= "
  150. LEFT JOIN
  151. `tbl_entries_data_{$field_id}` AS t{$field_id}_{$this->_key}
  152. ON (e.id = t{$field_id}_{$this->_key}.entry_id)
  153. ";
  154. $where .= "
  155. AND (
  156. t{$field_id}_{$this->_key}.value = '{$value}'
  157. OR t{$field_id}_{$this->_key}.handle = '{$value}'
  158. )
  159. ";
  160. }
  161. } else {
  162. if (!is_array($data)) $data = array($data);
  163. foreach ($data as &$value) {
  164. $value = $this->cleanValue($value);
  165. }
  166. $this->_key++;
  167. $data = implode("', '", $data);
  168. $joins .= "
  169. LEFT JOIN
  170. `tbl_entries_data_{$field_id}` AS t{$field_id}_{$this->_key}
  171. ON (e.id = t{$field_id}_{$this->_key}.entry_id)
  172. ";
  173. $where .= "
  174. AND (
  175. t{$field_id}_{$this->_key}.value IN ('{$data}')
  176. OR t{$field_id}_{$this->_key}.handle IN ('{$data}')
  177. )
  178. ";
  179. }
  180. return true;
  181. }
  182. function commit(){
  183. if(!parent::commit()) return false;
  184. $id = $this->get('id');
  185. if($id === false) return false;
  186. $fields = array();
  187. $fields['field_id'] = $id;
  188. if($this->get('static_options') != '') $fields['static_options'] = $this->get('static_options');
  189. if($this->get('dynamic_options') != '') $fields['dynamic_options'] = $this->get('dynamic_options');
  190. $fields['allow_multiple_selection'] = ($this->get('allow_multiple_selection') ? $this->get('allow_multiple_selection') : 'no');
  191. $this->Database->query("DELETE FROM `tbl_fields_".$this->handle()."` WHERE `field_id` = '$id' LIMIT 1");
  192. if(!$this->Database->insert($fields, 'tbl_fields_' . $this->handle())) return false;
  193. $this->removeSectionAssociation($id);
  194. $this->createSectionAssociation(NULL, $id, $this->get('dynamic_options'));
  195. return true;
  196. }
  197. function checkFields(&$errors, $checkForDuplicates=true){
  198. if(!is_array($errors)) $errors = array();
  199. if($this->get('static_options') == '' && ($this->get('dynamic_options') == '' || $this->get('dynamic_options') == 'none'))
  200. $errors['dynamic_options'] = __('At least one source must be specified, dynamic or static.');
  201. parent::checkFields($errors, $checkForDuplicates);
  202. }
  203. function findDefaults(&$fields){
  204. if(!isset($fields['allow_multiple_selection'])) $fields['allow_multiple_selection'] = 'no';
  205. }
  206. public function displaySettingsPanel(&$wrapper, $errors = null) {
  207. parent::displaySettingsPanel($wrapper, $errors);
  208. $div = new XMLElement('div', NULL, array('class' => 'group'));
  209. $label = Widget::Label(__('Static Options'));
  210. $label->appendChild(new XMLElement('i', __('Optional')));
  211. $input = Widget::Input('fields['.$this->get('sortorder').'][static_options]', General::sanitize($this->get('static_options')));
  212. $label->appendChild($input);
  213. $div->appendChild($label);
  214. $label = Widget::Label(__('Dynamic Options'));
  215. $sectionManager = new SectionManager($this->_engine);
  216. $sections = $sectionManager->fetch(NULL, 'ASC', 'name');
  217. $field_groups = array();
  218. if(is_array($sections) && !empty($sections))
  219. foreach($sections as $section) $field_groups[$section->get('id')] = array('fields' => $section->fetchFields(), 'section' => $section);
  220. $options = array(
  221. array('', false, __('None')),
  222. );
  223. foreach($field_groups as $group){
  224. if(!is_array($group['fields'])) continue;
  225. $fields = array();
  226. foreach($group['fields'] as $f){
  227. if($f->get('id') != $this->get('id') && $f->canPrePopulate()) $fields[] = array($f->get('id'), ($this->get('dynamic_options') == $f->get('id')), $f->get('label'));
  228. }
  229. if(is_array($fields) && !empty($fields)) $options[] = array('label' => $group['section']->get('name'), 'options' => $fields);
  230. }
  231. $label->appendChild(Widget::Select('fields['.$this->get('sortorder').'][dynamic_options]', $options));
  232. $div->appendChild($label);
  233. if(isset($errors['dynamic_options'])) $wrapper->appendChild(Widget::wrapFormElementWithError($div, $errors['dynamic_options']));
  234. else $wrapper->appendChild($div);
  235. ## Allow selection of multiple items
  236. $label = Widget::Label();
  237. $input = Widget::Input('fields['.$this->get('sortorder').'][allow_multiple_selection]', 'yes', 'checkbox');
  238. if($this->get('allow_multiple_selection') == 'yes') $input->setAttribute('checked', 'checked');
  239. $label->setValue(__('%s Allow selection of multiple options', array($input->generate())));
  240. $wrapper->appendChild($label);
  241. $this->appendShowColumnCheckbox($wrapper);
  242. }
  243. function groupRecords($records){
  244. if(!is_array($records) || empty($records)) return;
  245. $groups = array($this->get('element_name') => array());
  246. foreach($records as $r){
  247. $data = $r->getData($this->get('id'));
  248. $value = General::sanitize($data['value']);
  249. $handle = Lang::createHandle($value);
  250. if(!isset($groups[$this->get('element_name')][$handle])){
  251. $groups[$this->get('element_name')][$handle] = array('attr' => array('handle' => $handle, 'value' => $value),
  252. 'records' => array(), 'groups' => array());
  253. }
  254. $groups[$this->get('element_name')][$handle]['records'][] = $r;
  255. }
  256. return $groups;
  257. }
  258. function createTable(){
  259. return Symphony::Database()->query(
  260. "CREATE TABLE IF NOT EXISTS `tbl_entries_data_" . $this->get('id') . "` (
  261. `id` int(11) unsigned NOT NULL auto_increment,
  262. `entry_id` int(11) unsigned NOT NULL,
  263. `handle` varchar(255) default NULL,
  264. `value` varchar(255) default NULL,
  265. PRIMARY KEY (`id`),
  266. KEY `entry_id` (`entry_id`),
  267. KEY `handle` (`handle`),
  268. KEY `value` (`value`)
  269. ) TYPE=MyISAM;"
  270. );
  271. }
  272. public function getExampleFormMarkup(){
  273. $states = $this->getToggleStates();
  274. $options = array();
  275. foreach($states as $handle => $v){
  276. $options[] = array($v, NULL, $v);
  277. }
  278. $fieldname = 'fields['.$this->get('element_name').']';
  279. if($this->get('allow_multiple_selection') == 'yes') $fieldname .= '[]';
  280. $label = Widget::Label($this->get('label'));
  281. $label->appendChild(Widget::Select($fieldname, $options, ($this->get('allow_multiple_selection') == 'yes' ? array('multiple' => 'multiple') : NULL)));
  282. return $label;
  283. }
  284. }