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