Merge pull request #20399 from jaapjansma/dev_2624_2
[civicrm-core.git] / CRM / Report / Form / Register.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Report_Form_Register extends CRM_Core_Form {
18 public $_id;
19 protected $_values = NULL;
20
21 public function preProcess() {
22 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE);
23 $this->_id = CRM_Utils_Request::retrieve('id', 'String', $this, FALSE);
24
25 CRM_Utils_System::setTitle(ts('Report Template'));
26
27 if ($this->_action & CRM_Core_Action::DELETE) {
28 return;
29 }
30
6a488035
TO
31 $this->_opID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
32 'report_template', 'id', 'name'
33 );
34
be2fb01f 35 $instanceInfo = [];
6a488035
TO
36 }
37
74cf4551 38 /**
fe482240 39 * This virtual function is used to set the default values of.
74cf4551
EM
40 * various form elements
41 *
42 * access public
43 *
a6c01b45
CW
44 * @return array
45 * reference to the array of default values
74cf4551 46 */
00be9182 47 public function setDefaultValues() {
be2fb01f 48 $defaults = [];
6a488035
TO
49 if ($this->_action & CRM_Core_Action::DELETE) {
50 return $defaults;
51 }
52 if ($this->_id) {
be2fb01f
CW
53 $params = ['id' => $this->_id];
54 $defaults = [];
6a488035
TO
55 CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_OptionValue', $params, $defaults);
56 }
57 else {
58 $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
be2fb01f 59 ['option_group_id' => $this->_opID]
6a488035
TO
60 );
61 }
62 return $defaults;
63 }
64
65 public function buildQuickForm() {
66 if ($this->_action & CRM_Core_Action::DELETE) {
be2fb01f
CW
67 $this->addButtons([
68 [
6a488035
TO
69 'type' => 'next',
70 'name' => ts('Delete'),
71 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
72 'isDefault' => TRUE,
be2fb01f
CW
73 ],
74 [
6a488035
TO
75 'type' => 'cancel',
76 'name' => ts('Cancel'),
be2fb01f 77 ],
c86d4e7c 78 ]);
6a488035
TO
79 return;
80 }
81
be2fb01f
CW
82 $this->add('text', 'label', ts('Title'), ['size' => 40], TRUE);
83 $this->add('text', 'value', ts('URL'), ['size' => 40], TRUE);
84 $this->add('text', 'name', ts('Class'), ['size' => 40], TRUE);
85 $element = $this->add('number', 'weight', ts('Order'), ['size' => 4], TRUE);
6a488035 86 // $element->freeze( );
be2fb01f 87 $this->add('text', 'description', ts('Description'), ['size' => 40], TRUE);
6a488035
TO
88
89 $this->add('checkbox', 'is_active', ts('Enabled?'));
90 $this->_components = CRM_Core_Component::getComponents();
91 //unset the report component
92 unset($this->_components['CiviReport']);
93
be2fb01f 94 $components = [];
6a488035
TO
95 foreach ($this->_components as $name => $object) {
96 $components[$object->componentID] = $object->info['translatedName'];
97 }
98
be2fb01f 99 $this->add('select', 'component_id', ts('Component'), ['' => ts('Contact')] + $components);
6a488035 100
be2fb01f
CW
101 $this->addButtons([
102 [
6a488035
TO
103 'type' => 'upload',
104 'name' => ts('Save'),
105 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
106 'isDefault' => TRUE,
be2fb01f
CW
107 ],
108 [
6a488035
TO
109 'type' => 'cancel',
110 'name' => ts('Cancel'),
be2fb01f 111 ],
c86d4e7c 112 ]);
be2fb01f 113 $this->addFormRule(['CRM_Report_Form_Register', 'formRule'], $this);
6a488035
TO
114 }
115
74cf4551
EM
116 /**
117 * @param $fields
118 * @param $files
119 * @param $self
120 *
121 * @return array
122 */
00be9182 123 public static function formRule($fields, $files, $self) {
be2fb01f 124 $errors = [];
6a488035
TO
125 $dupeClass = FALSE;
126 $reportUrl = new CRM_Core_DAO_OptionValue();
127 $reportUrl->option_group_id = $self->_opID;
128 $reportUrl->value = $fields['value'];
129
130 if ($reportUrl->find(TRUE) && $self->_id != $reportUrl->id) {
131 $errors['value'] = ts('Url already exists in Database.');
132
133 if ($reportUrl->name == $fields['name']) {
134 $dupeClass = TRUE;
135 }
136 }
137 if (!$dupeClass) {
138 $reportClass = new CRM_Core_DAO_OptionValue();
139 $reportClass->option_group_id = $self->_opID;
140 $reportClass->name = $fields['name'];
141 if ($reportClass->find(TRUE) && $self->_id != $reportClass->id) {
142 $dupeClass = TRUE;
143 }
144 }
145
146 if ($dupeClass) {
147 $errors['name'] = ts('Class already exists in Database.');
148 }
149 return $errors;
150 }
151
152 /**
fe482240 153 * Process the form submission.
6a488035 154 *
6a488035 155 *
355ba699 156 * @return void
6a488035
TO
157 */
158 public function postProcess() {
159 if ($this->_action & CRM_Core_Action::DELETE) {
160
161 if (CRM_Core_BAO_OptionValue::del($this->_id)) {
be2fb01f 162 CRM_Core_Session::setStatus(ts('Selected %1 Report has been deleted.', [1 => $this->_GName]), ts('Record Deleted'), 'success');
6a488035
TO
163 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/report/options/report_template', "reset=1"));
164 }
165 else {
be2fb01f 166 CRM_Core_Session::setStatus(ts('Selected %1 type has not been deleted.', [1 => $this->_GName]), '', 'info');
6a488035
TO
167 CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_OptionValue', $fieldValues);
168 }
169 }
170 else {
171 // get the submitted form values.
172 $params = $this->controller->exportValues($this->_name);
6a488035 173
ff625280 174 $optionValue = CRM_Core_OptionValue::addOptionValue($params, 'report_template', $this->_action, $this->_id);
be2fb01f 175 CRM_Core_Session::setStatus(ts('The %1 \'%2\' has been saved.', [
c86d4e7c
SL
176 1 => 'Report Template',
177 2 => $optionValue->label,
178 ]), ts('Saved'), 'success');
6a488035
TO
179 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/report/options/report_template', "reset=1"));
180 }
181 }
96025800 182
6a488035 183}