modformcustomizationset.class.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /*
  3. * This file is part of MODX Revolution.
  4. *
  5. * Copyright (c) MODX, LLC. All Rights Reserved.
  6. *
  7. * For complete copyright and license information, see the COPYRIGHT and LICENSE
  8. * files found in the top-level directory of this distribution.
  9. */
  10. /**
  11. * A collection of rules for the related Form Customization Profile. Can be applied to different "actions", or pages,
  12. * within the manager. Also can set a constraint on the set so that it only applies under certain circumstances, or
  13. * with a certain template.
  14. *
  15. * @property int $profile The ID of the Profile this set belongs to
  16. * @property int $action The ID of the modAction this set is tied to
  17. * @property string $description A description of the set provided by the user
  18. * @property boolean $active Whether or not this set is active, and will have its rules applied.
  19. * @property int $template If set to a non-zero value, will only apply rules if the Resource has the specified Template ID
  20. * @property string $constraint Optional. The value of the constraint_field on constraint_class to check against to see if rules should be applied.
  21. * @property string $constraint_field Optional. The field name of the constraint_class to check against with the constraint value to see if rules should be applied.
  22. * @property string $constraint_class Optional. The class of the constraint_field to check against with the constraint value to see if rules should be applied.
  23. * @see modCustomizationProfile
  24. * @see modActionDom
  25. * @package modx
  26. */
  27. class modFormCustomizationSet extends xPDOSimpleObject {
  28. /**
  29. * Get the formatted data for the FC Set
  30. *
  31. * @return array
  32. */
  33. public function getData() {
  34. $setArray = array();
  35. // If the action ends in /* (wildcard rule), we assume the update action to be the "base" action
  36. $baseAction = $this->get('action');
  37. if (substr($baseAction, -2) === '/*') {
  38. $baseAction = str_replace('/*', '/update', $baseAction);
  39. }
  40. /* get fields */
  41. $c = $this->xpdo->newQuery('modActionField');
  42. $c->innerJoin('modActionField','Tab','Tab.name = modActionField.tab');
  43. $c->select($this->xpdo->getSelectColumns('modActionField','modActionField'));
  44. $c->select(array(
  45. 'tab_rank' => 'Tab.rank',
  46. ));
  47. $c->where(array(
  48. 'action' => $baseAction,
  49. 'type' => 'field',
  50. ));
  51. $c->sortby('Tab.rank','ASC');
  52. $c->sortby('modActionField.rank','ASC');
  53. $fields = $this->xpdo->getCollection('modActionField',$c);
  54. /** @var modActionField $field */
  55. foreach ($fields as $field) {
  56. $c = $this->xpdo->newQuery('modActionDom');
  57. $c->where(array(
  58. 'set' => $this->get('id'),
  59. 'name' => $field->get('name'),
  60. ));
  61. $rules = $this->xpdo->getCollection('modActionDom',$c);
  62. $fieldArray = $field->toArray();
  63. $fieldArray['visible'] = true;
  64. $fieldArray['label'] = '';
  65. $fieldArray['default_value'] = '';
  66. /** @var modActionDom $rule */
  67. foreach ($rules as $rule) {
  68. switch ($rule->get('rule')) {
  69. case 'fieldVisible':
  70. if ($rule->get('value') == 0) {
  71. $fieldArray['visible'] = false;
  72. }
  73. break;
  74. case 'fieldDefault':
  75. $fieldArray['default_value'] = $rule->get('value');
  76. break;
  77. case 'fieldTitle':
  78. case 'fieldLabel':
  79. $fieldArray['label'] = $rule->get('value');
  80. break;
  81. }
  82. }
  83. $setArray['fields'][] = $fieldArray;
  84. }
  85. /* get TVs */
  86. if ($this->get('template')) {
  87. $c = $this->xpdo->newQuery('modTemplateVar');
  88. $c->leftJoin('modCategory','Category');
  89. $c->innerJoin('modTemplateVarTemplate','TemplateVarTemplates');
  90. $c->select($this->xpdo->getSelectColumns('modTemplateVar', 'modTemplateVar'));
  91. $c->select(array(
  92. 'Category.category AS category_name',
  93. ));
  94. $c->where(array(
  95. 'TemplateVarTemplates.templateid' => $this->get('template'),
  96. ));
  97. $c->sortby('Category.category','ASC');
  98. $c->sortby('TemplateVarTemplates.rank','ASC');
  99. $tvs = $this->xpdo->getCollection('modTemplateVar',$c);
  100. } else {
  101. $c = $this->xpdo->newQuery('modTemplateVar');
  102. $c->leftJoin('modCategory','Category');
  103. $c->select($this->xpdo->getSelectColumns('modTemplateVar', 'modTemplateVar'));
  104. $c->select(array(
  105. 'Category.category AS category_name',
  106. ));
  107. $c->sortby('Category.category','ASC');
  108. $c->sortby('modTemplateVar.name','ASC');
  109. $tvs = $this->xpdo->getCollection('modTemplateVar',$c);
  110. }
  111. /** @var modTemplateVar $tv */
  112. foreach ($tvs as $tv) {
  113. $c = $this->xpdo->newQuery('modActionDom');
  114. $c->where(array(
  115. 'set' => $this->get('id'),
  116. ));
  117. $c->andCondition(array(
  118. 'name:=' => 'tv'.$tv->get('id'),
  119. 'OR:value:=' => 'tv'.$tv->get('id'),
  120. ),null,2);
  121. $rules = $this->xpdo->getCollection('modActionDom',$c);
  122. $tvArray = $tv->toArray('',true,true);
  123. $tvArray['visible'] = true;
  124. $tvArray['label'] = '';
  125. $tvArray['default_value'] = $tv->get('default_text');
  126. $tvArray['tab'] = 'modx-panel-resource-tv';
  127. $tvArray['rank'] = '';
  128. /** @var modActionDom $rule */
  129. foreach ($rules as $rule) {
  130. switch ($rule->get('rule')) {
  131. case 'tvVisible':
  132. if ($rule->get('value') == 0) {
  133. $tvArray['visible'] = false;
  134. }
  135. break;
  136. case 'tvDefault':
  137. case 'tvDefaultValue':
  138. $tvArray['default_value'] = $rule->get('value');
  139. break;
  140. case 'tvTitle':
  141. case 'tvLabel':
  142. $tvArray['label'] = $rule->get('value');
  143. break;
  144. case 'tvMove':
  145. $tvArray['tab'] = $rule->get('value');
  146. /* subtract 20 from rank that have been added in update processor */
  147. $tvArray['rank'] = ((int)$rule->get('rank'))-20;
  148. if ($tvArray['rank'] < 0) $tvArray['rank'] = 0;
  149. break;
  150. }
  151. }
  152. $setArray['tvs'][] = $tvArray;
  153. }
  154. /* get tabs */
  155. $c = $this->xpdo->newQuery('modActionField');
  156. $c->where(array(
  157. 'action' => $baseAction,
  158. 'type' => 'tab',
  159. ));
  160. $c->sortby($this->xpdo->escape('rank'), 'ASC');
  161. $tabs = $this->xpdo->getCollection('modActionField',$c);
  162. /** @var modActionField $tab */
  163. foreach ($tabs as $tab) {
  164. $c = $this->xpdo->newQuery('modActionDom');
  165. $c->where(array(
  166. 'set' => $this->get('id'),
  167. 'name' => $tab->get('name'),
  168. ));
  169. $rules = $this->xpdo->getCollection('modActionDom',$c);
  170. $tabArray = $tab->toArray();
  171. $tabArray['visible'] = true;
  172. $tabArray['label'] = '';
  173. foreach ($rules as $rule) {
  174. switch ($rule->get('rule')) {
  175. case 'tabVisible':
  176. if ($rule->get('value') == 0) {
  177. $tabArray['visible'] = false;
  178. }
  179. break;
  180. case 'tabLabel':
  181. case 'tabTitle':
  182. $tabArray['label'] = $rule->get('value');
  183. break;
  184. }
  185. }
  186. $setArray['tabs'][] = $tabArray;
  187. }
  188. $newTabs = $this->xpdo->getCollection('modActionDom',array(
  189. 'set' => $this->get('id'),
  190. 'action' => $this->get('action'),
  191. 'rule' => 'tabNew',
  192. ));
  193. foreach ($newTabs as $tab) {
  194. $tabArray = $tab->toArray();
  195. $tabArray['visible'] = true;
  196. $tabArray['label'] = $tab->get('value');
  197. $tabArray['default_value'] = '';
  198. $tabArray['type'] = 'new';
  199. $setArray['tabs'][] = $tabArray;
  200. }
  201. return $setArray;
  202. }
  203. }