| [ Index ] |
|
Code source de eGroupWare 1.2.106-2 |
1 <?php 2 /**************************************************************************\ 3 * eGroupWare API - Select Box 2 * 4 * Written by Ralf Becker <RalfBecker@outdoor-training.de> * 5 * Class for creating select boxes for addresse, projects, array items, ... * 6 * Copyright (C) 2000, 2001 Dan Kuykendall * 7 * ------------------------------------------------------------------------ * 8 * This library is part of the eGroupWare API * 9 * http://www.egroupware.org/api * 10 * ------------------------------------------------------------------------ * 11 * This library is free software; you can redistribute it and/or modify it * 12 * under the terms of the GNU Lesser General Public License as published by * 13 * the Free Software Foundation; either version 2.1 of the License, * 14 * or any later version. * 15 * This library is distributed in the hope that it will be useful, but * 16 * WITHOUT ANY WARRANTY; without even the implied warranty of * 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * 18 * See the GNU Lesser General Public License for more details. * 19 * You should have received a copy of the GNU Lesser General Public License * 20 * along with this library; if not, write to the Free Software Foundation, * 21 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * 22 \**************************************************************************/ 23 24 /* $Id: class.sbox2.inc.php 20295 2006-02-15 12:31:25Z $ */ 25 26 if(!isset($GLOBALS['egw_info']['flags']['included_classes']['sbox'])) 27 { 28 include (EGW_API_INC . '/class.sbox.inc.php'); 29 $GLOBALS['egw_info']['flags']['included_classes']['sbox'] = True; 30 } 31 32 class sbox2 extends sbox 33 { 34 /* 35 * Function: search for an id of an db-entry, eg. an address 36 * Parameter: $name base name for all template-vars and of the submitted vars (not to conflict with other template-var-names !!!) 37 * $lang_name titel of the field 38 * $prompt for the JavaScript prompt() 39 * $id_name id of previosly selected entry 40 * $content from id (eg. 'company: lastname, givenname' for address $id) if $id != 0, or 41 * array with searchresult (id's as key), if array is empty if search was unsucsessful 42 * $multipe present a multiple selectable box instead of one selector-button 43 * Returns: array with vars to set in temaplate, the vars are: 44 * {doSearchFkt} Javascript Funktion, place somewhere in Template (before rest of the vars) 45 * {$name.'_title} button with titel $lang_name (if JS) or just $lang_name 46 * {$name} content of $id if != 0, or lang('use Button to search for').$lang_name 47 * {$name.'_nojs} searchfield + button if we have no JavaScript, else empty 48 * 49 * To use call $template->set_var(getIdSearch(...)); 50 * the template should look like {doSearchFkt} <tr><td>{XXX_title}</td><td>{XXX}</td><td>{XXX_nojs}</td></tr> (XXX is content of $name) 51 * In the submitted page the vars $query_XXX and $id_XXX are set according to what is selected, see getAddress as Example 52 */ 53 54 function sbox2() 55 { 56 $this->sbox(); // call constructor extended class 57 } 58 59 function getId($name,$lang_name,$prompt,$id_name,$content='',$note='',$multiple=False) 60 { 61 // echo "<p>getId('$name','$lang_name','$prompt',$id_name,'$content') ="; 62 $ret['doSearchFkt'] = 63 '<script language="JavaScript">'."\n". 64 " function doSearch(field,ask) {\n". 65 " field.value = prompt(ask,'');\n". 66 " if (field.value != 'null') {\n". 67 " if (field.value.length == 0)\n". 68 " field.value = '%';\n". 69 " field.form.submit();\n". 70 " } else\n". 71 " field.value = ''\n". 72 " }\n". 73 '</script>'; 74 75 $ret[$name.'_title'] = is_array($content) && count($content) ? $lang_name : 76 '<script language="JavaScript">'."\n". 77 " document.writeln('<input type=\"hidden\" name=\"query_$name\" value=\"\">');\n". 78 " document.writeln('<input type=\"button\" onClick=\"doSearch(this.form.query_$name,\'$prompt\')\" value=\"$lang_name\">');\n". 79 "</script>\n". 80 "<noscript>\n". 81 " $lang_name\n". 82 "</noscript>"; 83 84 if (is_array($content)) 85 { 86 // result from search 87 if (!count($content)) 88 { 89 // search was unsuccsessful 90 $ret[$name] = lang('no entries found, try again ...'); 91 } 92 else 93 { 94 $ret[$name.'_OK'] = ''; // flag we have something so select 95 if ($multiple) 96 { 97 $ret[$name] = '<select name="id_'.$name.'[]" size=10 multiple>'."\n"; 98 } 99 else 100 { 101 $ret[$name] = '<select name="id_'.$name.'">'."\n"; 102 } 103 while (list($id,$text) = each($content)) 104 { 105 $ret[$name] .= "<option value=\"$id\">" . $GLOBALS['egw']->strip_html($text) . "\n"; 106 } 107 $ret[$name] .= '<option value="0">'.lang('none')."\n"; 108 $ret[$name] .= '</select>'; 109 } 110 } 111 else 112 { 113 if ($id_name) 114 { 115 $ret[$name] = $content . "\n<input type=\"hidden\" name=\"id_$name\" value=\"$id_name\">"; 116 } 117 else 118 { 119 $ret[$name] = "<span class=note>$note</span>"; 120 } 121 } 122 123 $ret[$name.'_nojs'] = 124 "<noscript>\n". 125 " <input name=\"query_$name\" value=\"\" size=10> <input type=\"submit\" value=\"?\">\n". 126 "</noscript>"; 127 128 // print_r($ret); 129 return $ret; 130 } 131 132 function event2name($event) 133 { 134 if (!is_object($this->bocal)) 135 { 136 $this->bocal = createobject('calendar.bocalendar'); 137 } 138 if (!is_array($event) && (int)$event > 0) 139 { 140 $event = $this->bocal->read_entry($event); 141 } 142 if (!is_array($event)) 143 { 144 return 'not an event !!!'; 145 } 146 $name = $GLOBALS['egw']->common->show_date($this->bocal->maketime($event['start']) - $this->bocal->datetime->tz_offset); 147 $name .= ' -- ' . $GLOBALS['egw']->common->show_date($this->bocal->maketime($event['end']) - $this->bocal->datetime->tz_offset); 148 $name .= ': ' . $event['title']; 149 150 return $GLOBALS['egw']->strip_html($name); 151 } 152 153 /* 154 * Function Allows you to show and select an event from the calendar (works with and without javascript !!!) 155 * Parameters $name string with basename of all variables (not to conflict with the name other template or submitted vars !!!) 156 * $id_name id of the address for edit or 0 if none selected so far 157 * $query_name have to be called $query_XXX, the search pattern after the submit, has to be passed back to the function 158 * $multipe present a multiple selectable box instead of one selector-button 159 * On Submit $id_XXX contains the selected event (if != 0) 160 * $query_XXX search pattern if the search button is pressed by the user, or '' if regular submit 161 * Returns array with vars to set for the template, set with: $template->set_var(getEvent(...)); (see getId()) 162 * 163 * Note As query's for an event are submitted, you have to check $query_XXX if it is a search or a regular submit (!$query_string) 164 */ 165 function getEvent($name,$id_name,$query_name,$title='',$multiple=False) 166 { 167 // echo "<p>getEvent('$name',$id_name,'$query_name','$title')</p>"; 168 169 // fallback if calendar is not installed or not enabled for user 170 if (!file_exists(EGW_SERVER_ROOT.'/calendar') || !$GLOBALS['egw_info']['user']['apps']['calendar']['enabled']) 171 { 172 return array( 173 $name => "<input type=\"hidden\" name=\"id_$name\" value=\"$id_name\">\n", 174 $name.'_no_js' => '', 175 $name.'_title' => '' 176 ); 177 } 178 if ($id_name || $query_name) 179 { 180 if (!is_object($this->bocal)) 181 { 182 $this->bocal = createobject('calendar.bocalendar'); 183 } 184 if ($query_name) 185 { 186 $event_ids = $this->bocal->search_keywords($query_name); 187 $content = array(); 188 while ($event_ids && list($key,$id) = each($event_ids)) 189 { 190 $content[$id] = $this->event2name($id); 191 } 192 } 193 else 194 { 195 $event = $this->bocal->read_entry($id_name); 196 if ($event && is_array($event)) 197 { 198 $content = $this->event2name($event); 199 } 200 } 201 } 202 if (!$title) 203 { 204 $title = lang('Calendar'); 205 } 206 return $this->getId($name,$title,lang('Pattern for Search in Calendar'),$id_name,$content,lang('use Button to search for Calendarevent'),$multiple); 207 } 208 209 function addr2name($addr) 210 { 211 $name = $addr['n_family']; 212 if ($addr['n_given']) 213 { 214 $name .= ', '.$addr['n_given']; 215 } 216 else 217 { 218 if ($addr['n_prefix']) 219 { 220 $name .= ', '.$addr['n_prefix']; 221 } 222 } 223 if ($addr['org_name']) 224 { 225 $name = $addr['org_name'].': '.$name; 226 } 227 return $GLOBALS['egw']->strip_html($name); 228 } 229 230 /* 231 * Function Allows you to show and select an address from the addressbook (works with and without javascript !!!) 232 * Parameters $name string with basename of all variables (not to conflict with the name other template or submitted vars !!!) 233 * $id_name id of the address for edit or 0 if none selected so far 234 * $query_name have to be called $query_XXX, the search pattern after the submit, has to be passed back to the function 235 * $multipe present a multiple selectable box instead of one selector-button 236 * On Submit $id_XXX contains the selected address (if != 0) 237 * $query_XXX search pattern if the search button is pressed by the user, or '' if regular submit 238 * Returns array with vars to set for the template, set with: $template->set_var(getAddress(...)); (see getId()) 239 * 240 * Note As query's for an address are submitted, you have to check $query_XXX if it is a search or a regular submit (!$query_string) 241 */ 242 function getAddress($name,$id_name,$query_name,$title='',$multiple=False) 243 { 244 // echo "<p>getAddress('$name',$id_name,'$query_name','$title')</p>"; 245 if ($id_name || $query_name) 246 { 247 $contacts = createobject('phpgwapi.contacts'); 248 249 if ($query_name) 250 { 251 $addrs = $contacts->read(0,0,'',$query_name,'','DESC','org_name,n_family,n_given'); 252 $content = array(); 253 while ($addrs && list($key,$addr) = each($addrs)) 254 { 255 $content[$addr['id']] = $this->addr2name($addr); 256 } 257 } 258 else 259 { 260 list($addr) = $contacts->read_single_entry($id_name); 261 if (count($addr)) 262 { 263 $content = $this->addr2name($addr); 264 } 265 } 266 } 267 if (!$title) 268 { 269 $title = lang('Addressbook'); 270 } 271 return $this->getId($name,$title,lang('Pattern for Search in Addressbook'),$id_name,$content,lang('use Button to search for Address'),$multiple); 272 } 273 274 function addr2email($addr,$home='') 275 { 276 if (!is_array($addr)) 277 { 278 $home = substr($addr,-1) == 'h'; 279 $contacts = createobject('phpgwapi.contacts'); 280 list($addr) = $contacts->read_single_entry((int)$addr); 281 } 282 if ($home) 283 { 284 $home = '_home'; 285 } 286 287 if (!count($addr) || !$addr['email'.$home]) 288 { 289 return False; 290 } 291 292 if ($addr['n_given']) 293 { 294 $name = $addr['n_given']; 295 } 296 else 297 { 298 if ($addr['n_prefix']) 299 { 300 $name = $addr['n_prefix']; 301 } 302 } 303 $name .= ($name ? ' ' : '') . $addr['n_family']; 304 305 return $name.' <'.$addr['email'.$home].'>'; 306 } 307 308 function getEmail($name,$id_name,$query_name,$title='') 309 { 310 // echo "<p>getAddress('$name',$id_name,'$query_name','$title')</p>"; 311 if ($id_name || $query_name) 312 { 313 $contacts = createobject('phpgwapi.contacts'); 314 315 if ($query_name) 316 { 317 $addrs = $contacts->read(0,0,'',$query_name,'','DESC','org_name,n_family,n_given'); 318 $content = array(); 319 while($addrs && list($key,$addr) = each($addrs)) 320 { 321 if ($addr['email']) 322 { 323 $content[$addr['id']] = $this->addr2email($addr); 324 } 325 if ($addr['email_home']) 326 { 327 $content[$addr['id'].'h'] = $this->addr2email($addr,'_home'); 328 } 329 } 330 } 331 else 332 { 333 $content = $this->addr2email($id_name); 334 } 335 } 336 if (!$title) 337 { 338 $title = lang('Addressbook'); 339 } 340 341 return $this->getId($name,$title,lang('Pattern for Search in Addressbook'),$id_name,$content); 342 } 343 344 /* 345 * Function Allows you to show and select an project from the projects-app (works with and without javascript !!!) 346 * Parameters $name string with basename of all variables (not to conflict with the name other template or submitted vars !!!) 347 * $id_name id of the project for edit or 0 if none selected so far 348 * $query_name have to be called $query_XXX, the search pattern after the submit, has to be passed back to the function 349 * On Submit $id_XXX contains the selected address (if != 0) 350 * $query_XXX search pattern if the search button is pressed by the user, or '' if regular submit 351 * Returns array with vars to set for the template, set with: $template->set_var(getProject(...)); (see getId()) 352 * 353 * Note As query's for an address are submitted, you have to check $query_XXX if it is a search or a regular submit (!$query_string) 354 */ 355 function getProject($name,$id_name,$query_name,$title='') 356 { 357 // echo "<p>getProject('$name',$id_name,'$query_name','$title')</p>"; 358 359 // fallback if projects is not installed or not enabled for user 360 if (!file_exists(EGW_SERVER_ROOT.'/projects') || !$GLOBALS['egw_info']['user']['apps']['projects']['enabled']) 361 { 362 return array( 363 $name => "<input type=\"hidden\" name=\"id_$name\" value=\"$id_name\">\n", 364 $name.'_no_js' => '', 365 $name.'_title' => '' 366 ); 367 } 368 if ($id_name || $query_name) 369 { 370 $projects = createobject('projects.boprojects'); 371 if (!is_object($projects)) 372 { 373 return ''; 374 } 375 if ($query_name) 376 { 377 $projs = $projects->list_projects(0,0,$query_name,'','','','',0,'mains',''); 378 $content = array(); 379 while ($projs && list($key,$proj) = each($projs)) 380 { 381 $content[$proj['project_id']] = $proj['title']; 382 } 383 } 384 else 385 { 386 if ($proj = $projects->read_single_project($id_name)) 387 { 388 $content = $proj['title']; 389 // $customer_id = $proj['customer']; 390 } 391 } 392 } 393 if (!$title) 394 { 395 $title = lang('Project'); 396 } 397 398 return $this->getId($name,$title,lang('Pattern for Search in Projects'),$id_name,$content,lang('use Button to search for Project')); 399 } 400 401 /* 402 * Function: Allows to show and select one item from an array 403 * Parameters: $name string with name of the submitted var which holds the key of the selected item form array 404 * $key key(s) of already selected item(s) from $arr, eg. '1' or '1,2' or array with keys 405 * $arr array with items to select, eg. $arr = array ('y' => 'yes','n' => 'no','m' => 'maybe'); 406 * $no_lang if !$no_lang send items through lang() 407 * $options additional options (e.g. 'multiple') 408 * On submit $XXX is the key of the selected item (XXX is the content of $name) 409 * Returns: string to set for a template or to echo into html page 410 */ 411 function getArrayItem($name, $key, $arr=0,$no_lang=0,$options='',$multiple=0) 412 { 413 // should be in class common.sbox 414 if (!is_array($arr)) 415 { 416 $arr = array('no','yes'); 417 } 418 if (0+$multiple > 0) 419 { 420 $options .= ' MULTIPLE SIZE='.(0+$multiple); 421 if (substr($name,-2) != '[]') 422 { 423 $name .= '[]'; 424 } 425 } 426 $out = "<select name=\"$name\" $options>\n"; 427 428 if (is_array($key)) 429 { 430 $key = implode(',',$key); 431 } 432 while (list($k,$text) = each($arr)) 433 { 434 $out .= '<option value="'.$k.'"'; 435 if($k == $key || strstr(",$key,",",$k,")) 436 { 437 $out .= " SELECTED"; 438 } 439 $out .= ">" . ($no_lang || $text == '' ? $text : lang($text)) . "</option>\n"; 440 } 441 $out .= "</select>\n"; 442 443 return $out; 444 } 445 446 function getPercentage($name, $selected=0,$options='') 447 { 448 // reimplemented using getArrayItem 449 for ($i=0; $i <= 100; $i+=10) 450 { 451 $arr[$i] = "$i%"; 452 } 453 return $this->getArrayItem($name,$selected,$arr,1,$options); 454 } 455 456 function getPriority($name, $selected=2,$options='') 457 { 458 // reimplemented using getArrayItem 459 $arr = array('','low','normal','high'); 460 461 return $this->getArrayItem($name,$selected,$arr,0,$options); 462 } 463 464 function getAccessList($name,$selected='private',$options='') 465 { 466 // reimplemented using getArrayItem 467 $arr = array( 468 'private' => 'Private', 469 'public' => 'Global public', 470 'group' => 'Group public' 471 ); 472 473 if (strstr($selected,',')) 474 { 475 $selected = "group"; 476 } 477 478 return $this->getArrayItem($name,$selected,$arr,0,$options); 479 } 480 481 function getCountry($name='country',$selected=' ',$options='') 482 { 483 // reimplemented using getArrayItem 484 return $this->getArrayItem($name,$selected,$this->country_array,0,$options); 485 } 486 487 function form_select($name='country',$selected=' ',$options='') 488 { 489 // reimplemented using getArrayItem (stupid name!!!) 490 return getCountry($name,$selected,$options); 491 } 492 493 function accountInfo($id,$account_data=0,$longnames=0,$show_type=0) 494 { 495 if (!$id) 496 { 497 return ' '; 498 } 499 500 if (!is_array($account_data)) 501 { 502 $accounts = createobject('phpgwapi.accounts',$id); 503 $accounts->db = $GLOBALS['egw']->db; 504 $accounts->read_repository(); 505 $account_data = $accounts->data; 506 } 507 $info = $show_type ? '('.$account_data['account_type'].') ' : ''; 508 509 switch ($longnames) 510 { 511 case 2: $info .= '<'.$account_data['account_lid'].'> '; // fall-through 512 case 1: $info .= $account_data['account_firstname'].' '.$account_data['account_lastname']; break; 513 default: $info .= $account_data['account_lid']; break; 514 } 515 return $info; 516 } 517 518 /* 519 * Function: Allows to select one accountname 520 * Parameters: $name string with name of the submitted var, which holds the account_id or 0 after submit 521 * $id account_id of already selected account 522 * $longnames -1=as user prefs, 0=account_lid 1=firstname lastname 523 */ 524 function getAccount($name,$id,$longnames=-1,$type='accounts',$multiple=0,$options='') 525 { 526 $accs = $GLOBALS['egw']->accounts->get_list($type); 527 528 if ($multiple < 0) 529 { 530 $aarr[] = lang('not assigned'); 531 } 532 while ($a = current($accs)) 533 { 534 $aarr[$a['account_id']] = $longnames == -1 ? 535 $GLOBALS['egw']->common->display_fullname($a['account_lid'],$a['account_firstname'],$a['account_lastname']) : 536 $this->accountInfo($a['account_id'],$a,$longnames,$type=='both'); 537 538 next($accs); 539 } 540 return $this->getArrayItem($name,$id,$aarr,1,$options,$multiple); 541 } 542 543 function getDate($n_year,$n_month,$n_day,$date,$options='') 544 { 545 if (is_array($date)) 546 { 547 list($year,$month,$day) = $date; 548 } 549 elseif (!$date) 550 { 551 $day = $month = $year = 0; 552 } 553 else 554 { 555 $day = date('d',$date); 556 $month = date('m',$date); 557 $year = date('Y',$date); 558 } 559 return $GLOBALS['egw']->common->dateformatorder( 560 $this->getYears($n_year,$year), 561 $this->getMonthText($n_month,$month), 562 $this->getDays($n_day,$day) 563 ); 564 } 565 566 function getCategory($name,$cat_id='',$notall=False,$jscript=True,$multiple=0,$options='') 567 { 568 if (!is_object($this->cat)) 569 { 570 $this->cat = CreateObject('phpgwapi.categories'); 571 } 572 if ($jscript) 573 { 574 $options .= ' onChange="this.form.submit();"'; 575 } 576 if (0+$multiple > 0) 577 { 578 $options .= ' MULTIPLE SIZE='.(0+$multiple); 579 if (substr($name,-2) != '[]') 580 { 581 $name .= '[]'; 582 } 583 } 584 /* Setup all and none first */ 585 $cats_link = "\n<SELECT NAME=\"$name\" $options>\n"; 586 587 if (!$notall) 588 { 589 $cats_link .= '<option value=""'; 590 if ($cat_id=='all') 591 { 592 $cats_link .= ' selected'; 593 } 594 $cats_link .= '>'.lang("all")."</option>\n"; 595 } 596 597 /* Get global and app-specific category listings */ 598 $cats_link .= $this->cat->formatted_list('select','all',$cat_id,True); 599 $cats_link .= '</select>'."\n"; 600 601 return $cats_link; 602 } 603 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Feb 25 17:20:01 2007 | par Balluche grâce à PHPXref 0.7 |