Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
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 | */ | |
39 | class CRM_SMS_Form_Provider extends CRM_Core_Form { | |
430ae6dd TO |
40 | protected $_id = NULL; |
41 | ||
00be9182 | 42 | public 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 | /** | |
c490a46a | 65 | * Build the form object |
6a488035 | 66 | * |
355ba699 | 67 | * @return void |
6a488035 TO |
68 | */ |
69 | public function buildQuickForm() { | |
70 | parent::buildQuickForm(); | |
71 | ||
185eb0fc CW |
72 | $this->addButtons(array( |
73 | array( | |
74 | 'type' => 'next', | |
75 | 'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'), | |
76 | 'isDefault' => TRUE, | |
77 | ), | |
78 | array( | |
79 | 'type' => 'cancel', | |
80 | 'name' => ts('Cancel'), | |
81 | ), | |
82 | ) | |
83 | ); | |
84 | ||
6a488035 | 85 | if ($this->_action & CRM_Core_Action::DELETE) { |
6a488035 TO |
86 | return; |
87 | } | |
6a488035 TO |
88 | |
89 | $attributes = CRM_Core_DAO::getAttribute('CRM_SMS_DAO_Provider'); | |
90 | ||
91 | $providerNames = CRM_Core_OptionGroup::values('sms_provider_name', FALSE, FALSE, FALSE, NULL, 'label'); | |
92 | $apiTypes = CRM_Core_OptionGroup::values('sms_api_type', FALSE, FALSE, FALSE, NULL, 'label'); | |
93 | ||
185eb0fc | 94 | $this->add('select', 'name', ts('Name'), array('' => '- select -') + $providerNames, TRUE); |
6a488035 TO |
95 | |
96 | $this->add('text', 'title', ts('Title'), | |
97 | $attributes['title'], TRUE | |
98 | ); | |
99 | ||
353ffa53 TO |
100 | $this->addRule('title', ts('This Title already exists in Database.'), 'objectExists', array( |
101 | 'CRM_SMS_DAO_Provider', | |
102 | $this->_id | |
103 | )); | |
6a488035 TO |
104 | |
105 | $this->add('text', 'username', ts('Username'), | |
106 | $attributes['username'], TRUE | |
107 | ); | |
108 | ||
109 | $this->add('password', 'password', ts('Password'), | |
110 | $attributes['password'], TRUE | |
111 | ); | |
112 | ||
113 | $this->add('select', 'api_type', ts('API Type'), $apiTypes, TRUE); | |
114 | ||
115 | $this->add('text', 'api_url', ts('API Url'), $attributes['api_url'], TRUE); | |
116 | ||
117 | $this->add('textarea', 'api_params', ts('API Parameters'), | |
118 | "cols=50 rows=6", TRUE | |
119 | ); | |
120 | ||
121 | $this->add('checkbox', 'is_active', ts('Is this provider active?')); | |
122 | ||
123 | $this->add('checkbox', 'is_default', ts('Is this a default provider?')); | |
124 | } | |
125 | ||
fd0e0077 EM |
126 | /** |
127 | * This virtual function is used to set the default values of | |
128 | * various form elements | |
129 | * | |
130 | * access public | |
131 | * | |
a6c01b45 CW |
132 | * @return array |
133 | * reference to the array of default values | |
fd0e0077 EM |
134 | */ |
135 | /** | |
136 | * @return array | |
137 | */ | |
00be9182 | 138 | public function setDefaultValues() { |
6a488035 TO |
139 | $defaults = array(); |
140 | ||
141 | $name = CRM_Utils_Request::retrieve('key', 'String', $this, FALSE, NULL); | |
142 | if ($name) { | |
143 | $defaults['name'] = $name; | |
144 | $provider = CRM_SMS_Provider::singleton(array('provider' => $name)); | |
145 | $defaults['api_url'] = $provider->_apiURL; | |
146 | } | |
147 | ||
148 | if (!$this->_id) { | |
149 | $defaults['is_active'] = $defaults['is_default'] = 1; | |
150 | return $defaults; | |
151 | } | |
152 | ||
153 | $dao = new CRM_SMS_DAO_Provider(); | |
154 | $dao->id = $this->_id; | |
155 | ||
156 | if ($name) | |
157 | $dao->name = $name; | |
158 | ||
159 | if (!$dao->find(TRUE)) { | |
160 | return $defaults; | |
161 | } | |
162 | ||
163 | CRM_Core_DAO::storeValues($dao, $defaults); | |
164 | ||
165 | return $defaults; | |
166 | } | |
167 | ||
168 | /** | |
c490a46a | 169 | * Process the form submission |
6a488035 | 170 | * |
6a488035 | 171 | * |
355ba699 | 172 | * @return void |
6a488035 TO |
173 | */ |
174 | public function postProcess() { | |
175 | ||
176 | CRM_Utils_System::flushCache('CRM_SMS_DAO_Provider'); | |
177 | ||
178 | if ($this->_action & CRM_Core_Action::DELETE) { | |
179 | CRM_SMS_BAO_Provider::del($this->_id); | |
180 | CRM_Core_Session::setStatus(ts('Selected Provider has been deleted.'), ts('Deleted'), 'success'); | |
181 | return; | |
182 | } | |
183 | ||
184 | $recData = $values = $this->controller->exportValues($this->_name); | |
185 | $recData['is_active'] = CRM_Utils_Array::value('is_active', $recData, 0); | |
186 | $recData['is_default'] = CRM_Utils_Array::value('is_default', $recData, 0); | |
187 | ||
188 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
189 | CRM_SMS_BAO_Provider::updateRecord($recData, $this->_id); | |
190 | } | |
191 | elseif ($this->_action & CRM_Core_Action::ADD) { | |
192 | CRM_SMS_BAO_Provider::saveRecord($recData); | |
193 | } | |
194 | } | |
195 | } |