/app/Plugin/Field/Model/Field.php

https://github.com/quickapps-fr/QuickApps-CMS · PHP · 212 lines · 160 code · 40 blank · 12 comment · 28 complexity · 25a895f9fd56fd92bb14e883cc01eaa3 MD5 · raw file

  1. <?php
  2. /**
  3. * Field Model
  4. *
  5. * PHP version 5
  6. *
  7. * @package QuickApps.Plugin.Field.Model
  8. * @version 1.0
  9. * @author Christopher Castro <chris@quickapps.es>
  10. * @link http://cms.quickapps.es
  11. */
  12. class Field extends FieldAppModel {
  13. public $name = 'Field';
  14. public $useTable = 'fields';
  15. public $order = array('Field.ordering' => 'ASC');
  16. public $actsAs = array('Serialized' => array('settings'));
  17. public $validate = array(
  18. 'label' => array('required' => true, 'allowEmpty' => false, 'rule' => array('between', 1, 128), 'message' => 'Invalid field label.'),
  19. 'name' => array(
  20. 'alphaNumeric' => array('required' => true, 'allowEmpty' => false, 'rule' => array('custom', '/^[a-z0-9_]{3,32}$/i'), 'message' => "Field name must only contain letters and numbers. between 3-32 characters are required (character '_' is allowed)."),
  21. 'isUnique' => array('required' => true, 'allowEmpty' => false, 'rule' => 'checkUnique', 'message' => 'Field name already in use.')
  22. ),
  23. 'field_module' => array('required' => true, 'allowEmpty' => false, 'rule' => 'notEmpty', 'message' => 'Select a field type.')
  24. );
  25. public function beforeValidate() {
  26. # merge settings (array treatment): formatter form post
  27. if (isset($this->data['Field']['id']) && isset($this->data['Field']['settings'])) {
  28. $this->validate = false;
  29. $settings = $this->field('settings', array('Field.id' => $this->data['Field']['id']));
  30. $this->data['Field']['settings'] = Set::merge($settings, $this->data['Field']['settings']);
  31. if (!isset($this->data['Field']['field_module']) || empty($this->data['Field']['field_module'])) {
  32. $this->data['Field']['field_module'] = $this->field('field_module', array('Field.id' => $this->data['Field']['id']));
  33. }
  34. } elseif (!isset($this->data['Field']['id'])) { # new field
  35. $default_settings = array(
  36. 'display' => array(
  37. 'default' => array(
  38. 'label' => 'hidden',
  39. 'type' => '', #formatter name
  40. 'settings' => array(),
  41. 'ordering' => 0
  42. )
  43. )
  44. );
  45. $this->data['Field']['settings'] = isset($this->data['Field']['settings']) ? Set::merge($this->data['Field']['settings'], $default_settings) : $default_settings;
  46. }
  47. $before = $this->hook("{$this->data['Field']['field_module']}_before_validate_instance", $this);
  48. if ($before === false) {
  49. return $before;
  50. }
  51. return true;
  52. }
  53. public function beforeSave() {
  54. if (isset($this->data['Field']['field_module'])) {
  55. $this->data['Field']['settings'] = @unserialize($this->data['Field']['settings']);
  56. $before = $this->hook("{$this->data['Field']['field_module']}_before_save_instance", $this);
  57. $this->data['Field']['settings'] = !is_array($this->data['Field']['settings']) ? array() : $this->data['Field']['settings'];
  58. $this->data['Field']['settings'] = @serialize($this->data['Field']['settings']);
  59. if ($before === false) {
  60. return $before;
  61. }
  62. }
  63. return true;
  64. }
  65. public function afterSave() {
  66. $field = $this->read();
  67. $this->hook("{$field['Field']['field_module']}_after_save_instance", $this);
  68. }
  69. public function beforeDelete() {
  70. $this->data = $this->read(); # tmp holder (before/afterDelete)
  71. $before = $this->hook("{$this->field['Field']['field_module']}_before_delete_instance", $this, array('collectReturn' => false));
  72. if ($before === false) {
  73. return $before;
  74. }
  75. return true;
  76. }
  77. public function afterDelete() {
  78. $this->hook("{$this->data['Field']['field_module']}_after_delete_instance", $this, array('collectReturn' => false));
  79. }
  80. public function checkUnique($check) {
  81. $value = array_shift($check);
  82. return $this->find('count',
  83. array(
  84. 'conditions' => array(
  85. 'Field.belongsTo' => $this->data['Field']['belongsTo'],
  86. 'Field.name' => $value
  87. )
  88. )
  89. ) === 0;
  90. }
  91. public function move($id, $dir = 'up', $view_mode = false) {
  92. if (!($record = $this->findById($id))) {
  93. return false;
  94. }
  95. $_data = array('id' => $id, 'dir' => $dir, 'view_mode' => $view_mode);
  96. $this->hook("{$record['Field']['field_module']}_before_move_instance", $_data);
  97. extract($_data);
  98. # get brothers
  99. $nodes = $this->find('all',
  100. array(
  101. 'conditions' => array(
  102. 'Field.belongsTo ' => $record['Field']['belongsTo']
  103. ),
  104. 'order' => array("Field.ordering" => 'ASC'),
  105. 'fields' => array('id', 'ordering', 'settings', 'label'),
  106. 'recursive' => -1
  107. )
  108. );
  109. if (is_string($view_mode)) {
  110. $nodes = Set::sort($nodes, '{n}.Field.settings.display.' . $view_mode . '.ordering', 'asc');
  111. }
  112. $ids = Set::extract('/Field/id', $nodes);
  113. if (($dir == 'down' && $ids[count($ids)-1] == $record['Field']['id']) ||
  114. ($dir == 'up' && $ids[0] == $record['Field']['id'])
  115. ) { #edge -> cant go down/up
  116. return false;
  117. }
  118. $position = array_search($record['Field']['id'], $ids);
  119. $key = ($dir == 'up') ? $position-1 : $position+1;
  120. $tmp = $ids[$key];
  121. $ids[$key] = $ids[$position];
  122. $ids[$position] = $tmp;
  123. $i = 1;
  124. $prev_id = $this->id;
  125. foreach ($ids as $id) {
  126. $this->id = $id;
  127. if (is_string($view_mode)) {
  128. $node = Set::extract("/Field[id={$id}]", $nodes);
  129. if (isset($node[0]['Field']['settings']['display'][$view_mode])) {
  130. $node[0]['Field']['settings']['display'][$view_mode]['ordering'] = $i;
  131. $this->saveField('settings', $node[0]['Field']['settings'], false);
  132. }
  133. } else {
  134. $this->saveField('ordering', $i, false);
  135. }
  136. $i++;
  137. }
  138. $this->id = $prev_id;
  139. $this->hook("{$record['Field']['field_module']}_after_move_instance", $_data);
  140. return true;
  141. }
  142. public function setViewModes($modes, $conditions = false) {
  143. if (!is_array($modes) || empty($modes)) {
  144. $modes = array();
  145. }
  146. $conditions = (!$conditions || !is_array($conditions)) ? '1 = 1' : $conditions;
  147. $fields = $this->find('all', array('conditions' => $conditions));
  148. foreach ($fields as &$field) {
  149. $this->hook("{$field['Field']['field_module']}_before_set_view_modes", $field);
  150. $actual = array_keys($field['Field']['settings']['display']);
  151. foreach ($actual as $actual_mode) { # remove old modes
  152. if (!in_array($actual_mode, $modes) && $actual_mode !== 'default') {
  153. unset($field['Field']['settings']['display'][$actual_mode]);
  154. }
  155. }
  156. if (!empty($modes)){
  157. foreach ($modes as $new_mode) { # add if not set yet
  158. if (!isset($field['Field']['settings']['display'][$new_mode])) {
  159. $field['Field']['settings']['display'][$new_mode] = array(
  160. 'label' => 'hidden',
  161. 'type' => '', #formatter name
  162. 'settings' => array(),
  163. 'ordering' => 0
  164. );
  165. }
  166. }
  167. }
  168. $this->save($field, false);
  169. $this->hook("{$field['Field']['field_module']}_after_set_view_modes", $field);
  170. }
  171. }
  172. }