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