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