[ Index ]
 

Code source de eGroupWare 1.2.106-2

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

title

Body

[fermer]

/sitemgr/inc/ -> class.module.inc.php (source)

   1  <?php
   2      /**************************************************************************\
   3      * eGroupWare SiteMgr - Web Content Management                              *
   4      * http://www.egroupware.org                                                *
   5      * --------------------------------------------                             *
   6      *  This program is free software; you can redistribute it and/or modify it *
   7      *  under the terms of the GNU General Public License as published by the   *
   8      *  Free Software Foundation; either version 2 of the License, or (at your  *
   9      *  option) any later version.                                              *
  10      \**************************************************************************/
  11  
  12      /* $Id: class.module.inc.php 20295 2006-02-15 12:31:25Z  $ */
  13  
  14  class Transformer
  15  {
  16      var $arguments;
  17  
  18  	function Transformer($arguments=array())
  19      {
  20          $this->arguments = $arguments;
  21      }
  22  
  23  	function apply_transform($title,$content)
  24      {
  25          return $content;
  26      }
  27  }
  28  
  29  
  30  class Module 
  31  {
  32      var $i18n; //flag a module must use if it wants its content to be translatable
  33      var $validation_error;
  34      var $transformer_chain;
  35      var $arguments;
  36      var $properties;
  37      var $block;
  38  
  39  	function Module()
  40      {
  41          
  42          $this->arguments = array();
  43          $this->properties = array();
  44          $this->transformer_chain = array();
  45          $this->title = "Standard module";
  46          $this->description = "Parent class that all modules should extend";
  47      }
  48  
  49  	function add_transformer(&$transformer)
  50      {
  51          $this->transformer_chain[] =& $transformer;
  52      }
  53  
  54      //before calling the functions get_user_interface, get_output,
  55      //the function set_block is used, so that we know in what scope we are, know the arguments,
  56      //and can retrieve the properties
  57      //this function can be overriden (but do not forget to call parent::set_block) in order to do some configuration
  58      //that depends on the blocks arguments
  59      //the produce argument is set when content is generated, so we can do some stuff we do not need when editing the block
  60  	function set_block(&$block,$produce=False)
  61      {
  62          if ($produce)
  63          {
  64              if (is_array($this->session) && count($this->session))
  65              {
  66                  $sessionvars = $GLOBALS['egw']->session->appsession('block[' . $block->id . ']', 'sitemgr-site');
  67              }
  68              foreach(array(
  69                  'session' => $sessionvars,
  70                  'get'     => $_GET['block'][$block->id],
  71                  'post'    => $_POST['block'][$block->id],
  72                  'cookie'  => $_COOKIE['block'][$block->id]
  73                  ) as $where => $values)
  74              {
  75                  if (is_array($this->$where))
  76                  {
  77                      foreach($this->$where as $key => $argument)
  78                      {
  79                          //contrary to $this->get, cookie and session, the argument name is the key in $this->post,
  80                          //because this array also defines the form element
  81                          if ($where == 'post') $argument = $key;
  82  
  83                          if (isset($values[$argument]) && !$block->addcontents)
  84                          {
  85                              $block->arguments[$argument] = $values[$argument];
  86                          }
  87                      }
  88                  }
  89              }
  90          }
  91          $this->block =& $block;
  92      }
  93  
  94  	function link($modulevars=array(),$extravars=array(),$addcontent='')
  95      {
  96          if (is_array($modulevars))
  97          {
  98              foreach($modulevars as $key => $value)
  99              {
 100                  $extravars['block['. $this->block->id  .'][' . $key . ']'] = $value;
 101              }
 102          }
 103          if ($GLOBALS['page']->name)
 104          {
 105              $extravars['page_name'] = $GLOBALS['page']->name;
 106          }
 107          elseif ($GLOBALS['page']->cat_id)
 108          {
 109              $extravars['category_id'] = $GLOBALS['page']->cat_id;
 110          }
 111          elseif ($GLOBALS['page']->toc)
 112          {
 113              $extravars['toc'] = 1;
 114          }
 115          elseif ($GLOBALS['page']->index)
 116          {
 117              $extravars['index'] = 1;
 118          }
 119          if (is_array($addcontent))
 120          {
 121              $add_cont = $GLOBALS['egw']->session->appsession('addcontent','sitemgr');
 122              $add_counter = is_array($add_cont) ? count($add_cont) : 0;
 123              $new_add = array_pop($addcontent);
 124              $extravars['addcontent'] = '';
 125              while($new_add)
 126              {
 127                  $extravars['addcontent'] .= $add_counter;
 128                  $add_cont[$add_counter] = $new_add;
 129                  if($new_add['page']) $extravars['page_name'] = $new_add['page'];
 130                  
 131                  $new_add = array_pop($addcontent);
 132                  if($new_add)
 133                  {
 134                      $extravars['addcontent'] .= ',';
 135                      $add_counter = $add_counter + 1;
 136                  }
 137              }
 138              $GLOBALS['egw']->session->appsession('addcontent','sitemgr',$add_cont);
 139          }
 140          return sitemgr_link($extravars);
 141      }
 142  
 143  	function find_template_dir()
 144      {
 145          $templaterootformat = $GLOBALS['sitemgr_info']['site_dir']. SEP . 'templates' . SEP . '%s' . SEP . 'modules' . SEP . $this->block->module_name;
 146          $themetemplatedir = sprintf($templaterootformat,$GLOBALS['sitemgr_info']['themesel']);
 147          if (is_dir($themetemplatedir))
 148          {
 149              return $themetemplatedir;
 150          }
 151          else
 152          {
 153              return sprintf($templaterootformat,'default');
 154          }
 155      }
 156  
 157  	function get_properties($cascading=True)
 158      {
 159          if ($this->properties)
 160          {
 161              if ($cascading)
 162              {
 163                  return $GLOBALS['Common_BO']->modules->getcascadingmoduleproperties(
 164                      $this->block->module_id,
 165                      $this->block->area,
 166                      $this->block->cat_id,
 167                      $this->block->module_name
 168                  );
 169              }
 170              else
 171              {
 172                  return $GLOBALS['Common_BO']->modules->getmoduleproperties(
 173                      $this->block->module_id,
 174                      $this->block->area,
 175                      $this->block->cat_id
 176                  );
 177              }
 178          }
 179          else
 180          {
 181              return False;
 182          }
 183      }
 184  
 185  	function get_user_interface()
 186      {
 187          //if you override this function you can fetch properties and adapt the interface accordingly
 188          //$properties = $this->get_properties();
 189          $interface = array();
 190          reset($this->arguments);
 191          while (list($key,$input) = @each($this->arguments))
 192          {
 193              $elementname = 'element[' . $this->block->version . ']';
 194              $elementname .= ($input['i18n'] ? ('[i18n][' .$key . ']') : ('[' .$key . ']'));
 195              //arrays of input elements are only implemented for the user interface
 196              if ($input['type'] == 'array')
 197              {
 198                  $i = 0;
 199                  while (isset($input[$i]))
 200                  {
 201                      $element['label'] = $input[$i]['label'];
 202                      $element['form'] = $this->build_input_element($input[$i],$this->block->arguments[$key][$i],$elementname.'[]');
 203                      $interface[] = $element;
 204                      $i++;
 205                  }
 206              }
 207              else
 208              {
 209                  $element['label'] = $input['label'];
 210                  $element['large'] = $input['large'];    // show label above instead beside content
 211                  $element['form'] = $this->build_input_element($input,$this->block->arguments[$key],$elementname);
 212                  $interface[] = $element;
 213              }
 214          }
 215          return $interface;
 216      }
 217  
 218  
 219  	function get_translation_interface($fromblock,$toblock)
 220      {
 221          //if you override this function you can fetch properties and adapt the interface accordingly
 222          //$properties = $this->get_properties();
 223          $interface = array();
 224          reset($this->arguments);
 225          while (list($key,$input) = @each($this->arguments))
 226          {
 227              if ($input['i18n'])
 228              {
 229                  $elementname = 'element[' . $this->block->version . '][i18n][' .$key . ']';
 230                  //arrays of input elements are only implemented for the user interface
 231                  if ($input['type'] == 'array')
 232                  {
 233                      $i = 0;
 234                      while (isset($input[$i]))
 235                      {
 236                          $element['label'] = $input[$i]['label'];
 237                          $element['form'] = $this->build_input_element($input[$i],$toblock->arguments[$key][$i],$elementname.'[]');
 238                          $element['value'] = $fromblock->arguments[$key][$i];
 239                          $interface[] = $element;
 240                          $i++;
 241                      }
 242                  }
 243                  else
 244                  {
 245                      $element['label'] = $input['label'];
 246                      $element['form'] = $this->build_input_element($input,$toblock->arguments[$key],$elementname);
 247                      $element['value'] = $fromblock->arguments[$key];
 248                      $interface[] = $element;
 249                  }
 250              }
 251          }
 252          return $interface;
 253      }
 254  
 255  
 256  	function get_admin_interface()
 257      {
 258          $properties = $this->get_properties(False);
 259          $interface = array();
 260          while (list($key,$input) = @each($this->properties))
 261          {
 262              $elementname = 'element[' .$key . ']';
 263              $element['label'] = $input['label'];
 264              $element['form'] = $this->build_input_element($input,$properties[$key],$elementname);
 265              $interface[$key] = $element;
 266          }
 267          return $interface;
 268      }
 269  
 270  	function build_post_element($key,$default=False)
 271      {
 272          return $this->build_input_element(
 273              $this->post[$key],
 274              ($default !== False) ? $default : $this->block->arguments[$key],
 275              ('block[' . $this->block->id  . '][' . $key . ']')
 276          );
 277      }
 278  
 279      //this function strips html and curly braces from the default values of the input elements
 280      //the former is necessary for valid input forms, the latter would hurt phpgw's template
 281  	function escape_default(&$default)
 282      {
 283          $trans = array('{' => '&#123;', '}' => '&#125;');
 284          if (is_array($default))
 285          {
 286              reset($default);
 287              while (list($key,$val) = each($default))
 288              {
 289                  $this->escape_default($data[$key]);
 290              }
 291          }
 292          else
 293          {
 294              $default = strtr($GLOBALS['egw']->strip_html($default),$trans);
 295          }
 296      }
 297  
 298  	function build_input_element($input,$default,$elementname)
 299      {
 300          //echo "<p>module::build_input_element(".print_r($input,True).",'$default','$elementname')</p>";
 301          if (empty($default) && isset($input['default']))
 302          {
 303              $default = $input['default'];
 304          }
 305          if ($default && $input['type'] != 'htmlarea')    // htmlarea does its own escape !!!
 306          {
 307              $this->escape_default($default);
 308          }
 309          $paramstring = '';
 310          while (list($param,$value) = @each($input['params']))
 311          {
 312              $paramstring .= $param . '="' . $value . '" ';
 313          }
 314          $inputdef = $paramstring . ' name="' . $elementname . ($input['multiple'] ? '[]' : '') . '"';
 315          switch($input['type'])
 316          {
 317              case 'htmlarea':
 318                  if (!is_object($GLOBALS['egw']->html))
 319                  {
 320                      $GLOBALS['egw']->html =& CreateObject('phpgwapi.html');
 321                  }
 322  //                 return $GLOBALS['egw']->html->htmlarea($elementname,$default,$input['params']['style'],false,$input['params']['plugins']);
 323                  return $GLOBALS['egw']->html->tinymce($elementname,$default,$input['params']['style'],$input['params']['plugins']);
 324  
 325  //                 $GLOBALS['Common_BO']->sites->current_site['site_url']
 326              case 'textarea':
 327                  return '<textarea ' . $inputdef . '>' . $default . '</textarea>';
 328              case 'textfield':
 329                  return '<input type="text" ' . $inputdef . ' value ="' . $default . '" />';
 330              case 'checkbox':
 331                  return '<input type="checkbox" ' . $inputdef . ($default ? 'checked="1"' :'') . '" />';
 332              case 'select':
 333                  $select = '<select ' .($input['multiple'] ? 'multiple="1"'.($input['multiple'] > 1 ? ' size="'.$input['multiple'].'"' : '') : '') . $inputdef . '>';
 334                  foreach ($input['options'] as $value => $display)
 335                  {
 336                      $title = '';
 337                      if (is_array($display))
 338                      {
 339                          $title = @$display['title'] ? ' title="'.htmlspecialchars($display['titel']).'"' : '';
 340                          $display = $display['name'];
 341                      }
 342                      $selected='';
 343                      if
 344                      (
 345                          ($input['multiple'] && is_array($default) && in_array($value,$default)) ||
 346                          (!$input['multiple'] && ($default == $value))
 347                      )
 348                      {
 349                          $selected = ' selected="1"';
 350                      }
 351                      $select .= '<option value="'. $value . '"' . $selected . $title . '>' . $display . '</option>';
 352                  }
 353                  $select .= '</select>';
 354                  return $select;
 355              case 'submit':
 356                  return '<input type="submit" ' . $inputdef .' value ="' . $input['value'] . '" />';
 357              case 'image':
 358                  return '<input type="image" ' . $inputdef .' src ="' . $input['src'] . '" />';
 359          }
 360      }
 361  
 362  	function validate(&$data)
 363      {
 364          return true;
 365      }
 366  
 367  	function validate_properties(&$data)
 368      {
 369          return true;
 370      }
 371  
 372      //never call get_content directly, get_output takes care of passing it the right arguments
 373  	function get_content(&$arguments,$properties)
 374      {
 375  
 376      }
 377  
 378  	function get_output($type='html')
 379      {
 380          $content= $this->get_content($this->block->arguments,$this->get_properties());
 381          if (!$content)
 382          {
 383              return '';
 384          }
 385          if ($type == 'raw')
 386          {
 387              return $content;
 388          }
 389          else
 390          {
 391              for ( $i = 0; $i < count( $this->transformer_chain ); ++$i )
 392              {
 393                  $content = $this->transformer_chain[$i]->apply_transform($this->block->title,$content,$this->block);
 394              }
 395              //store session variables
 396              if ($this->session)
 397              {
 398                  reset($this->session);
 399                  while (list(,$argument) = each($this->session))
 400                  {
 401                      if (isset($this->block->arguments[$argument]))
 402                      {
 403                          $sessionarguments[$argument] = $this->block->arguments[$argument];
 404                      }
 405                  }
 406                  $GLOBALS['egw']->session->appsession('block[' . $this->block->id . ']','sitemgr-site',$sessionarguments);
 407              }
 408              return $content;
 409          }
 410      }
 411  }


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