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