Merge pull request #22342 from civicrm/5.45
[civicrm-core.git] / CRM / Price / Form / Preview.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
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 /**
29 * The group tree data.
30 *
31 * @var array
32 */
33 protected $_groupTree;
34
35 /**
36 * Pre processing work done here.
37 *
38 * gets session variables for group or field id
39 *
40 * @return void
41 */
42 public function preProcess() {
43 // get the controller vars
44 $groupId = $this->get('groupId');
45 $fieldId = $this->get('fieldId');
46
47 if ($fieldId) {
48 $groupTree = CRM_Price_BAO_PriceSet::getSetDetail($groupId);
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}");
52 $breadCrumb = [
53 [
54 'title' => ts('Price Set Fields'),
55 'url' => $url,
56 ],
57 ];
58 }
59 else {
60 // group preview
61 $this->_groupTree = CRM_Price_BAO_PriceSet::getSetDetail($groupId);
62 $this->assign('preview_type', 'group');
63 $this->assign('setTitle', CRM_Price_BAO_PriceSet::getTitle($groupId));
64 $url = CRM_Utils_System::url('civicrm/admin/price', 'reset=1');
65 $breadCrumb = [
66 [
67 'title' => ts('Price Sets'),
68 'url' => $url,
69 ],
70 ];
71 }
72 CRM_Utils_System::appendBreadCrumb($breadCrumb);
73 }
74
75 /**
76 * Set the default form values.
77 *
78 * @return array
79 * the default array reference
80 */
81 public function setDefaultValues() {
82 $defaults = [];
83 $groupId = $this->get('groupId');
84 $fieldId = $this->get('fieldId');
85 if (!empty($this->_groupTree[$groupId]['fields'])) {
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 /**
103 * Build the form object.
104 *
105 * @return void
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;
117 if (!empty($field['options'])) {
118 CRM_Price_BAO_PriceField::addQuickFormElement($this, $elementName, $fieldId, FALSE, $field['is_required']);
119 }
120 }
121 }
122 }
123
124 $this->addButtons([
125 [
126 'type' => 'cancel',
127 'name' => ts('Done with Preview'),
128 'isDefault' => TRUE,
129 ],
130 ]);
131 }
132
133 }