Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
19 | * This class generates form components for previewing custom data | |
20 | * | |
21 | * It delegates the work to lower level subclasses and integrates the changes | |
22 | * back in. It also uses a lot of functionality with the CRM API's, so any change | |
23 | * made here could potentially affect the API etc. Be careful, be aware, use unit tests. | |
24 | * | |
25 | */ | |
26 | class CRM_Price_Form_Preview extends CRM_Core_Form { | |
27 | ||
28 | /** | |
fe482240 | 29 | * The group tree data. |
6a488035 TO |
30 | * |
31 | * @var array | |
32 | */ | |
33 | protected $_groupTree; | |
34 | ||
35 | /** | |
100fef9d | 36 | * Pre processing work done here. |
6a488035 TO |
37 | * |
38 | * gets session variables for group or field id | |
39 | * | |
6a488035 | 40 | * @return void |
95ea96be | 41 | */ |
bed98343 | 42 | public function preProcess() { |
6a488035 TO |
43 | // get the controller vars |
44 | $groupId = $this->get('groupId'); | |
45 | $fieldId = $this->get('fieldId'); | |
46 | ||
47 | if ($fieldId) { | |
9da8dc8c | 48 | $groupTree = CRM_Price_BAO_PriceSet::getSetDetail($groupId); |
6a488035 TO |
49 | $this->_groupTree[$groupId]['fields'][$fieldId] = $groupTree[$groupId]['fields'][$fieldId]; |
50 | $this->assign('preview_type', 'field'); | |
51 | $url = CRM_Utils_System::url('civicrm/admin/price/field', "reset=1&action=browse&sid={$groupId}"); | |
be2fb01f CW |
52 | $breadCrumb = [ |
53 | [ | |
353ffa53 | 54 | 'title' => ts('Price Set Fields'), |
6a488035 | 55 | 'url' => $url, |
be2fb01f CW |
56 | ], |
57 | ]; | |
6a488035 TO |
58 | } |
59 | else { | |
60 | // group preview | |
9da8dc8c | 61 | $this->_groupTree = CRM_Price_BAO_PriceSet::getSetDetail($groupId); |
6a488035 | 62 | $this->assign('preview_type', 'group'); |
9da8dc8c | 63 | $this->assign('setTitle', CRM_Price_BAO_PriceSet::getTitle($groupId)); |
6a488035 | 64 | $url = CRM_Utils_System::url('civicrm/admin/price', 'reset=1'); |
be2fb01f CW |
65 | $breadCrumb = [ |
66 | [ | |
353ffa53 | 67 | 'title' => ts('Price Sets'), |
6a488035 | 68 | 'url' => $url, |
be2fb01f CW |
69 | ], |
70 | ]; | |
6a488035 TO |
71 | } |
72 | CRM_Utils_System::appendBreadCrumb($breadCrumb); | |
73 | } | |
74 | ||
75 | /** | |
fe482240 | 76 | * Set the default form values. |
6a488035 | 77 | * |
a6c01b45 CW |
78 | * @return array |
79 | * the default array reference | |
6a488035 | 80 | */ |
00be9182 | 81 | public function setDefaultValues() { |
be2fb01f | 82 | $defaults = []; |
353ffa53 TO |
83 | $groupId = $this->get('groupId'); |
84 | $fieldId = $this->get('fieldId'); | |
a7488080 | 85 | if (!empty($this->_groupTree[$groupId]['fields'])) { |
6a488035 TO |
86 | foreach ($this->_groupTree[$groupId]['fields'] as $key => $val) { |
87 | foreach ($val['options'] as $keys => $values) { | |
88 | if ($values['is_default']) { | |
89 | if ($val['html_type'] == 'CheckBox') { | |
90 | $defaults["price_{$key}"][$keys] = 1; | |
91 | } | |
92 | else { | |
93 | $defaults["price_{$key}"] = $keys; | |
94 | } | |
95 | } | |
96 | } | |
97 | } | |
98 | } | |
99 | return $defaults; | |
100 | } | |
101 | ||
102 | /** | |
fe482240 | 103 | * Build the form object. |
6a488035 | 104 | * |
6a488035 | 105 | * @return void |
6a488035 TO |
106 | */ |
107 | public function buildQuickForm() { | |
108 | $this->assign('groupTree', $this->_groupTree); | |
109 | ||
110 | // add the form elements | |
111 | ||
112 | foreach ($this->_groupTree as $group) { | |
113 | if (is_array($group['fields']) && !empty($group['fields'])) { | |
114 | foreach ($group['fields'] as $field) { | |
115 | $fieldId = $field['id']; | |
116 | $elementName = 'price_' . $fieldId; | |
1019b2fe SL |
117 | if (!empty($field['options'])) { |
118 | CRM_Price_BAO_PriceField::addQuickFormElement($this, $elementName, $fieldId, FALSE, $field['is_required']); | |
119 | } | |
6a488035 TO |
120 | } |
121 | } | |
122 | } | |
123 | ||
be2fb01f CW |
124 | $this->addButtons([ |
125 | [ | |
c5c263ca AH |
126 | 'type' => 'cancel', |
127 | 'name' => ts('Done with Preview'), | |
128 | 'isDefault' => TRUE, | |
be2fb01f CW |
129 | ], |
130 | ]); | |
6a488035 | 131 | } |
96025800 | 132 | |
6a488035 | 133 | } |