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 @list($number, $timeout)= split(";", $fw);
198 $forwarder_list.= "<tr><td>";
199 $forwarder_list.= "<input name=\"fwn$nr\" size=25 align=center maxlength=60 value=\"$number\" $acl>";
200 $forwarder_list.= "</td><td>";
201 $forwarder_list.= "<input name=\"fwt$nr\" size=5 align=center maxlength=5 value=\"$timeout\" $acl>";
202 $forwarder_list.= "</td><td>";
203 $forwarder_list.= "<input type=\"submit\" value=\""._("Add")."\" name=\"add_fw$nr\" $acl>";
204 if (count($this->forwarders) > 1){
205 $forwarder_list.= "<input type=\"submit\" value=\""._("Remove")."\" name=\"remove_fw$nr\" $acl>";
206 }
207 $forwarder_list.= "</td></tr>";
208 }
209 $smarty->assign("forwarder_list", $forwarder_list);
213 /* Check box */
214 if ($this->parent->by_object['mailAccount'] &&
215 $this->parent->by_object['mailAccount']->is_account &&
216 preg_match("/M/i", $this->goFonDeliveryMode)){
217 $smarty->assign("fon_to_mail", "checked");
218 } else {
219 $smarty->assign("fon_to_mail", "");
220 }
222 if (!isset($this->parent->by_object['mailAccount'])) {
223 $smarty->assign("has_mailaccount", "false");
224 $this->has_mailAccount= false;
225 } elseif ( !$this->parent->by_object['mailAccount']->is_account){
226 $smarty->assign("has_mailaccount", "false");
227 $this->has_mailAccount= false;
228 }
230 /* Show main page */
231 $display.= $smarty->fetch(get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
232 return($display);
233 }
236 function save_object()
237 {
238 if (isset($_POST["phoneTab"])){
239 plugin::save_object();
241 /* Save checkbox */
242 if (isset($_POST['fon_to_mail'])){
243 $tmp= "[M]";
244 } else {
245 $tmp= "[]";
246 }
247 if (chkacl ($this->acl, "goFonDeliveryMode") == ""){
248 if ($this->goFonDeliveryMode != $tmp){
249 $this->is_modified= TRUE;
250 }
251 $this->goFonDeliveryMode= $tmp;
252 }
254 /* Save forwarding numbers and timeouts */
255 if (chkacl ($this->acl, "goFonForwarder") == ""){
256 foreach ($this->forwarders as $nr => $fw){
257 $tmp= $_POST["fwn$nr"].";".$_POST["fwt$nr"];
258 if ($this->forwarders[$nr] != $tmp){
259 $this->is_modified= TRUE;
260 }
261 $this->forwarders[$nr]= $tmp;
262 }
263 }
265 /* Check if mail account is active and correct the internal
266 reference to represent the current status. */
267 if ($this->parent->by_object['mailAccount']->is_account){
268 $this->has_mailAccount= TRUE;
269 }
270 }
272 }
274 function check()
275 {
276 /* Reset message array */
277 $message= array();
279 /* We need at least one phone number */
280 if (count($this->phoneNumbers) == 0){
281 $message[]= sprintf(_("You need to specify at least one phone number!"));
282 }
284 /* Check timestamps and phonenumbers */
285 foreach ($this->forwarders as $fw){
287 /* Skip empty values */
288 if ($fw == ";"){
289 continue;
290 }
292 /* Check */
293 list($number, $timeout)= split(";", $fw);
294 if (!is_phone_nr($number)){
295 $message[]= sprintf(_("The number '%s' is no valid phone number!"), $number);
296 }
297 if (!is_id($timeout)){
298 $message[]= sprintf(_("The timeout '%s' contains invalid characters!"), $timeout);
299 }
300 }
302 return ($message);
303 }
307 function save()
308 {
309 plugin::save();
311 /* goFonAccount has "mail" as must! Block if no mailaddress is specified... */
312 if (isset($this->parent->by_object['mailAccount']) &&
313 !$this->parent->by_object['mailAccount']->is_account) {
315 $this->goFonDeliveryMode= preg_replace("/M/i", "", $this->goFonDeliveryMode);
316 }
318 /* Save arrays */
319 $this->attrs['telephoneNumber']= array();
320 foreach ($this->phoneNumbers as $number){
321 $this->attrs['telephoneNumber'][]= $number;
322 }
323 $this->attrs['goFonForwarding']= array();
324 foreach ($this->forwarders as $index => $number){
325 $this->attrs['goFonForwarding'][]= "$index;$number";
326 }
328 /* Write back to ldap */
329 $ldap= $this->config->get_ldap_link();
330 $ldap->cd($this->dn);
331 $ldap->modify($this->attrs);
332 show_ldap_error($ldap->get_error());
334 /* Optionally execute a command after we're done */
335 if ($this->initially_was_account == $this->is_account){
336 if ($this->is_modified){
337 $this->handle_post_events("modify");
338 }
339 } else {
340 $this->handle_post_events("add");
341 }
343 }
346 function insert_after($entry, $nr, $list)
347 {
348 /* Is the entry free? No? Make it free... */
349 if (isset($list[$nr])) {
350 $dest= array();
351 $newidx= 0;
353 foreach ($list as $idx => $contents){
354 $dest[$newidx++]= $contents;
355 if ($idx == $nr){
356 $dest[$newidx++]= $entry;
357 }
358 }
359 } else {
360 $dest= $list;
361 $dest[$nr]= $entry;
362 }
364 return ($dest);
365 }
368 function adapt_from_template($dn)
369 {
370 plugin::adapt_from_template($dn);
372 /* Assemble phone numbers */
373 if (isset($this->attrs['telephoneNumber'])){
374 for ($i= 0; $i<$this->attrs['telephoneNumber']['count']; $i++){
375 $number= $this->attrs['telephoneNumber'][$i];
376 $this->phoneNumbers[$number]= $number;
377 }
378 }
380 /* Assemble forwarders */
381 if (isset($this->attrs['goFonForwarding'])){
382 for ($i= 0; $i<$this->attrs['goFonForwarding']['count']; $i++){
383 list($num, $v1, $v2) =split(';', $this->attrs['goFonForwarding'][$i]);
384 $this->forwarders[$num]= "$v1;$v2";
385 }
386 } else {
387 $this->forwarders= array("");
388 }
389 }
392 function remove_from_parent()
393 {
394 /* Cancel if there's nothing to do here */
395 if (!$this->initially_was_account){
396 return;
397 }
399 plugin::remove_from_parent();
401 /* Just keep one phone number */
402 if (count($this->telephoneNumber) && $this->telephoneNumber != ""){
403 $this->attrs['telephoneNumber']= $this->telephoneNumber[0];
404 } else {
405 $this->attrs['telephoneNumber']= array();
406 }
408 $ldap= $this->config->get_ldap_link();
409 $ldap->cd($this->dn);
410 $ldap->modify($this->attrs);
411 show_ldap_error($ldap->get_error());
413 /* Optionally execute a command after we're done */
414 $this->handle_post_events('remove');
415 }
418 }
420 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
421 ?>