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