1 <?php
3 class phoneAccount extends plugin
4 {
5 /* Definitions */
6 var $plHeadline= "Phone";
7 var $plDescription= "This does something";
8 var $has_mailAccount= FALSE;
10 /* Attributes */
11 var $telephoneNumber= "";
12 var $goFonHardware= "";
13 var $goFonForwarding= "";
14 var $goFonFormat= "";
15 var $goFonPIN= "";
16 var $goFonDeliveryMode= "";
17 var $phoneNumbers= array();
18 var $forwarders= array();
19 var $mail= "";
20 var $hardware_list= array();
21 var $used_hardware= array();
23 /* CLI vars */
24 var $cli_summary= "Manage users phone account";
25 var $cli_description= "Some longer text\nfor help";
26 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
28 /* attribute list for save action */
29 var $attributes= array("goFonDeliveryMode", "goFonForwarding", "goFonFormat",
30 "goFonHardware", "goFonPIN", "telephoneNumber");
31 var $objectclasses= array("goFonAccount");
33 function phoneAccount ($config, $dn= NULL)
34 {
35 plugin::plugin ($config, $dn);
37 /* Set phone hardware */
38 if (!isset($this->attrs['goFonHardware'])){
39 $this->goFonHardware= "automatic";
40 }
42 /* Preset voice format */
43 if (!isset($this->attrs['goFonFormat'])){
44 $this->goFonFormat= "wav";
45 }
47 /* Assemble phone numbers */
48 if (isset($this->attrs['telephoneNumber'])){
49 for ($i= 0; $i<$this->attrs['telephoneNumber']['count']; $i++){
50 $number= $this->attrs['telephoneNumber'][$i];
51 $this->phoneNumbers[$number]= $number;
52 }
53 }
55 /* Assemble forwarders */
56 if (isset($this->attrs['goFonForwarding'])){
57 for ($i= 0; $i<$this->attrs['goFonForwarding']['count']; $i++){
58 list($num, $v1, $v2) =split(';', $this->attrs['goFonForwarding'][$i]);
59 $this->forwarders[$num]= "$v1;$v2";
60 }
61 } else {
62 $this->forwarders= array("");
63 }
65 /* Set up has_mailAccount */
66 if (isset($this->attrs['objectClass'])){
67 if (in_array("gosaMailAccount", $this->attrs['objectClass'])){
68 $this->has_mailAccount= TRUE;
69 }
70 }
72 /* Load hardware list */
73 $ldap= $this->config->get_ldap_link();
74 $ldap->cd($this->config->current['BASE']);
75 $ldap->search("(objectClass=goFonHardware)", array('cn', 'description'));
76 while ($attrs= $ldap->fetch()){
77 $cn= $attrs['cn'][0];
78 if (isset($attrs['description'])){
79 $description= " - ".$attrs['description'][0];
80 } else {
81 $description= "";
82 }
83 $this->hardware_list[$cn]= "$cn$description";
85 }
87 /* Eventually colorize phones */
88 $ldap->cd($this->config->current['BASE']);
89 foreach ($this->hardware_list as $cn => $desc){
90 $ldap->search("(goFonHardware=$cn)", array('cn'));
91 if ($ldap->count() > 0){
92 $ldap->fetch();
93 if ($ldap->getDN() != $this->dn){
94 $this->used_hardware[$cn]= $ldap->getDN();
95 }
96 }
97 }
99 $this->hardware_list["automatic"]= _("automatic");
100 ksort($this->hardware_list);
101 }
104 function execute()
105 {
106 /* Do we need to flip is_account state? */
107 if (isset($_POST['modify_state'])){
108 $this->is_account= !$this->is_account;
109 }
111 /* Do we represent a valid account? */
112 if (!$this->is_account && $this->parent == NULL){
113 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
114 _("This account has no phone extensions.")."</b>";
115 $display.= back_to_main();
116 return($display);
117 }
119 $display= "";
121 /* Show tab dialog headers */
122 if ($this->parent != NULL){
123 if ($this->is_account){
124 $display= $this->show_header(_("Remove phone account"),
125 _("This account has phone features enabled. You can disable them by clicking below."));
126 } else {
127 $display= $this->show_header(_("Create phone account"),
128 _("This account has phone features disabled. You can enable them by clicking below."));
129 return ($display);
130 }
131 }
133 /* Add phone number */
134 if (isset($_POST["add_phonenumber"]) && $_POST['phonenumber']){
135 if (is_phone_nr($_POST['phonenumber'])){
136 $number= $_POST["phonenumber"];
137 $this->phoneNumbers[$number]= $number;
138 $this->is_modified= TRUE;
139 } else {
140 print_red(_("Please enter a valid phone number!"));
141 }
142 }
144 /* Remove phone number */
145 if (isset($_POST["delete_phonenumber"]) && isset($_POST["phonenumber_list"])){
146 foreach ($_POST['phonenumber_list'] as $number){
147 unset($this->phoneNumbers[$number]);
148 $this->is_modified= TRUE;
149 }
150 }
152 /* Check for forwarding action */
153 foreach ($this->forwarders as $nr => $fw){
155 /* Buttons pressed? */
156 if (isset($_POST["add_fw$nr"])){
157 $this->forwarders= $this->insert_after("", $nr, $this->forwarders);
158 }
159 if (isset($_POST["remove_fw$nr"])){
160 unset($this->forwarders[$nr]);
161 }
162 }
164 /* Prepare templating */
165 $smarty= get_smarty();
167 /* Transfer ACL's */
168 foreach($this->attributes as $val){
169 $smarty->assign($val."ACL", chkacl($this->acl, "$val"));
170 }
172 /* Fill arrays */
173 $smarty->assign ("goFonHardware", $this->goFonHardware);
174 if (!count($this->phoneNumbers)){
175 $smarty->assign ("phoneNumbers", array(""));
176 } else {
177 $smarty->assign ("phoneNumbers", $this->phoneNumbers);
178 }
179 $hl= "<select size=\"1\" name=\"goFonHardware\" title=\"".
180 _("Choose your private phone")."\" ".chkacl($this->acl, "goFonHardware").">\n";
181 foreach ($this->hardware_list as $cn => $description){
182 if ($cn == $this->goFonHardware){
183 $selected= "selected";
184 } else {
185 $selected= "";
186 }
187 if (isset($this->used_hardware[$cn])){
188 $color= "style=\"color:#A0A0A0\"";
189 } else {
190 $color= "";
191 }
192 $hl.= " <option $color label=\"$cn\" value=\"$cn\" $selected>$description</option>\n";
193 }
194 $hl.= "</select>\n";
195 $smarty->assign ("hardware_list", $hl);
197 /* Generate forwarder view */
198 $forwarder_list="";
199 $acl= chkacl($this->acl, "goFonForwaring");
200 foreach ($this->forwarders as $nr => $fw){
201 if ($fw == ""){
202 $number= ""; $timeout= "";
203 } else {
204 list($number, $timeout)= split(";", $fw);
205 }
206 $forwarder_list.= "<tr><td>";
207 $forwarder_list.= "<input name=\"fwn$nr\" size=25 align=\"middle\" maxlength=60 value=\"$number\" $acl>";
208 $forwarder_list.= "</td><td>";
209 $forwarder_list.= "<input name=\"fwt$nr\" size=5 align=\"middle\" maxlength=5 value=\"$timeout\" $acl>";
210 $forwarder_list.= "</td><td>";
211 $forwarder_list.= "<input type=\"submit\" value=\""._("Add")."\" name=\"add_fw$nr\" $acl>";
212 if (count($this->forwarders) > 1){
213 $forwarder_list.= "<input type=\"submit\" value=\""._("Remove")."\" name=\"remove_fw$nr\" $acl>";
214 }
215 $forwarder_list.= "</td></tr>";
216 }
217 $smarty->assign("forwarder_list", $forwarder_list);
221 /* Check box */
222 if ($this->parent->by_object['mailAccount'] &&
223 $this->parent->by_object['mailAccount']->is_account &&
224 preg_match("/M/i", $this->goFonDeliveryMode)){
225 $smarty->assign("fon_to_mail", "checked");
226 } else {
227 $smarty->assign("fon_to_mail", "");
228 }
230 if (!isset($this->parent->by_object['mailAccount'])) {
231 $smarty->assign("has_mailaccount", "false");
232 $this->has_mailAccount= false;
233 } elseif ( !$this->parent->by_object['mailAccount']->is_account){
234 $smarty->assign("has_mailaccount", "false");
235 $this->has_mailAccount= false;
236 } else {
237 $smarty->assign("has_mailaccount", "true");
238 }
240 /* Show main page */
241 $display.= $smarty->fetch(get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
242 return($display);
243 }
246 function save_object()
247 {
248 if (isset($_POST["phoneTab"])){
249 plugin::save_object();
251 /* Save checkbox */
252 if (isset($_POST['fon_to_mail'])){
253 $tmp= "[M]";
254 } else {
255 $tmp= "[]";
256 }
257 if (chkacl ($this->acl, "goFonDeliveryMode") == ""){
258 if ($this->goFonDeliveryMode != $tmp){
259 $this->is_modified= TRUE;
260 }
261 $this->goFonDeliveryMode= $tmp;
262 }
264 /* Save forwarding numbers and timeouts */
265 if (chkacl ($this->acl, "goFonForwarder") == ""){
266 foreach ($this->forwarders as $nr => $fw){
267 $tmp= $_POST["fwn$nr"].";".$_POST["fwt$nr"];
268 if ($this->forwarders[$nr] != $tmp){
269 $this->is_modified= TRUE;
270 }
271 $this->forwarders[$nr]= $tmp;
272 }
273 }
275 /* Check if mail account is active and correct the internal
276 reference to represent the current status. */
277 if ($this->parent->by_object['mailAccount']->is_account){
278 $this->has_mailAccount= TRUE;
279 }
280 }
282 }
284 function check()
285 {
286 /* Reset message array */
287 $message= array();
289 /* We need at least one phone number */
290 if (count($this->phoneNumbers) == 0){
291 $message[]= sprintf(_("You need to specify at least one phone number!"));
292 }
294 /* Check timestamps and phonenumbers */
295 foreach ($this->forwarders as $fw){
297 /* Skip empty values */
298 if ($fw == ";"){
299 continue;
300 }
302 /* Check */
303 list($number, $timeout)= split(";", $fw);
304 if (!is_phone_nr($number)){
305 $message[]= sprintf(_("The number '%s' is no valid phone number!"), $number);
306 }
307 if (!is_id($timeout)){
308 $message[]= sprintf(_("The timeout '%s' contains invalid characters!"), $timeout);
309 }
310 }
312 return ($message);
313 }
317 function save()
318 {
319 plugin::save();
321 /* goFonAccount has "mail" as must! Block if no mailaddress is specified... */
322 if (isset($this->parent->by_object['mailAccount']) &&
323 !$this->parent->by_object['mailAccount']->is_account) {
325 $this->goFonDeliveryMode= preg_replace("/M/i", "", $this->goFonDeliveryMode);
326 }
328 /* Save arrays */
329 $this->attrs['telephoneNumber']= array();
330 foreach ($this->phoneNumbers as $number){
331 $this->attrs['telephoneNumber'][]= $number;
332 }
333 $this->attrs['goFonForwarding']= array();
334 foreach ($this->forwarders as $index => $number){
335 $this->attrs['goFonForwarding'][]= "$index;$number";
336 }
338 /* Write back to ldap */
339 $ldap= $this->config->get_ldap_link();
340 $ldap->cd($this->dn);
341 $ldap->modify($this->attrs);
342 show_ldap_error($ldap->get_error());
344 /* Optionally execute a command after we're done */
345 if ($this->initially_was_account == $this->is_account){
346 if ($this->is_modified){
347 $this->handle_post_events("modify");
348 }
349 } else {
350 $this->handle_post_events("add");
351 }
353 }
356 function insert_after($entry, $nr, $list)
357 {
358 /* Is the entry free? No? Make it free... */
359 if (isset($list[$nr])) {
360 $dest= array();
361 $newidx= 0;
363 foreach ($list as $idx => $contents){
364 $dest[$newidx++]= $contents;
365 if ($idx == $nr){
366 $dest[$newidx++]= $entry;
367 }
368 }
369 } else {
370 $dest= $list;
371 $dest[$nr]= $entry;
372 }
374 return ($dest);
375 }
378 function adapt_from_template($dn)
379 {
380 plugin::adapt_from_template($dn);
382 /* Assemble phone numbers */
383 if (isset($this->attrs['telephoneNumber'])){
384 for ($i= 0; $i<$this->attrs['telephoneNumber']['count']; $i++){
385 $number= $this->attrs['telephoneNumber'][$i];
386 $this->phoneNumbers[$number]= $number;
387 }
388 }
390 /* Assemble forwarders */
391 if (isset($this->attrs['goFonForwarding'])){
392 for ($i= 0; $i<$this->attrs['goFonForwarding']['count']; $i++){
393 list($num, $v1, $v2) =split(';', $this->attrs['goFonForwarding'][$i]);
394 $this->forwarders[$num]= "$v1;$v2";
395 }
396 } else {
397 $this->forwarders= array("");
398 }
399 }
402 function remove_from_parent()
403 {
404 /* Cancel if there's nothing to do here */
405 if (!$this->initially_was_account){
406 return;
407 }
409 plugin::remove_from_parent();
411 /* Just keep one phone number */
412 if (count($this->telephoneNumber) && $this->telephoneNumber != ""){
413 $this->attrs['telephoneNumber']= $this->telephoneNumber[0];
414 } else {
415 $this->attrs['telephoneNumber']= array();
416 }
418 $ldap= $this->config->get_ldap_link();
419 $ldap->cd($this->dn);
420 $ldap->modify($this->attrs);
421 show_ldap_error($ldap->get_error());
423 /* Optionally execute a command after we're done */
424 $this->handle_post_events('remove');
425 }
428 }
430 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
431 ?>