CRM-13863 - Get rid of double title for forms that use popups
[civicrm-core.git] / CRM / SMS / Form / Provider.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id: $
33 *
34 */
35
36/**
37 *
38 */
39class CRM_SMS_Form_Provider extends CRM_Core_Form {
430ae6dd
TO
40 protected $_id = NULL;
41
42 function preProcess() {
6a488035
TO
43
44 $this->_id = $this->get('id');
45
e2046b33 46 $this->setPageTitle(ts('SMS Provider'));
6a488035
TO
47
48 if ($this->_id) {
49 $refreshURL = CRM_Utils_System::url('civicrm/admin/sms/provider',
50 "reset=1&action=update&id={$this->_id}",
51 FALSE, NULL, FALSE
52 );
53 }
54 else {
55 $refreshURL = CRM_Utils_System::url('civicrm/admin/sms/provider',
56 "reset=1&action=add",
57 FALSE, NULL, FALSE
58 );
59 }
60
61 $this->assign('refreshURL', $refreshURL);
62 }
63
64 /**
65 * Function to build the form
66 *
355ba699 67 * @return void
6a488035
TO
68 * @access public
69 */
70 public function buildQuickForm() {
71 parent::buildQuickForm();
72
185eb0fc
CW
73 $this->addButtons(array(
74 array(
75 'type' => 'next',
76 'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'),
77 'isDefault' => TRUE,
78 ),
79 array(
80 'type' => 'cancel',
81 'name' => ts('Cancel'),
82 ),
83 )
84 );
85
6a488035 86 if ($this->_action & CRM_Core_Action::DELETE) {
6a488035
TO
87 return;
88 }
6a488035
TO
89
90 $attributes = CRM_Core_DAO::getAttribute('CRM_SMS_DAO_Provider');
91
92 $providerNames = CRM_Core_OptionGroup::values('sms_provider_name', FALSE, FALSE, FALSE, NULL, 'label');
93 $apiTypes = CRM_Core_OptionGroup::values('sms_api_type', FALSE, FALSE, FALSE, NULL, 'label');
94
185eb0fc 95 $this->add('select', 'name', ts('Name'), array('' => '- select -') + $providerNames, TRUE);
6a488035
TO
96
97 $this->add('text', 'title', ts('Title'),
98 $attributes['title'], TRUE
99 );
100
101 $this->addRule('title', ts('This Title already exists in Database.'), 'objectExists', array('CRM_SMS_DAO_Provider', $this->_id));
102
103 $this->add('text', 'username', ts('Username'),
104 $attributes['username'], TRUE
105 );
106
107 $this->add('password', 'password', ts('Password'),
108 $attributes['password'], TRUE
109 );
110
111 $this->add('select', 'api_type', ts('API Type'), $apiTypes, TRUE);
112
113 $this->add('text', 'api_url', ts('API Url'), $attributes['api_url'], TRUE);
114
115 $this->add('textarea', 'api_params', ts('API Parameters'),
116 "cols=50 rows=6", TRUE
117 );
118
119 $this->add('checkbox', 'is_active', ts('Is this provider active?'));
120
121 $this->add('checkbox', 'is_default', ts('Is this a default provider?'));
122 }
123
fd0e0077
EM
124 /**
125 * This virtual function is used to set the default values of
126 * various form elements
127 *
128 * access public
129 *
130 * @return array reference to the array of default values
131 *
132 */
133 /**
134 * @return array
135 */
6a488035
TO
136 function setDefaultValues() {
137 $defaults = array();
138
139 $name = CRM_Utils_Request::retrieve('key', 'String', $this, FALSE, NULL);
140 if ($name) {
141 $defaults['name'] = $name;
142 $provider = CRM_SMS_Provider::singleton(array('provider' => $name));
143 $defaults['api_url'] = $provider->_apiURL;
144 }
145
146 if (!$this->_id) {
147 $defaults['is_active'] = $defaults['is_default'] = 1;
148 return $defaults;
149 }
150
151 $dao = new CRM_SMS_DAO_Provider();
152 $dao->id = $this->_id;
153
154 if ($name)
155 $dao->name = $name;
156
157 if (!$dao->find(TRUE)) {
158 return $defaults;
159 }
160
161 CRM_Core_DAO::storeValues($dao, $defaults);
162
163 return $defaults;
164 }
165
166 /**
167 * Function to process the form
168 *
169 * @access public
170 *
355ba699 171 * @return void
6a488035
TO
172 */
173 public function postProcess() {
174
175 CRM_Utils_System::flushCache('CRM_SMS_DAO_Provider');
176
177 if ($this->_action & CRM_Core_Action::DELETE) {
178 CRM_SMS_BAO_Provider::del($this->_id);
179 CRM_Core_Session::setStatus(ts('Selected Provider has been deleted.'), ts('Deleted'), 'success');
180 return;
181 }
182
183 $recData = $values = $this->controller->exportValues($this->_name);
184 $recData['is_active'] = CRM_Utils_Array::value('is_active', $recData, 0);
185 $recData['is_default'] = CRM_Utils_Array::value('is_default', $recData, 0);
186
187 if ($this->_action & CRM_Core_Action::UPDATE) {
188 CRM_SMS_BAO_Provider::updateRecord($recData, $this->_id);
189 }
190 elseif ($this->_action & CRM_Core_Action::ADD) {
191 CRM_SMS_BAO_Provider::saveRecord($recData);
192 }
193 }
194}
195