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