Import from SVN (r45945, r596)
[civicrm-core.git] / CRM / SMS / Form / Provider.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id: $
33 *
34 */
35
36/**
37 *
38 */
39class CRM_SMS_Form_Provider extends CRM_Core_Form {
40 protected $_id = NULL; function preProcess() {
41
42 $this->_id = $this->get('id');
43
44 CRM_Utils_System::setTitle(ts('Manage - SMS Providers'));
45
46 if ($this->_id) {
47 $refreshURL = CRM_Utils_System::url('civicrm/admin/sms/provider',
48 "reset=1&action=update&id={$this->_id}",
49 FALSE, NULL, FALSE
50 );
51 }
52 else {
53 $refreshURL = CRM_Utils_System::url('civicrm/admin/sms/provider',
54 "reset=1&action=add",
55 FALSE, NULL, FALSE
56 );
57 }
58
59 $this->assign('refreshURL', $refreshURL);
60 }
61
62 /**
63 * Function to build the form
64 *
65 * @return None
66 * @access public
67 */
68 public function buildQuickForm() {
69 parent::buildQuickForm();
70
71 if ($this->_action & CRM_Core_Action::DELETE) {
72 $this->addButtons(array(
73 array(
74 'type' => 'next',
75 'name' => ts('Delete'),
76 'isDefault' => TRUE,
77 ),
78 array(
79 'type' => 'cancel',
80 'name' => ts('Cancel'),
81 ),
82 )
83 );
84 return;
85 }
86 else {
87 $this->addButtons(array(
88 array(
89 'type' => 'next',
90 'name' => ts('Save'),
91 'isDefault' => TRUE,
92 ),
93 array(
94 'type' => 'cancel',
95 'name' => ts('Cancel'),
96 ),
97 )
98 );
99 }
100
101 $attributes = CRM_Core_DAO::getAttribute('CRM_SMS_DAO_Provider');
102
103 $providerNames = CRM_Core_OptionGroup::values('sms_provider_name', FALSE, FALSE, FALSE, NULL, 'label');
104 $apiTypes = CRM_Core_OptionGroup::values('sms_api_type', FALSE, FALSE, FALSE, NULL, 'label');
105
106 $this->add('select', 'name', ts('Name'), array('' => '- select -') + $providerNames, TRUE, array('onchange' => "reload(true)"));
107
108 $this->add('text', 'title', ts('Title'),
109 $attributes['title'], TRUE
110 );
111
112 $this->addRule('title', ts('This Title already exists in Database.'), 'objectExists', array('CRM_SMS_DAO_Provider', $this->_id));
113
114 $this->add('text', 'username', ts('Username'),
115 $attributes['username'], TRUE
116 );
117
118 $this->add('password', 'password', ts('Password'),
119 $attributes['password'], TRUE
120 );
121
122 $this->add('select', 'api_type', ts('API Type'), $apiTypes, TRUE);
123
124 $this->add('text', 'api_url', ts('API Url'), $attributes['api_url'], TRUE);
125
126 $this->add('textarea', 'api_params', ts('API Parameters'),
127 "cols=50 rows=6", TRUE
128 );
129
130 $this->add('checkbox', 'is_active', ts('Is this provider active?'));
131
132 $this->add('checkbox', 'is_default', ts('Is this a default provider?'));
133 }
134
135 function setDefaultValues() {
136 $defaults = array();
137
138 $name = CRM_Utils_Request::retrieve('key', 'String', $this, FALSE, NULL);
139 if ($name) {
140 $defaults['name'] = $name;
141 $provider = CRM_SMS_Provider::singleton(array('provider' => $name));
142 $defaults['api_url'] = $provider->_apiURL;
143 }
144
145 if (!$this->_id) {
146 $defaults['is_active'] = $defaults['is_default'] = 1;
147 return $defaults;
148 }
149
150 $dao = new CRM_SMS_DAO_Provider();
151 $dao->id = $this->_id;
152
153 if ($name)
154 $dao->name = $name;
155
156 if (!$dao->find(TRUE)) {
157 return $defaults;
158 }
159
160 CRM_Core_DAO::storeValues($dao, $defaults);
161
162 return $defaults;
163 }
164
165 /**
166 * Function to process the form
167 *
168 * @access public
169 *
170 * @return None
171 */
172 public function postProcess() {
173
174 CRM_Utils_System::flushCache('CRM_SMS_DAO_Provider');
175
176 if ($this->_action & CRM_Core_Action::DELETE) {
177 CRM_SMS_BAO_Provider::del($this->_id);
178 CRM_Core_Session::setStatus(ts('Selected Provider has been deleted.'), ts('Deleted'), 'success');
179 return;
180 }
181
182 $recData = $values = $this->controller->exportValues($this->_name);
183 $recData['is_active'] = CRM_Utils_Array::value('is_active', $recData, 0);
184 $recData['is_default'] = CRM_Utils_Array::value('is_default', $recData, 0);
185
186 if ($this->_action & CRM_Core_Action::UPDATE) {
187 CRM_SMS_BAO_Provider::updateRecord($recData, $this->_id);
188 }
189 elseif ($this->_action & CRM_Core_Action::ADD) {
190 CRM_SMS_BAO_Provider::saveRecord($recData);
191 }
192 }
193}
194