Merge pull request #11986 from eileenmcnaughton/test
[civicrm-core.git] / CRM / Report / Form / Register.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2018
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
49 $this->_opID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
50 'report_template', 'id', 'name'
51 );
52
53 $instanceInfo = array();
54 }
55
56 /**
57 * This virtual function is used to set the default values of.
58 * various form elements
59 *
60 * access public
61 *
62 * @return array
63 * reference to the array of default values
64 */
65 public function setDefaultValues() {
66 $defaults = array();
67 if ($this->_action & CRM_Core_Action::DELETE) {
68 return $defaults;
69 }
70 if ($this->_id) {
71 $params = array('id' => $this->_id);
72 $defaults = array();
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',
77 array('option_group_id' => $this->_opID)
78 );
79 }
80 return $defaults;
81 }
82
83 public function buildQuickForm() {
84 if ($this->_action & CRM_Core_Action::DELETE) {
85 $this->addButtons(array(
86 array(
87 'type' => 'next',
88 'name' => ts('Delete'),
89 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
90 'isDefault' => TRUE,
91 ),
92 array(
93 'type' => 'cancel',
94 'name' => ts('Cancel'),
95 ),
96 )
97 );
98 return;
99 }
100
101 $this->add('text', 'label', ts('Title'), array('size' => 40), TRUE);
102 $this->add('text', 'value', ts('URL'), array('size' => 40), TRUE);
103 $this->add('text', 'name', ts('Class'), array('size' => 40), TRUE);
104 $element = $this->add('text', 'weight', ts('Order'), array('size' => 4), TRUE);
105 // $element->freeze( );
106 $this->add('text', 'description', ts('Description'), array('size' => 40), TRUE);
107
108 $this->add('checkbox', 'is_active', ts('Enabled?'));
109 $this->_components = CRM_Core_Component::getComponents();
110 //unset the report component
111 unset($this->_components['CiviReport']);
112
113 $components = array();
114 foreach ($this->_components as $name => $object) {
115 $components[$object->componentID] = $object->info['translatedName'];
116 }
117
118 $this->add('select', 'component_id', ts('Component'), array('' => ts('Contact')) + $components);
119
120 $this->addButtons(array(
121 array(
122 'type' => 'upload',
123 'name' => ts('Save'),
124 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
125 'isDefault' => TRUE,
126 ),
127 array(
128 'type' => 'cancel',
129 'name' => ts('Cancel'),
130 ),
131 )
132 );
133 $this->addFormRule(array('CRM_Report_Form_Register', 'formRule'), $this);
134 }
135
136 /**
137 * @param $fields
138 * @param $files
139 * @param $self
140 *
141 * @return array
142 */
143 public static function formRule($fields, $files, $self) {
144 $errors = array();
145 $dupeClass = FALSE;
146 $reportUrl = new CRM_Core_DAO_OptionValue();
147 $reportUrl->option_group_id = $self->_opID;
148 $reportUrl->value = $fields['value'];
149
150 if ($reportUrl->find(TRUE) && $self->_id != $reportUrl->id) {
151 $errors['value'] = ts('Url already exists in Database.');
152
153 if ($reportUrl->name == $fields['name']) {
154 $dupeClass = TRUE;
155 }
156 }
157 if (!$dupeClass) {
158 $reportClass = new CRM_Core_DAO_OptionValue();
159 $reportClass->option_group_id = $self->_opID;
160 $reportClass->name = $fields['name'];
161 if ($reportClass->find(TRUE) && $self->_id != $reportClass->id) {
162 $dupeClass = TRUE;
163 }
164 }
165
166 if ($dupeClass) {
167 $errors['name'] = ts('Class already exists in Database.');
168 }
169 return $errors;
170 }
171
172 /**
173 * Process the form submission.
174 *
175 *
176 * @return void
177 */
178 public function postProcess() {
179 if ($this->_action & CRM_Core_Action::DELETE) {
180
181 if (CRM_Core_BAO_OptionValue::del($this->_id)) {
182 CRM_Core_Session::setStatus(ts('Selected %1 Report has been deleted.', array(1 => $this->_GName)), ts('Record Deleted'), 'success');
183 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/report/options/report_template', "reset=1"));
184 }
185 else {
186 CRM_Core_Session::setStatus(ts('Selected %1 type has not been deleted.', array(1 => $this->_GName)), '', 'info');
187 CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_OptionValue', $fieldValues);
188 }
189 }
190 else {
191 // get the submitted form values.
192 $params = $this->controller->exportValues($this->_name);
193 $ids = array();
194
195 $groupParams = array('name' => ('report_template'));
196 $optionValue = CRM_Core_OptionValue::addOptionValue($params, $groupParams, $this->_action, $this->_id);
197 CRM_Core_Session::setStatus(ts('The %1 \'%2\' has been saved.', array(
198 1 => 'Report Template',
199 2 => $optionValue->label,
200 )), ts('Saved'), 'success');
201 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/report/options/report_template', "reset=1"));
202 }
203 }
204
205 }