PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/fields/field.nestedcats.php

https://github.com/6ui11em/nestedcats
PHP | 281 lines | 194 code | 85 blank | 2 comment | 40 complexity | e01dd75213279e1d0918d6d04c62d9c4 MD5 | raw file
  1. <?php
  2. if(!defined('__IN_SYMPHONY__')) die('<h2>Symphony Error</h2><p>You cannot directly access this file</p>');
  3. Class fieldNestedCats extends Field{
  4. protected $_driver = null;
  5. function __construct(&$parent){
  6. parent::__construct($parent);
  7. $this->_name = __('Nested Categories');
  8. $this->_required = true;
  9. $this->_driver = $this->_engine->ExtensionManager->create('nestedcats');
  10. // Set default
  11. $this->set('show_column', 'no');
  12. $this->set('required', 'yes');
  13. }
  14. function canFilter(){
  15. return true;
  16. }
  17. function allowDatasourceOutputGrouping(){
  18. return true;
  19. }
  20. function allowDatasourceParamOutput(){
  21. return true;
  22. }
  23. public function getParameterPoolValue($data){
  24. return $data['relation_id'];
  25. }
  26. public function set($field, $value){
  27. if($field == 'related_field_id' && !is_array($value)){
  28. $value = explode(',', $value);
  29. }
  30. $this->_fields[$field] = $value;
  31. }
  32. public function setArray($array){
  33. if(empty($array) || !is_array($array)) return;
  34. foreach($array as $field => $value) $this->set($field, $value);
  35. }
  36. public function groupRecords($records){
  37. if(!is_array($records) || empty($records)) return;
  38. $groups = array($this->get('element_name') => array());
  39. foreach($records as $r){
  40. $data = $r->getData($this->get('id'));
  41. if(!is_array($data['relation_id'])){
  42. $data['relation_id'] = array($data['relation_id']);
  43. $data['value'] = array($data['value']);
  44. $data['handle'] = array($data['handle']);
  45. }
  46. $value = $data['relation_id'];
  47. foreach($value as $k => $v){
  48. if(!isset($groups[$this->get('element_name')][$v])){
  49. $groups[$this->get('element_name')][$v] = array('attr' => array('link-id' => $data['relation_id'][$k], 'link-handle' => $data['handle'][$k]));
  50. }
  51. $groups[$this->get('element_name')][$v]['records'][] = $r;
  52. }
  53. }
  54. return $groups;
  55. }
  56. public function prepareTableValue($data, XMLElement $link=NULL){
  57. if(!is_array($data) || (is_array($data) && !isset($data['relation_id']))) return parent::prepareTableValue(NULL);
  58. if(!is_array($data['relation_id'])){
  59. $data['relation_id'] = array($data['relation_id']);
  60. $data['value'] = array($data['value']);
  61. $data['handle'] = array($data['handle']);
  62. }
  63. $output = NULL;
  64. foreach($data['relation_id'] as $k => $v){
  65. $link = Widget::Anchor($data['value'][$k], URL . '/symphony/extension/nestedcats/list/view/' . $data['relation_id'][$k]);
  66. $output .= $link->generate() . ' ';
  67. }
  68. return trim($output);
  69. }
  70. function processRawFieldData($data, &$status, $simulate=false, $entry_id=NULL){
  71. $status = self::__OK__;
  72. if(empty($data)) return NULL;
  73. if(!is_array($data)) $data = array('relation_id' => $data);
  74. $result = array();
  75. foreach($data as $a => $value) {
  76. $result['relation_id'][] = $data[$a];
  77. $cat = $this->_driver->get($value);
  78. $result['value'][] = $cat['title'];
  79. $result['handle'][] = $cat['handle'];
  80. }
  81. return $result;
  82. }
  83. public function appendFormattedElement(&$wrapper, $data, $encode = false) {
  84. if (!is_array($data) || empty($data)) return;
  85. $list = new XMLElement($this->get('element_name'));
  86. if (!is_array($data['relation_id'])) {
  87. $data['relation_id'] = array($data['relation_id']);
  88. $data['handle'] = array($data['handle']);
  89. $data['value'] = array($data['value']);
  90. }
  91. foreach ($data['relation_id'] as $k => $v) {
  92. $list->appendChild(new XMLElement('item', General::sanitize($data['value'][$k]), array(
  93. 'handle' => $data['handle'][$k],
  94. 'id' => $v,
  95. )));
  96. }
  97. $wrapper->appendChild($list);
  98. }
  99. function displayPublishPanel(&$wrapper, $data=NULL, $flagWithError=NULL, $fieldnamePrefix=NULL, $fieldnamePostfix=NULL){
  100. if(!is_array($data['relation_id'])){
  101. $entry_ids = array($data['relation_id']);
  102. }else{
  103. $entry_ids = array_values($data['relation_id']);
  104. }
  105. if(!$root = $this->get('related_field_id')){
  106. $select = Widget::Select(NULL, NULL, array('disabled' => 'true'));
  107. } else {
  108. $multiple = $this->get('allow_multiple_selection') == 'yes';
  109. $select = $this->_driver->buildSelectAtPublishPannel($root[0], $entry_ids, $fieldnamePrefix, $this->get('element_name'), $fieldnamePostfix, $multiple);
  110. }
  111. $label = Widget::Label($this->get('label'));
  112. $label->appendChild($select);
  113. if($flagWithError != NULL) $wrapper->appendChild(Widget::wrapFormElementWithError($label, $flagWithError));
  114. else $wrapper->appendChild($label);
  115. }
  116. function displaySettingsPanel(&$wrapper, $errors=NULL){
  117. parent::displaySettingsPanel($wrapper, $errors);
  118. $div = new XMLElement('div', NULL, array('class' => 'group'));
  119. $label = Widget::Label(__('Root'));
  120. $sectionManager = new SectionManager($this->_engine);
  121. $sections = $sectionManager->fetch(NULL, 'ASC', 'name');
  122. $field_groups = array();
  123. if(is_array($sections) && !empty($sections)){
  124. foreach($sections as $section) $field_groups[$section->get('id')] = array('fields' => $section->fetchFields(), 'section' => $section);
  125. }
  126. $current = $this->get('related_field_id');
  127. $select = $this->_driver->buildSelectAtSettingsPannel($current[0], $this->get('sortorder'), 'related_field_id', null);
  128. $label->appendChild($select);
  129. $div->appendChild($label);
  130. if(isset($errors['related_field_id'])) $wrapper->appendChild(Widget::wrapFormElementWithError($div, $errors['related_field_id']));
  131. else $wrapper->appendChild($div);
  132. ## Allow multiple selection
  133. $label = Widget::Label();
  134. $input = Widget::Input('fields['.$this->get('sortorder').'][allow_multiple_selection]', 'yes', 'checkbox');
  135. if($this->get('allow_multiple_selection') == 'yes') $input->setAttribute('checked', 'checked');
  136. $label->setValue($input->generate() . ' ' . __('Allow selection of multiple options'));
  137. $wrapper->appendChild($label);
  138. $this->appendShowColumnCheckbox($wrapper);
  139. $this->appendRequiredCheckbox($wrapper);
  140. }
  141. function commit(){
  142. if(!parent::commit()) return false;
  143. $id = $this->get('id');
  144. if($id === false) return false;
  145. $fields = array();
  146. $fields['field_id'] = $id;
  147. if($this->get('related_field_id') != '') $fields['related_field_id'] = $this->get('related_field_id');
  148. $fields['related_field_id'] = implode(',', $this->get('related_field_id'));
  149. $fields['allow_multiple_selection'] = ($this->get('allow_multiple_selection') ? $this->get('allow_multiple_selection') : 'no');
  150. $this->Database->query("DELETE FROM `tbl_fields_".$this->handle()."` WHERE `field_id` = '$id'");
  151. if(!$this->Database->insert($fields, 'tbl_fields_' . $this->handle())) return false;
  152. $this->removeSectionAssociation($id);
  153. foreach($this->get('related_field_id') as $field_id){
  154. $this->createSectionAssociation(NULL, $id, $field_id);
  155. }
  156. return true;
  157. }
  158. function buildSortingSQL(&$joins, &$where, &$sort, $order='ASC'){
  159. $joins .= "INNER JOIN `tbl_entries_data_".$this->get('id')."` AS `ed` ON (`e`.`id` = `ed`.`entry_id`) ";
  160. $sort = 'ORDER BY ' . (in_array(strtolower($order), array('random', 'rand')) ? 'RAND()' : "`ed`.`relation_id` $order");
  161. }
  162. function buildDSRetrivalSQL($data, &$joins, &$where, $andOperation=false){
  163. $field_id = $this->get('id');
  164. $mode = is_numeric($data[0]) ? 'id' : 'handle';
  165. $tree = $this->_driver->fetch($data[0]);
  166. if(!$tree) return false;
  167. $cats = array();
  168. foreach($tree as $cat) {
  169. $cats[] = "'$cat[$mode]'";
  170. }
  171. unset($tree);
  172. if(!$cats) return false;
  173. $joins .= " LEFT JOIN `tbl_entries_data_$field_id` AS `t$field_id` ON (`e`.`id` = `t$field_id`.entry_id) ";
  174. $where .= ($mode == 'id') ?
  175. " AND `t$field_id`.relation_id IN (".@implode(',', $cats).") " :
  176. " AND `t$field_id`.handle IN (".@implode(',', $cats).") ";
  177. return true;
  178. }
  179. function createTable(){
  180. return $this->_engine->Database->query(
  181. "CREATE TABLE IF NOT EXISTS `tbl_entries_data_" . $this->get('id') . "` (
  182. `id` int(11) unsigned NOT NULL auto_increment,
  183. `entry_id` int(11) unsigned NOT NULL,
  184. `relation_id` int(11) unsigned NOT NULL,
  185. `handle` varchar(255) NOT NULL,
  186. `value` varchar(255) NOT NULL,
  187. PRIMARY KEY (`id`),
  188. KEY `entry_id` (`entry_id`),
  189. KEY `relation_id` (`relation_id`)
  190. ) TYPE=MyISAM;"
  191. );
  192. }
  193. public function getExampleFormMarkup(){
  194. return Widget::Input('fields['.$this->get('element_name').']', '...', 'hidden');
  195. }
  196. }