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 src=\"images/stop.png\" align=center> <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 $smarty->assign ("phoneNumbers", $this->phoneNumbers);
175 $hl= "<select size=\"1\" name=\"goFonHardware\" title=\"".
176 _("Choose your private phone")."\" ".chkacl($this->acl, "goFonHardware").">\n";
177 foreach ($this->hardware_list as $cn => $description){
178 if ($cn == $this->goFonHardware){
179 $selected= "selected";
180 } else {
181 $selected= "";
182 }
183 if (isset($this->used_hardware[$cn])){
184 $color= "style=\"color:#A0A0A0\"";
185 } else {
186 $color= "";
187 }
188 $hl.= " <option $color label=\"$cn\" value=\"$cn\" $selected>$description</option>\n";
189 }
190 $hl.= "</select>\n";
191 $smarty->assign ("hardware_list", $hl);
193 /* Generate forwarder view */
194 $forwarder_list="";
195 $acl= chkacl($this->acl, "goFonForwaring");
196 foreach ($this->forwarders as $nr => $fw){
197 if ($fw == ""){
198 $number= ""; $timeout= "";
199 } else {
200 list($number, $timeout)= split(";", $fw);
201 }
202 $forwarder_list.= "<tr><td>";
203 $forwarder_list.= "<input name=\"fwn$nr\" size=25 align=center maxlength=60 value=\"$number\" $acl>";
204 $forwarder_list.= "</td><td>";
205 $forwarder_list.= "<input name=\"fwt$nr\" size=5 align=center maxlength=5 value=\"$timeout\" $acl>";
206 $forwarder_list.= "</td><td>";
207 $forwarder_list.= "<input type=\"submit\" value=\""._("Add")."\" name=\"add_fw$nr\" $acl>";
208 if (count($this->forwarders) > 1){
209 $forwarder_list.= "<input type=\"submit\" value=\""._("Remove")."\" name=\"remove_fw$nr\" $acl>";
210 }
211 $forwarder_list.= "</td></tr>";
212 }
213 $smarty->assign("forwarder_list", $forwarder_list);
217 /* Check box */
218 if ($this->parent->by_object['mailAccount'] &&
219 $this->parent->by_object['mailAccount']->is_account &&
220 preg_match("/M/i", $this->goFonDeliveryMode)){
221 $smarty->assign("fon_to_mail", "checked");
222 } else {
223 $smarty->assign("fon_to_mail", "");
224 }
226 if (!isset($this->parent->by_object['mailAccount'])) {
227 $smarty->assign("has_mailaccount", "false");
228 $this->has_mailAccount= false;
229 } elseif ( !$this->parent->by_object['mailAccount']->is_account){
230 $smarty->assign("has_mailaccount", "false");
231 $this->has_mailAccount= false;
232 } else {
233 $smarty->assign("has_mailaccount", "true");
234 }
236 /* Show main page */
237 $display.= $smarty->fetch(get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
238 return($display);
239 }
242 function save_object()
243 {
244 if (isset($_POST["phoneTab"])){
245 plugin::save_object();
247 /* Save checkbox */
248 if (isset($_POST['fon_to_mail'])){
249 $tmp= "[M]";
250 } else {
251 $tmp= "[]";
252 }
253 if (chkacl ($this->acl, "goFonDeliveryMode") == ""){
254 if ($this->goFonDeliveryMode != $tmp){
255 $this->is_modified= TRUE;
256 }
257 $this->goFonDeliveryMode= $tmp;
258 }
260 /* Save forwarding numbers and timeouts */
261 if (chkacl ($this->acl, "goFonForwarder") == ""){
262 foreach ($this->forwarders as $nr => $fw){
263 $tmp= $_POST["fwn$nr"].";".$_POST["fwt$nr"];
264 if ($this->forwarders[$nr] != $tmp){
265 $this->is_modified= TRUE;
266 }
267 $this->forwarders[$nr]= $tmp;
268 }
269 }
271 /* Check if mail account is active and correct the internal
272 reference to represent the current status. */
273 if ($this->parent->by_object['mailAccount']->is_account){
274 $this->has_mailAccount= TRUE;
275 }
276 }
278 }
280 function check()
281 {
282 /* Reset message array */
283 $message= array();
285 /* We need at least one phone number */
286 if (count($this->phoneNumbers) == 0){
287 $message[]= sprintf(_("You need to specify at least one phone number!"));
288 }
290 /* Check timestamps and phonenumbers */
291 foreach ($this->forwarders as $fw){
293 /* Skip empty values */
294 if ($fw == ";"){
295 continue;
296 }
298 /* Check */
299 list($number, $timeout)= split(";", $fw);
300 if (!is_phone_nr($number)){
301 $message[]= sprintf(_("The number '%s' is no valid phone number!"), $number);
302 }
303 if (!is_id($timeout)){
304 $message[]= sprintf(_("The timeout '%s' contains invalid characters!"), $timeout);
305 }
306 }
308 return ($message);
309 }
313 function save()
314 {
315 plugin::save();
317 /* goFonAccount has "mail" as must! Block if no mailaddress is specified... */
318 if (isset($this->parent->by_object['mailAccount']) &&
319 !$this->parent->by_object['mailAccount']->is_account) {
321 $this->goFonDeliveryMode= preg_replace("/M/i", "", $this->goFonDeliveryMode);
322 }
324 /* Save arrays */
325 $this->attrs['telephoneNumber']= array();
326 foreach ($this->phoneNumbers as $number){
327 $this->attrs['telephoneNumber'][]= $number;
328 }
329 $this->attrs['goFonForwarding']= array();
330 foreach ($this->forwarders as $index => $number){
331 $this->attrs['goFonForwarding'][]= "$index;$number";
332 }
334 /* Write back to ldap */
335 $ldap= $this->config->get_ldap_link();
336 $ldap->cd($this->dn);
337 $ldap->modify($this->attrs);
338 show_ldap_error($ldap->get_error());
340 /* Optionally execute a command after we're done */
341 if ($this->initially_was_account == $this->is_account){
342 if ($this->is_modified){
343 $this->handle_post_events("modify");
344 }
345 } else {
346 $this->handle_post_events("add");
347 }
349 }
352 function insert_after($entry, $nr, $list)
353 {
354 /* Is the entry free? No? Make it free... */
355 if (isset($list[$nr])) {
356 $dest= array();
357 $newidx= 0;
359 foreach ($list as $idx => $contents){
360 $dest[$newidx++]= $contents;
361 if ($idx == $nr){
362 $dest[$newidx++]= $entry;
363 }
364 }
365 } else {
366 $dest= $list;
367 $dest[$nr]= $entry;
368 }
370 return ($dest);
371 }
374 function adapt_from_template($dn)
375 {
376 plugin::adapt_from_template($dn);
378 /* Assemble phone numbers */
379 if (isset($this->attrs['telephoneNumber'])){
380 for ($i= 0; $i<$this->attrs['telephoneNumber']['count']; $i++){
381 $number= $this->attrs['telephoneNumber'][$i];
382 $this->phoneNumbers[$number]= $number;
383 }
384 }
386 /* Assemble forwarders */
387 if (isset($this->attrs['goFonForwarding'])){
388 for ($i= 0; $i<$this->attrs['goFonForwarding']['count']; $i++){
389 list($num, $v1, $v2) =split(';', $this->attrs['goFonForwarding'][$i]);
390 $this->forwarders[$num]= "$v1;$v2";
391 }
392 } else {
393 $this->forwarders= array("");
394 }
395 }
398 function remove_from_parent()
399 {
400 /* Cancel if there's nothing to do here */
401 if (!$this->initially_was_account){
402 return;
403 }
405 plugin::remove_from_parent();
407 /* Just keep one phone number */
408 if (count($this->telephoneNumber) && $this->telephoneNumber != ""){
409 $this->attrs['telephoneNumber']= $this->telephoneNumber[0];
410 } else {
411 $this->attrs['telephoneNumber']= array();
412 }
414 $ldap= $this->config->get_ldap_link();
415 $ldap->cd($this->dn);
416 $ldap->modify($this->attrs);
417 show_ldap_error($ldap->get_error());
419 /* Optionally execute a command after we're done */
420 $this->handle_post_events('remove');
421 }
424 }
426 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
427 ?>