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