Merge pull request #22974 from mattwire/activitytokens
[civicrm-core.git] / CRM / Report / Form / Register.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 class CRM_Report_Form_Register extends CRM_Core_Form {
18 public $_id;
19 protected $_values = NULL;
20
21 public function preProcess() {
22 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE);
23 $this->_id = CRM_Utils_Request::retrieve('id', 'String', $this, FALSE);
24
25 $this->setTitle(ts('Report Template'));
26
27 if ($this->_action & CRM_Core_Action::DELETE) {
28 return;
29 }
30
31 $this->_opID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
32 'report_template', 'id', 'name'
33 );
34
35 $instanceInfo = [];
36 }
37
38 /**
39 * This virtual function is used to set the default values of.
40 * various form elements
41 *
42 * access public
43 *
44 * @return array
45 * reference to the array of default values
46 */
47 public function setDefaultValues() {
48 $defaults = [];
49 if ($this->_action & CRM_Core_Action::DELETE) {
50 return $defaults;
51 }
52 if ($this->_id) {
53 $params = ['id' => $this->_id];
54 $defaults = [];
55 CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_OptionValue', $params, $defaults);
56 }
57 else {
58 $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
59 ['option_group_id' => $this->_opID]
60 );
61 }
62 return $defaults;
63 }
64
65 public function buildQuickForm() {
66 if ($this->_action & CRM_Core_Action::DELETE) {
67 $this->addButtons([
68 [
69 'type' => 'next',
70 'name' => ts('Delete'),
71 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
72 'isDefault' => TRUE,
73 ],
74 [
75 'type' => 'cancel',
76 'name' => ts('Cancel'),
77 ],
78 ]);
79 return;
80 }
81
82 $this->add('text', 'label', ts('Title'), ['size' => 40], TRUE);
83 $this->add('text', 'value', ts('URL'), ['size' => 40], TRUE);
84 $this->add('text', 'name', ts('Class'), ['size' => 40], TRUE);
85 $element = $this->add('number', 'weight', ts('Order'), ['size' => 4], TRUE);
86 // $element->freeze( );
87 $this->add('text', 'description', ts('Description'), ['size' => 40], TRUE);
88
89 $this->add('checkbox', 'is_active', ts('Enabled?'));
90 $this->_components = CRM_Core_Component::getComponents();
91 //unset the report component
92 unset($this->_components['CiviReport']);
93
94 $components = [];
95 foreach ($this->_components as $name => $object) {
96 $components[$object->componentID] = $object->info['translatedName'];
97 }
98
99 $this->add('select', 'component_id', ts('Component'), ['' => ts('Contact')] + $components);
100
101 $this->addButtons([
102 [
103 'type' => 'upload',
104 'name' => ts('Save'),
105 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
106 'isDefault' => TRUE,
107 ],
108 [
109 'type' => 'cancel',
110 'name' => ts('Cancel'),
111 ],
112 ]);
113 $this->addFormRule(['CRM_Report_Form_Register', 'formRule'], $this);
114 }
115
116 /**
117 * @param $fields
118 * @param $files
119 * @param self $self
120 *
121 * @return array
122 */
123 public static function formRule($fields, $files, $self) {
124 $errors = [];
125 $dupeClass = FALSE;
126 $reportUrl = new CRM_Core_DAO_OptionValue();
127 $reportUrl->option_group_id = $self->_opID;
128 $reportUrl->value = $fields['value'];
129
130 if ($reportUrl->find(TRUE) && $self->_id != $reportUrl->id) {
131 $errors['value'] = ts('Url already exists in Database.');
132
133 if ($reportUrl->name == $fields['name']) {
134 $dupeClass = TRUE;
135 }
136 }
137 if (!$dupeClass) {
138 $reportClass = new CRM_Core_DAO_OptionValue();
139 $reportClass->option_group_id = $self->_opID;
140 $reportClass->name = $fields['name'];
141 if ($reportClass->find(TRUE) && $self->_id != $reportClass->id) {
142 $dupeClass = TRUE;
143 }
144 }
145
146 if ($dupeClass) {
147 $errors['name'] = ts('Class already exists in Database.');
148 }
149 return $errors;
150 }
151
152 /**
153 * Process the form submission.
154 *
155 *
156 * @return void
157 */
158 public function postProcess() {
159 if ($this->_action & CRM_Core_Action::DELETE) {
160
161 if (CRM_Core_BAO_OptionValue::del($this->_id)) {
162 CRM_Core_Session::setStatus(ts('Selected %1 Report has been deleted.', [1 => $this->_GName]), ts('Record Deleted'), 'success');
163 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/report/options/report_template', "reset=1"));
164 }
165 else {
166 CRM_Core_Session::setStatus(ts('Selected %1 type has not been deleted.', [1 => $this->_GName]), '', 'info');
167 CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_OptionValue', $fieldValues);
168 }
169 }
170 else {
171 // get the submitted form values.
172 $params = $this->controller->exportValues($this->_name);
173
174 $optionValue = CRM_Core_OptionValue::addOptionValue($params, 'report_template', $this->_action, $this->_id);
175 CRM_Core_Session::setStatus(ts('The %1 \'%2\' has been saved.', [
176 1 => 'Report Template',
177 2 => $optionValue->label,
178 ]), ts('Saved'), 'success');
179 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/report/options/report_template', "reset=1"));
180 }
181 }
182
183 }