Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 | 32 | * $Id: $ |
6a488035 TO |
33 | */ |
34 | ||
35 | /** | |
57507ae6 | 36 | * SMS Form. |
6a488035 TO |
37 | */ |
38 | class CRM_SMS_Form_Provider extends CRM_Core_Form { | |
430ae6dd TO |
39 | protected $_id = NULL; |
40 | ||
00be9182 | 41 | public function preProcess() { |
6a488035 TO |
42 | |
43 | $this->_id = $this->get('id'); | |
44 | ||
e2046b33 | 45 | $this->setPageTitle(ts('SMS Provider')); |
6a488035 TO |
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 | /** | |
d8689418 | 64 | * Build the form object. |
6a488035 TO |
65 | */ |
66 | public function buildQuickForm() { | |
67 | parent::buildQuickForm(); | |
68 | ||
185eb0fc CW |
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 | ||
6a488035 | 82 | if ($this->_action & CRM_Core_Action::DELETE) { |
6a488035 TO |
83 | return; |
84 | } | |
6a488035 TO |
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 | ||
185eb0fc | 91 | $this->add('select', 'name', ts('Name'), array('' => '- select -') + $providerNames, TRUE); |
6a488035 TO |
92 | |
93 | $this->add('text', 'title', ts('Title'), | |
94 | $attributes['title'], TRUE | |
95 | ); | |
96 | ||
353ffa53 TO |
97 | $this->addRule('title', ts('This Title already exists in Database.'), 'objectExists', array( |
98 | 'CRM_SMS_DAO_Provider', | |
5c9ff055 | 99 | $this->_id, |
353ffa53 | 100 | )); |
6a488035 TO |
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 | ||
fd0e0077 | 123 | /** |
d8689418 | 124 | * Set the default values of various form elements. |
fd0e0077 | 125 | * |
fd0e0077 EM |
126 | * @return array |
127 | */ | |
00be9182 | 128 | public function setDefaultValues() { |
6a488035 TO |
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 | ||
db7de9c1 | 146 | if ($name) { |
6a488035 | 147 | $dao->name = $name; |
db7de9c1 | 148 | } |
6a488035 TO |
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 | /** | |
ee0ce2ef | 160 | * Process the form submission. |
6a488035 TO |
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 | } | |
96025800 | 183 | |
6a488035 | 184 | } |