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