Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
f299f7db | 6 | | Copyright CiviCRM LLC (c) 2004-2020 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
f299f7db | 31 | * @copyright CiviCRM LLC (c) 2004-2020 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Report_Form_Register extends CRM_Core_Form { | |
36 | public $_id; | |
37 | protected $_values = NULL; | |
38 | ||
39 | public function preProcess() { | |
40 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE); | |
41 | $this->_id = CRM_Utils_Request::retrieve('id', 'String', $this, FALSE); | |
42 | ||
43 | CRM_Utils_System::setTitle(ts('Report Template')); | |
44 | ||
45 | if ($this->_action & CRM_Core_Action::DELETE) { | |
46 | return; | |
47 | } | |
48 | ||
6a488035 TO |
49 | $this->_opID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', |
50 | 'report_template', 'id', 'name' | |
51 | ); | |
52 | ||
be2fb01f | 53 | $instanceInfo = []; |
6a488035 TO |
54 | } |
55 | ||
74cf4551 | 56 | /** |
fe482240 | 57 | * This virtual function is used to set the default values of. |
74cf4551 EM |
58 | * various form elements |
59 | * | |
60 | * access public | |
61 | * | |
a6c01b45 CW |
62 | * @return array |
63 | * reference to the array of default values | |
74cf4551 | 64 | */ |
00be9182 | 65 | public function setDefaultValues() { |
be2fb01f | 66 | $defaults = []; |
6a488035 TO |
67 | if ($this->_action & CRM_Core_Action::DELETE) { |
68 | return $defaults; | |
69 | } | |
70 | if ($this->_id) { | |
be2fb01f CW |
71 | $params = ['id' => $this->_id]; |
72 | $defaults = []; | |
6a488035 TO |
73 | CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_OptionValue', $params, $defaults); |
74 | } | |
75 | else { | |
76 | $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', | |
be2fb01f | 77 | ['option_group_id' => $this->_opID] |
6a488035 TO |
78 | ); |
79 | } | |
80 | return $defaults; | |
81 | } | |
82 | ||
83 | public function buildQuickForm() { | |
84 | if ($this->_action & CRM_Core_Action::DELETE) { | |
be2fb01f CW |
85 | $this->addButtons([ |
86 | [ | |
6a488035 TO |
87 | 'type' => 'next', |
88 | 'name' => ts('Delete'), | |
89 | 'spacing' => ' ', | |
90 | 'isDefault' => TRUE, | |
be2fb01f CW |
91 | ], |
92 | [ | |
6a488035 TO |
93 | 'type' => 'cancel', |
94 | 'name' => ts('Cancel'), | |
be2fb01f | 95 | ], |
c86d4e7c | 96 | ]); |
6a488035 TO |
97 | return; |
98 | } | |
99 | ||
be2fb01f CW |
100 | $this->add('text', 'label', ts('Title'), ['size' => 40], TRUE); |
101 | $this->add('text', 'value', ts('URL'), ['size' => 40], TRUE); | |
102 | $this->add('text', 'name', ts('Class'), ['size' => 40], TRUE); | |
103 | $element = $this->add('number', 'weight', ts('Order'), ['size' => 4], TRUE); | |
6a488035 | 104 | // $element->freeze( ); |
be2fb01f | 105 | $this->add('text', 'description', ts('Description'), ['size' => 40], TRUE); |
6a488035 TO |
106 | |
107 | $this->add('checkbox', 'is_active', ts('Enabled?')); | |
108 | $this->_components = CRM_Core_Component::getComponents(); | |
109 | //unset the report component | |
110 | unset($this->_components['CiviReport']); | |
111 | ||
be2fb01f | 112 | $components = []; |
6a488035 TO |
113 | foreach ($this->_components as $name => $object) { |
114 | $components[$object->componentID] = $object->info['translatedName']; | |
115 | } | |
116 | ||
be2fb01f | 117 | $this->add('select', 'component_id', ts('Component'), ['' => ts('Contact')] + $components); |
6a488035 | 118 | |
be2fb01f CW |
119 | $this->addButtons([ |
120 | [ | |
6a488035 TO |
121 | 'type' => 'upload', |
122 | 'name' => ts('Save'), | |
123 | 'spacing' => ' ', | |
124 | 'isDefault' => TRUE, | |
be2fb01f CW |
125 | ], |
126 | [ | |
6a488035 TO |
127 | 'type' => 'cancel', |
128 | 'name' => ts('Cancel'), | |
be2fb01f | 129 | ], |
c86d4e7c | 130 | ]); |
be2fb01f | 131 | $this->addFormRule(['CRM_Report_Form_Register', 'formRule'], $this); |
6a488035 TO |
132 | } |
133 | ||
74cf4551 EM |
134 | /** |
135 | * @param $fields | |
136 | * @param $files | |
137 | * @param $self | |
138 | * | |
139 | * @return array | |
140 | */ | |
00be9182 | 141 | public static function formRule($fields, $files, $self) { |
be2fb01f | 142 | $errors = []; |
6a488035 TO |
143 | $dupeClass = FALSE; |
144 | $reportUrl = new CRM_Core_DAO_OptionValue(); | |
145 | $reportUrl->option_group_id = $self->_opID; | |
146 | $reportUrl->value = $fields['value']; | |
147 | ||
148 | if ($reportUrl->find(TRUE) && $self->_id != $reportUrl->id) { | |
149 | $errors['value'] = ts('Url already exists in Database.'); | |
150 | ||
151 | if ($reportUrl->name == $fields['name']) { | |
152 | $dupeClass = TRUE; | |
153 | } | |
154 | } | |
155 | if (!$dupeClass) { | |
156 | $reportClass = new CRM_Core_DAO_OptionValue(); | |
157 | $reportClass->option_group_id = $self->_opID; | |
158 | $reportClass->name = $fields['name']; | |
159 | if ($reportClass->find(TRUE) && $self->_id != $reportClass->id) { | |
160 | $dupeClass = TRUE; | |
161 | } | |
162 | } | |
163 | ||
164 | if ($dupeClass) { | |
165 | $errors['name'] = ts('Class already exists in Database.'); | |
166 | } | |
167 | return $errors; | |
168 | } | |
169 | ||
170 | /** | |
fe482240 | 171 | * Process the form submission. |
6a488035 | 172 | * |
6a488035 | 173 | * |
355ba699 | 174 | * @return void |
6a488035 TO |
175 | */ |
176 | public function postProcess() { | |
177 | if ($this->_action & CRM_Core_Action::DELETE) { | |
178 | ||
179 | if (CRM_Core_BAO_OptionValue::del($this->_id)) { | |
be2fb01f | 180 | CRM_Core_Session::setStatus(ts('Selected %1 Report has been deleted.', [1 => $this->_GName]), ts('Record Deleted'), 'success'); |
6a488035 TO |
181 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/report/options/report_template', "reset=1")); |
182 | } | |
183 | else { | |
be2fb01f | 184 | CRM_Core_Session::setStatus(ts('Selected %1 type has not been deleted.', [1 => $this->_GName]), '', 'info'); |
6a488035 TO |
185 | CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_OptionValue', $fieldValues); |
186 | } | |
187 | } | |
188 | else { | |
189 | // get the submitted form values. | |
190 | $params = $this->controller->exportValues($this->_name); | |
6a488035 | 191 | |
ff625280 | 192 | $optionValue = CRM_Core_OptionValue::addOptionValue($params, 'report_template', $this->_action, $this->_id); |
be2fb01f | 193 | CRM_Core_Session::setStatus(ts('The %1 \'%2\' has been saved.', [ |
c86d4e7c SL |
194 | 1 => 'Report Template', |
195 | 2 => $optionValue->label, | |
196 | ]), ts('Saved'), 'success'); | |
6a488035 TO |
197 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/report/options/report_template', "reset=1")); |
198 | } | |
199 | } | |
96025800 | 200 | |
6a488035 | 201 | } |