[ Index ]
 

Code source de eGroupWare 1.2.106-2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/etemplate/inc/ -> class.link_widget.inc.php (source)

   1  <?php
   2      /**************************************************************************\
   3      * eGroupWare - eTemplate Extension - Link Widgets / UI for the link class  *
   4      * http://www.egroupware.org                                                *
   5      * Written by Ralf Becker <RalfBecker@outdoor-training.de>                  *
   6      * --------------------------------------------                             *
   7      *  This program is free software; you can redistribute it and/or modify it *
   8      *  under the terms of the GNU General Public License as published by the   *
   9      *  Free Software Foundation; either version 2 of the License, or (at your  *
  10      *  option) any later version.                                              *
  11      \**************************************************************************/
  12  
  13      /* $Id: class.link_widget.inc.php 20202 2006-01-06 04:50:11Z ralfbecker $ */
  14  
  15      /**
  16       * eTemplate Extension: several widgets as user-interface for the link-class
  17       *
  18       * All widgets use the link-registry, to "know" which apps use popups (and what size).
  19       * If run in a popup and the app uses no popups, a target will be set, to open a new full decorated window.
  20       *
  21       * The class contains the following widgets:
  22       * - link: Show a link to one linked entry specified by an array with keys app, id and optional title and help-message
  23       * - link-to: Widget to create links to an other entries of link-aware apps
  24       *    If an id was set, this widgets creats the links without further interaction with the calling code.
  25       *    If the entry does not yet exist, the widget returns an array with the new links in the id. After the
  26       *    entry was successful create, bolink::link($app,$new_id,$arr) has to be called to create the links!
  27       * - link-list: Widget to shows the links to an entry in a table with an unlink icon for each link
  28       * - link-string: comma-separated list of link-titles with a link to its view method, value is like get_links() 
  29       *    or array with keys to_app and to_id (widget calls then get_links itself)
  30       * - link-add:    Add a new entry of the select app, which is already linked to a given entry
  31       *
  32       *<code>
  33       * $content[$name] = array(
  34       *   'to_app'       => // I  string appname of the entry to link to
  35       *   'to_id'        => // IO int id of the entry to link to, for new entries 0, returns the array with new links
  36       *    // the following params apply only for the link-to widget!
  37       *   'no_files'     => // I  boolean suppress attach-files, default no
  38       *   'search_label' => // I  string label to use instead of search
  39       *   'link_label'   => // I  string label for the link button, default 'Link'
  40       *  // optional only for the link-add widget
  41       *   'extra'        => // I  array with extra parameters, eg. array('cat_id' => 15)
  42       * );
  43       *</code>
  44       *
  45       * This widget is independent of the UI as it only uses etemplate-widgets and has therefor no render-function.
  46       *
  47       * @package etemplate
  48       * @subpackage extensions
  49       * @author Ralf Becker <RalfBecker@outdoor-training.de>
  50       * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
  51       */
  52      class link_widget
  53      {
  54          /** 
  55           * @var array exported methods of this class
  56           */
  57          var $public_functions = array(
  58              'pre_process' => True,
  59              'post_process' => True
  60          );
  61          /**
  62           * @var array availible extensions and there names for the editor
  63           */
  64          var $human_name = array(
  65              'link'        => 'Link',
  66              'link-to'     => 'LinkTo',
  67              'link-list'   => 'LinkList',
  68              'link-string' => 'LinkString',
  69              'link-add'    => 'LinkEntry',
  70          );
  71          /**
  72           * @var boolean $debug switches debug-messages on and off
  73           */
  74          var $debug = False;
  75          /**
  76           * @var object $link reference to the link class
  77           */
  78          var $link;
  79  
  80          /**
  81           * Constructor of the extension
  82           *
  83           * @param string $ui '' for html
  84           */
  85  		function link_widget($ui)
  86          {
  87              if (!is_object($GLOBALS['egw']->link))
  88              {
  89                  $GLOBALS['egw']->link =& CreateObject('infolog.bolink');
  90              }
  91              $this->link =& $GLOBALS['egw']->link;
  92          }
  93  
  94          /**
  95           * pre-processing of the extension
  96           *
  97           * This function is called before the extension gets rendered
  98           *
  99           * @param string $name form-name of the control
 100           * @param mixed &$value value / existing content, can be modified
 101           * @param array &$cell array with the widget, can be modified for ui-independent widgets 
 102           * @param array &$readonlys names of widgets as key, to be made readonly
 103           * @param mixed &$extension_data data the extension can store persisten between pre- and post-process
 104           * @param object &$tmpl reference to the template we belong too
 105           * @return boolean true if extra label is allowed, false otherwise
 106           */
 107  		function pre_process($name,&$value,&$cell,&$readonlys,&$extension_data,&$tmpl)
 108          {
 109              $type = $cell['type'];
 110              $help = $cell['help'] ? ($value['help'] ? $value['help'] : $cell['help']) : lang('view this linked entry in its application');
 111  
 112              if (($type == 'link-to' || $type == 'link-add') && ($cell['readonly'] || $readonlys))
 113              {
 114                  //echo "<p>link-to is readonly, cell=".print_r($cell,true).", readonlys=".print_r($readonlys)."</p>\n";
 115                  // readonly ==> omit the whole widget
 116                  $cell = $tmpl->empty_cell();
 117                  return;
 118              }
 119              if (!is_array($value) && $type != 'link-string' && $type != 'link')
 120              {
 121                  $value = array(
 122                      'to_id' => $value,
 123                      'to_app' => $GLOBALS['egw_info']['flags']['currentapp']
 124                  );
 125              }
 126              if ($this->debug)
 127              {
 128                  echo "<p>link_widget::pre_process($name,$value,".print_r($cell,true).",$readonlys,,)</p>\n";
 129                  echo "<p>start: $cell[type][$name]::pre_process: value ="; _debug_array($value);
 130                  echo "extension_data[$cell[type]][$name] ="; _debug_array($extension_data);
 131              }
 132              switch ($type = $cell['type'])
 133              {
 134              case 'link':
 135                  $cell['readonly'] = True;    // set it readonly to NOT call our post_process function
 136                  $cell['no_lang'] = 1;
 137                  $link = $target = $popup = '';
 138                  if ($value['app'] && $value['id'])
 139                  {
 140                      $view = $this->link->view($value['app'],$value['id']);
 141                      $link = $view['menuaction']; unset($view['menuaction']);
 142                      foreach($view as $var => $val)
 143                      {
 144                          $link .= '&'.$var.'='.$val;
 145                      }
 146                      if (!($popup = $this->link->is_popup($value['app'],'view')) &&
 147                          $GLOBALS['egw_info']['etemplate']['output_mode'] == 2)    // we are in a popup
 148                      {
 149                          $target = '_blank';
 150                      }
 151                      if (!$cell['help'])
 152                      {
 153                          $cell['help'] = $help;
 154                          $cell['no_lang'] = 2;
 155                      }
 156                  }
 157                  elseif (!$value['title'])
 158                  {
 159                      $cell = $tmpl->empty_cell();
 160                      $cell['readonly'] = True;    // set it readonly to NOT call our post_process function
 161                      return;            
 162                  }
 163                  $cell['type'] = 'label';
 164                  //  size: [b[old]][i[talic]],[link],[activate_links],[label_for],[link_target],[link_popup_size]
 165                  list($cell['size']) = explode(',',$cell['size']);
 166                  $cell['size'] .= ','.$link.',,,'.$target.','.$popup;
 167                  $value = $value['title'] ? $value['title'] : $this->link->title($value['app'],$value['id']);
 168                  return true;
 169  
 170              case 'link-string':
 171                  $str = '';
 172                  if ($values['to_id'] && $values['to_app'])
 173                  {
 174                      $values = $this->link->get_links($values['to_app'],$values['to_id']);
 175                  }
 176                  if (is_array($value))
 177                  {
 178                      foreach ($value as $link)
 179                      {
 180                          $options .= " onMouseOver=\"self.status='".addslashes($tmpl->html->htmlspecialchars($help))."'; return true;\"";
 181                          $options .= " onMouseOut=\"self.status=''; return true;\"";
 182                          if (($popup = $this->link->is_popup($link['app'],'view')))
 183                          {
 184                              list($w,$h) = explode('x',$popup);
 185                              $options = ' onclick="window.open(this,this.target,\'width='.(int)$w.',height='.(int)$h.',location=no,menubar=no,toolbar=no,scrollbars=yes,status=yes\'); return false;"';
 186                          }
 187                          elseif ($GLOBALS['egw_info']['etemplate']['output_mode'] == 2)    // we are in a popup
 188                          {
 189                              $options = ' target="_blank"';
 190                          }
 191                          $str .= ($str !== '' ? ', ' : '') . $tmpl->html->a_href(
 192                              $tmpl->html->htmlspecialchars($this->link->title($link['app'],$link['id'])),
 193                              '/index.php',$this->link->view($link['app'],$link['id'],$link),$options);
 194                      }
 195                  }
 196                  $cell['type'] = 'html';
 197                  $cell['readonly'] = True;    // set it readonly to NOT call our post_process function
 198                  $value = $str;
 199                  return True;
 200  
 201              case 'link-add':
 202                  $apps = $this->link->app_list($type == 'link-add' ? 'add' : '');
 203                  if (!$apps)    // cant do an add without apps or already created entry
 204                  {
 205                      $cell = $tmpl->empty_cell();
 206                      return;
 207                  }
 208                  asort($apps);    // sort them alphabetic
 209                  $value['options-app'] = array();
 210                  foreach($apps as $app => $label)
 211                  {
 212                      $link = $GLOBALS['egw']->link('/index.php',$this->link->add($app,$value['to_app'],$value['to_id'])+
 213                          (is_array($value['extra']) ? $value['extra'] : array()));
 214                      if (($popup = $this->link->is_popup($app,'add')))
 215                      {
 216                          list($w,$h) = explode('x',$popup);
 217                          $action = "window.open('$link','_blank','width=$w,height=$h,location=no,menubar=no,toolbar=no,scrollbars=yes,status=yes');";
 218                      }
 219                      else
 220                      {
 221                          $action = "location.href = '$link';";
 222                      }
 223                      $value['options-app'][$action] = $label;
 224                  }
 225                  $tpl =& new etemplate('etemplate.link_widget.add');
 226                  break;
 227  
 228              case 'link-to':
 229                  $value['msg'] = '';
 230                  if ($value['button'] == 'search' && count($ids = $this->link->query($value['app'],$value['query'])))
 231                  {
 232                      $extension_data['app'] = $value['app'];
 233  
 234                      $value = $extension_data;
 235                      $value['options-id'] = $ids;
 236                      $value['remark'] = '';
 237  
 238                      $tpl =& new etemplate('etemplate.link_widget.create');
 239                      if ($value['link_label'])
 240                      {
 241                          $tpl->set_cell_attribute('create','label',$value['link_label']);
 242                      }
 243                  }
 244                  else
 245                  {
 246                      // error from search or upload
 247                      $value['msg'] = $value['button'] == 'search' ? lang('Nothing found - try again !!!') : $extension_data['msg'];
 248  
 249                      if (!$value['button'])
 250                      {
 251                          $extension_data = $value;
 252                      }
 253                      $value = array_merge($extension_data,$value);
 254                      $value['options-app'] = $this->link->app_list();
 255                      asort($value['options-app']);    // sort them alphabetic
 256  
 257                      $tpl =& new etemplate('etemplate.link_widget.search');
 258                      if ($value['search_label'])
 259                      {
 260                          $tpl->set_cell_attribute('app','label',$value['search_label']);
 261                      }
 262                      $tpl->set_cell_attribute('comment','onchange',"set_style_by_class('*','hide_comment','display',this.checked ? 'block' : 'none');");
 263                      unset($value['comment']);
 264                  }
 265                  break;
 266  
 267              case 'link-list':
 268                  $app = $value['to_app'];
 269                  $id  = isset($extension_data['to_id']) ? $extension_data['to_id'] : $value['to_id'];
 270                  if ($this->debug)
 271                  {
 272                      echo "<p>link-list-widget[$name].preprocess: value="; _debug_array($value);
 273                  }
 274                  if (!isset($value['title']))
 275                  {
 276                      $value['title'] = $this->link->title($app,$id);
 277                  }
 278                  $links = $this->link->get_links($app,$id);
 279                  $value['anz_links'] = count($links);
 280                  $extension_data = $value;
 281  
 282                  if (!count($links))
 283                  {
 284                      $cell = $tmpl->empty_cell();
 285                      $value = '';
 286                      return True;
 287                  }
 288                  $tpl =& new etemplate('etemplate.link_widget.list');
 289                  $tpl->data[0]['A'] = $tmpl->data[0]['A'];    // set width of first col like the tmpl. calling us
 290                  for($row=$tpl->rows-1; list(,$link) = each($links); ++$row)
 291                  {
 292                      $value[$row] = $link;
 293                      $value[$row]['title'] = $this->link->title($link['app'],$link['id'],$link);
 294                      if (!is_array($link['id']))
 295                      {
 296                          $value[$row]['view']  = $this->link->view($link['app'],$link['id'],$link);
 297                          if (!($value[$row]['popup'] = $this->link->is_popup($link['app'],'view')) &&
 298                              $GLOBALS['egw_info']['etemplate']['output_mode'] == 2)    // we are in a popup
 299                          {
 300                              $value[$row]['target'] = '_blank';        // we create a new window as the linked page is no popup
 301                          }
 302                      }
 303                      if ($link['app'] == $this->link->vfs_appname)
 304                      {
 305                          $value[$row]['label'] = 'Delete';
 306                          $value[$row]['help'] = lang('Delete this file');
 307                      }
 308                      else
 309                      {
 310                          $value[$row]['label'] = 'Unlink';
 311                          $value[$row]['help'] = lang('Remove this link (not the entry itself)');
 312                      }
 313                  }
 314                  break;
 315              }
 316              $cell['size'] = $cell['name'];
 317              $cell['type'] = 'template';
 318              $cell['name'] = $tpl->name;
 319              $cell['obj'] =& $tpl;
 320              // keep the editor away from the generated tmpls
 321              $tpl->no_onclick = true;            
 322  
 323              if ($this->debug)
 324              {
 325                  echo "<p>end: $type"."[$name]::pre_process: value ="; _debug_array($value);
 326              }
 327              return True;    // extra Label is ok
 328          }
 329  
 330          /**
 331           * postprocessing method, called after the submission of the form
 332           *
 333           * It has to copy the allowed/valid data from $value_in to $value, otherwise the widget
 334           * will return no data (if it has a preprocessing method). The framework insures that
 335           * the post-processing of all contained widget has been done before.
 336           *
 337           * Only used by select-dow so far
 338           *
 339           * @param string $name form-name of the widget
 340           * @param mixed &$value the extension returns here it's input, if there's any
 341           * @param mixed &$extension_data persistent storage between calls or pre- and post-process
 342           * @param boolean &$loop can be set to true to request a re-submision of the form/dialog
 343           * @param object &$tmpl the eTemplate the widget belongs too
 344           * @param mixed &value_in the posted values (already striped of magic-quotes)
 345           * @return boolean true if $value has valid content, on false no content will be returned!
 346           */
 347  		function post_process($name,&$value,&$extension_data,&$loop,&$tmpl,$value_in)
 348          {
 349              //echo "<p>link_widget::post_process('$name',value=".print_r($value,true).",ext=".print_r($extension_data,true).",$loop,,value_in=".print_r($value_in,true)."</p>\n";
 350  
 351              $buttons = array('search','create','new','upload','attach');
 352              while (!$button && list(,$bname) = each($buttons))
 353              {
 354                  $button = $value[$bname] ? $bname : '';
 355              }
 356              if (is_array($value['unlink']))
 357              {
 358                  $button = 'unlink';
 359                  list($unlink) = @each($value['unlink']);
 360              }
 361              unset($value[$button]);
 362              unset($value['msg']);
 363              unset($extension_data['msg']);
 364  
 365              if (is_array($extension_data))
 366              {
 367                  $value = is_array($value) ? array_merge($extension_data,$value) : $extension_data;
 368              }
 369              if ($button && $this->debug)
 370              {
 371                  echo "<p>start: link_widget[$name]::post_process: button='$button', unlink='$unlink', value ="; _debug_array($value);
 372              }
 373              switch ($button)
 374              {
 375                  case 'create':
 376                      if ($value['to_app'])                        // make the link
 377                      {
 378                          $link_id = $this->link->link($value['to_app'],$value['to_id'],
 379                              $value['app'],$value['id'],$value['remark']);
 380                          $value['remark'] = '';
 381  
 382                          if (isset($value['primary']) && !$value['anz_links'] )
 383                          {
 384                              $value['primary'] = $link_id;
 385                          }
 386                      }
 387                      // fall-trough
 388                  case 'search':
 389                  case 'new':
 390                      $extension_data = $value;
 391                      $loop = True;
 392                      break;
 393  
 394                  case 'attach':
 395                      if (is_array($value['file']) && $value['to_app'] &&
 396                          !empty($value['file']['tmp_name']) && $value['file']['tmp_name'] != 'none')
 397                      {
 398                          if (!$value['to_id'] || is_array($value['to_id']))    // otherwise the webserver deletes the file
 399                          {
 400                              move_uploaded_file($value['file']['tmp_name'],$value['file']['tmp_name'].'+');
 401                              $value['file']['tmp_name'] .= '+';
 402                          }
 403                          $link_id = $this->link->link($value['to_app'],$value['to_id'],
 404                              $this->link->vfs_appname,$value['file'],$value['remark']);
 405                          $value['remark'] = '';
 406  
 407                          if (isset($value['primary']) && !$value['anz_links'] )
 408                          {
 409                              $value['primary'] = $link_id;
 410                          }
 411                          unset($value['file']);
 412                      }
 413                      else
 414                      {
 415                          $value['msg'] = 'You need to select a file first!';
 416                      }
 417                      $extension_data = $value;
 418                      $loop = True;
 419                      break;
 420  
 421                  case 'unlink':
 422                      if ($this->debug)
 423                      {
 424                          //echo "<p>unlink(link-id=$unlink,$value[to_app],$value[to_id])</p>\n";
 425                          if (is_array($value['to_id'])) _debug_array($value['to_id']);
 426                      }
 427                      $this->link->unlink2($unlink,$value['to_app'],$value['to_id']);
 428                      if (is_array($value['to_id']))
 429                      {
 430                          $extension_data['to_id'] = $value['to_id'];    // else changes from unlink get lost
 431                      }
 432                      $loop = True;
 433                      break;
 434              }
 435              $value['button'] = $button;
 436  
 437              if ($this->debug)
 438              {
 439                  echo "<p>end: link_widget[$name]::post_process: value ="; _debug_array($value);
 440              }
 441              return True;
 442          }
 443      }


Généré le : Sun Feb 25 17:20:01 2007 par Balluche grâce à PHPXref 0.7