source: trunk/phpgwapi/inc/class.sbox2.inc.php @ 2

Revision 2, 19.3 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

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