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