[ 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.Content_SO.inc.php (source)

   1  <?php
   2      /**************************************************************************\
   3      * eGroupWare SiteMgr - Web Content Management                              *
   4      * http://www.egroupware.org                                                *
   5      * Rewritten with the new db-functions and documented                       *
   6      * by RalfBecker-AT-outdoor-training.de                                     *
   7      * --------------------------------------------                             *
   8      *  This program is free software; you can redistribute it and/or modify it *
   9      *  under the terms of the GNU General Public License as published by the   *
  10      *  Free Software Foundation; either version 2 of the License, or (at your  *
  11      *  option) any later version.                                              *
  12      \**************************************************************************/
  13  
  14      /* $Id: class.Content_SO.inc.php 20295 2006-02-15 12:31:25Z  $ */
  15  
  16      require_once (EGW_SERVER_ROOT . '/sitemgr/inc/class.Block_SO.inc.php');
  17  
  18      class Content_SO
  19      {
  20          var $db;  /* @var $db db */
  21          // Name of the differnt tables, initialised in the constructor
  22          var $content_table,$content_lang_table,$blocks_table,$blocks_lang_table,$modules_table,$modules_lang_table;
  23  
  24          /**
  25           * Constructor of the content-storage-object
  26           */
  27  		function Content_SO()
  28          {
  29              $this->db = clone($GLOBALS['egw']->db); 
  30              $this->db->set_app('sitemgr');
  31              foreach(array('content','content_lang','blocks','blocks_lang','modules','modules_lang') as $name)
  32              {
  33                  $var = $name.'_table';
  34                  $this->$var = 'egw_sitemgr_'.$name; // only reference to the db-prefix
  35              }
  36          }
  37  
  38          /**
  39           * Add a new block
  40           * @param object $block block-object
  41           * @return int the new block-id
  42           */
  43  		function addblock($block)
  44          {
  45              $this->db->insert($this->blocks_table,array(
  46                  'area'    => $block->area,
  47                  'module_id' => $block->module_id,
  48                  'page_id' => $block->page_id,
  49                  'cat_id'  => $block->cat_id,
  50                  'sort_order'=> 0,
  51                  'viewable'  => 0,
  52              ),False,__LINE__,__FILE__);
  53  
  54              return $this->db->get_last_insert_id($this->blocks_table,'block_id');
  55          }
  56  
  57          /**
  58           * Updates the scope of a block (for which cat's and pages it gets displayed)
  59           * @param int $blockid id of the block
  60           * @param int $cat_id cat_id
  61           * @param int $page_id page-id or 0 for a cat- or site-wide block
  62           * @param int number of rows updated, eg. 1=success
  63           * @return int changed rows
  64           */
  65  		function updatescope($blockid,$cat_id,$page_id)
  66          {
  67              $this->db->update($this->blocks_table,array(
  68                  'cat_id'  => $cat_id,
  69                  'page_id' => $page_id,
  70              ),array(
  71                  'block_id'  => $blockid,
  72              ),__LINE__,__FILE__);
  73  
  74              return $this->db->affected_rows();
  75          }
  76  
  77          /**
  78          * Creates a new version (in state draft) for a block
  79          * @return boolean true on success
  80          * @param int $blockid block-id
  81          */
  82  		function createversion($blockid)
  83          {
  84              return $this->db->insert($this->content_table,array(
  85                  'block_id'  => $blockid,
  86                  'state'   =>  SITEMGR_STATE_DRAFT,
  87              ),False,__LINE__,__FILE__);
  88          }
  89  
  90          /**
  91           * Deletes a version (of a block content), version-id's are unique and NOT block-specific
  92           * @param int $versionid version-id
  93           * @return boolean true on success, false otherwise
  94           */
  95  		function deleteversion($versionid)
  96          {
  97              if (!$this->db->delete($this->content_table,array('version_id'=>$versionid),__LINE__,__FILE__))
  98              {
  99                  return False;
 100              }
 101              return $this->db->delete($this->content_lang_table,array('version_id'=>$versionid),__LINE__,__FILE__);
 102          }
 103  
 104          /**
 105           * Retrives the block-id from a version-id
 106           * @param int $versionid version-id
 107           * @return int block-id or false
 108           */
 109  		function getblockidforversion($versionid)
 110          {
 111              $this->db->select($this->content_table,'block_id',array('version_id'=>$versionid),__LINE__,__FILE__);
 112              
 113              return $this->db->next_record() ? $this->db->f('block_id') : false;
 114          }
 115  
 116          /**
 117           * Deletes a block identified by its block-id
 118           * @param int block-id
 119           * @return boolean true on success, flase otherwise
 120           */
 121  		function removeblock($blockid)
 122          {
 123              if (!$this->db->delete($this->blocks_table,array('block_id'=>$blockid),__LINE__,__FILE__))
 124              {
 125                  return false;
 126              }
 127              return $this->db->delete($this->blocks_lang_table,array('block_id'=>$blockid),__LINE__,__FILE__);
 128          }
 129  
 130          /**
 131           * Retrives all block's for a given scope (cat,page)
 132           * @param int $cat_id cat-id
 133           * @param int $page_id page-id or 0 for cat- or site-wide blocks
 134           * @return array of block-objects, with only block-id, module-id & -name and content-area set
 135           */
 136          function &getblocksforscope($cat_id,$page_id)
 137          {
 138              $sql = $this->db->expression($this->blocks_table,"SELECT t1.block_id AS id,t1.module_id,module_name,area FROM $this->blocks_table AS t1,$this->modules_table AS t2 WHERE t1.module_id = t2.module_id AND ",array(
 139                  'cat_id'  => $cat_id,
 140                  'page_id' => $page_id,
 141              ),' ORDER BY sort_order');
 142  
 143              $this->db->query($sql,__LINE__,__FILE__);
 144  
 145              $result = array();
 146              while (($row = $this->db->row(true)))
 147              {
 148                  $result[$row['id']] =& new Block_SO($row);
 149              }
 150              return $result;
 151          }
 152  
 153          /**
 154           * Retrives all blocks for a given content-area, cat-list, page and language
 155           * @param string $area name of content-area
 156           * @param array $cat_list array of cat-ids
 157           * @param int $page_id page-id or 0 for eg. an index-page 
 158           * @param string $lang 2-char language id
 159           * @return array of block-objects
 160           */
 161  		function getallblocksforarea($area,$cat_list,$page_id,$lang)
 162          {
 163              $lang = $this->db->quote($lang);
 164              $sql = "SELECT t1.block_id AS id, area, cat_id, page_id, t1.module_id, module_name, sort_order, title, viewable AS view".
 165                  " FROM $this->blocks_table AS t1".
 166                  " LEFT JOIN $this->modules_table AS t2 ON t1.module_id=t2.module_id".
 167                  " LEFT JOIN $this->blocks_lang_table AS t3 ON (t1.block_id=t3.block_id AND t3.lang=$lang) ".
 168                  " WHERE ".$this->db->expression($this->block_table,array(
 169                      'area' => $area,
 170                      '('
 171                  ));
 172                  
 173              $cat_list[] = CURRENT_SITE_ID;  // always included
 174              $sql .= $this->db->expression($this->block_table,array(
 175                  'page_id' => 0,
 176                  'cat_id'  => $cat_list,
 177              ));
 178              if ($page_id)
 179              {
 180                  $sql .= $this->db->expression($this->blocks_table,' OR ',array('page_id' => $page_id));
 181              }
 182              $sql .= ') ORDER BY sort_order';
 183  
 184              $this->db->query($sql,__LINE__,__FILE__);
 185  
 186              $result = array();
 187              while (($row = $this->db->row(true)))
 188              {
 189                  $result[$row['id']] =& new Block_SO($row);
 190              }
 191              return $result;
 192          }
 193  
 194          /**
 195          * Retrives the id's for all content-versions of a block
 196          * @param int $blockid block-id
 197          * @return array of int version-id's
 198          */
 199  		function getversionidsforblock($blockid)
 200          {
 201              $this->db->select($this->content_table,'version_id',array(
 202                  'block_id' => $blockid
 203              ),__LINE__,__FILE__);
 204  
 205              $result = array();
 206              while ($this->db->next_record())
 207              {
 208                  $result[] = $this->db->f('version_id');
 209              }
 210              return $result;
 211          }
 212  
 213  
 214          /**
 215          * Retrives all content-versions for a given block and language
 216          * @param int $blockid block-id
 217          * @param string $lang 2-char language id
 218          * @return array of version-id - version-information-array's
 219          */
 220  		function getallversionsforblock($blockid,$lang)
 221          {
 222              $lang = $this->db->quote($lang);
 223              $sql = "SELECT t1.version_id, arguments,arguments_lang,state".
 224                  " FROM $this->content_table AS t1".
 225                  " LEFT JOIN $this->content_lang_table AS t2 ON (t1.version_id=t2.version_id AND t2.lang=$lang)".
 226                  " WHERE ".$this->db->expression($this->content_table,array('block_id' => $blockid));
 227  
 228              $this->db->query($sql,__LINE__,__FILE__);
 229  
 230              $result = array();
 231              while ($this->db->next_record())
 232              {
 233                  $version['arguments'] = array_merge(
 234                      (array) unserialize($this->db->f('arguments')),
 235                      (array) unserialize($this->db->f('arguments_lang'))
 236                  );
 237                  $version['state'] = $this->db->f('state');
 238                  $version['id'] = $this->db->f('version_id');
 239                  
 240                  $result[$version['id']] = $version;
 241              }
 242              return $result;
 243          }
 244  
 245          /**
 246           * Selects all blocks from a given cat_list + site-wide blocks that are in given states
 247           * @param array $cat_list array of int cat-id's
 248           * @param int/array $states state-id's
 249           * @return array of block-objects, without content
 250           */
 251          function &getallblocks($cat_list,$states)
 252          {
 253              $cat_list[] = CURRENT_SITE_ID;
 254              $sql = "SELECT COUNT(state) AS cnt,t1.block_id AS id,area,cat_id,page_id,viewable AS view,state FROM $this->blocks_table AS t1,$this->content_table as t2 WHERE ".
 255                  $this->db->expression($this->content,array(
 256                      't1.block_id=t2.block_id',
 257                      'cat_id' => $cat_list,
 258                      'state'  => $states,
 259                  ))." GROUP BY t1.block_id,area,cat_id,page_id,viewable,state";
 260  
 261              $this->db->query($sql,__LINE__,__FILE__);
 262  
 263              $result = array();
 264              while (($row = $this->db->row(true)))
 265              {
 266                  //in cnt we retrieve the numbers of versions that are commitable for a block,
 267                  //i.e. if there are more than one, it should normally be a prepublished version 
 268                  //that will replace a preunpublished version
 269                  $result[$row['id']] =& new Block_SO($row);
 270              }
 271              return $result;
 272          }
 273  
 274          /**
 275           * Retrives the visible blocks in a content-area for given cat-list and page
 276           * @param string $area name of content area
 277           * @param array $cat_list array of int cat-id's
 278           * @param int $page_id page-id or 0 for eg. an index page
 279           * @param boolean $isadmin viewer is administrator
 280           * @param boolean $isuser viewer is regular eGW user, not anonymous
 281           * @return array of block-objects, with the latest version-id, but not the content
 282           */
 283          function &getvisibleblockdefsforarea($area,$cat_list,$page_id,$isadmin,$isuser)
 284          {
 285              $viewable[] = SITEMGR_VIEWABLE_EVERBODY;
 286              $viewable[] = $isuser ? SITEMGR_VIEWABLE_USER : SITEMGR_VIEWABLE_ANONYMOUS;
 287              if ($isadmin) $viewable[] = SITEMGR_VIEWABLE_ADMIN;
 288  
 289              // show anonymous blocks in edit-mode too, else one could not edit them
 290              if ($GLOBALS['sitemgr_info']['mode'] == 'Edit')
 291              {
 292                  $viewable[] = SITEMGR_VIEWABLE_ANONYMOUS;
 293              }
 294              $cat_list[] = CURRENT_SITE_ID;  // always included
 295  
 296              $sql = "SELECT t1.block_id AS id,area,cat_id,page_id,t1.module_id,module_name,state,version_id AS version,sort_order,viewable AS view " . 
 297                  "FROM $this->blocks_table AS t1,$this->modules_table AS t2,$this->content_table AS t3 " . 
 298                  "WHERE t1.module_id = t2.module_id AND t1.block_id=t3.block_id AND ".
 299                  $this->db->expression($this->blocks_table,array(
 300                      'area' => $area,
 301                  ),' AND (',array(
 302                      'page_id' => 0,
 303                      'cat_id'  => $cat_list,
 304                  ));
 305  
 306              if ($page_id)
 307              {
 308                  $sql .= ' OR '.$this->db->expression($this->blocks_table,array(
 309                      'page_id' => $page_id
 310                  ));
 311              }
 312              $sql .= ') AND '.$this->db->expression($this->blocks_table,array(
 313                      'viewable' => $viewable,
 314                  )) . ' AND '.$this->db->expression($this->content_table,array(
 315                      'state'    => $GLOBALS['Common_BO']->visiblestates
 316                  )) . ' ORDER BY sort_order';
 317              
 318              $this->db->query($sql,__LINE__,__FILE__);
 319  
 320              $result = array();
 321              while (($row = $this->db->row(true)))
 322              {
 323                  $result[$row['id']] =& new Block_SO($row);
 324              }
 325              return $result;
 326          }
 327  
 328          /**
 329           * Retrives the availible languages for the title of a block
 330           * @param int $block_id block-id
 331           * @return array of 2-char language id's
 332           */
 333  		function getlangarrayforblocktitle($block_id)
 334          {
 335              $this->db->select($this->blocks_lang_table,'lang',array(
 336                  'block_id' => $block_id
 337              ),__LINE__,__FILE__);
 338              
 339              $retval = array();
 340              while ($this->db->next_record())
 341              {
 342                  $retval[] = $this->db->f('lang');
 343              }
 344              return $retval;
 345          }
 346  
 347          /**
 348           * Retrives the availible languages for a blocks content-version
 349           * @param int $version_id version-id
 350           * @return array of 2-char language id's
 351           */    
 352  		function getlangarrayforversion($version_id)
 353          {
 354              $this->db->select($this->content_lang_table,'lang',array(
 355                  'version_id' => $version_id
 356              ),__LINE__,__FILE__);
 357              
 358              $retval = array();
 359              while ($this->db->next_record())
 360              {
 361                  $retval[] = $this->db->f('lang');
 362              }
 363              return $retval;
 364          }
 365  
 366          /**
 367           * Retrives the content (arguments) for a block's content-version and a language
 368           * @param int $version_id version-id
 369           * @param string/boolean $lang 2-char language-id or false for the first language found
 370           * @return array/boolean arguments array or false if language not found
 371           */
 372  		function getversion($version_id,$lang=false)
 373          {
 374              $fields = "arguments" . ($lang ? ', arguments_lang' : '');
 375              $lang_join = $lang ? "LEFT JOIN $this->content_lang_table AS t2 ON (t1.version_id = t2.version_id AND t2.lang=".$this->db->quote($lang).')' : '';
 376              $sql = "SELECT $fields FROM $this->content_table AS t1 $lang_join".
 377                  " WHERE t1.version_id = ".(int)$version_id;
 378  
 379              $this->db->query($sql,__LINE__,__FILE__);
 380              if ($this->db->next_record())
 381              {
 382                   return $lang ? 
 383                      array_merge(
 384                          (array) unserialize($this->db->f('arguments')),
 385                          (array) unserialize($this->db->f('arguments_lang')) 
 386                      ) : 
 387                      unserialize($this->db->f('arguments'));
 388              }
 389              return false;
 390          }
 391  
 392          /**
 393           * Retrives a block for a given block-id and language
 394           * @param int $block_id block-id
 395           * @param string $lang 2-char language id
 396           * @return object/boolean block-object or false if not found
 397           */
 398          function &getblock($block_id,$lang)
 399          {
 400              $lang = $this->db->quote($lang);
 401              $sql = "SELECT t1.block_id AS id,area,cat_id,page_id,area,t1.module_id,module_name,sort_order,title,viewable AS view".
 402                  " FROM $this->blocks_table AS t1".
 403                  " LEFT JOIN $this->modules_table as t2 ON t1.module_id=t2.module_id".
 404                  " LEFT JOIN $this->blocks_lang_table AS t3 ON (t1.block_id=t3.block_id AND t3.lang=$lang)".
 405                  " WHERE t1.block_id=".(int)$block_id;
 406              
 407              $this->db->query($sql,__LINE__,__FILE__);
 408  
 409              if (($row = $this->db->row(true)))
 410              {
 411                  return new Block_SO($row);
 412              }
 413              return false;
 414          }
 415  
 416          /**
 417           * Retrieves basic infos about a block
 418           * @param int $block_id block-id
 419           * @return object/boolean limited(!) block-object or false if not found
 420           */
 421          function &getblockdef($block_id)
 422          {
 423              $sql = "SELECT t1.block_id AS id,cat_id,page_id,area,t1.module_id,module_name".
 424                  " FROM $this->blocks_table AS t1,$this->modules_table AS t2".
 425                  " WHERE t1.module_id=t2.module_id AND t1.block_id=".(int)$block_id;
 426  
 427              $this->db->query($sql,__LINE__,__FILE__);
 428  
 429              if (($row = $this->db->row(true)))
 430              {
 431                  return new Block_SO($row);
 432              }
 433              return false;
 434          }
 435  
 436          /**
 437           * Retrives the title of a block in a given language
 438           * @param int $block_id block-id
 439           * @return string/boolean title of the block or false if not found
 440           */
 441  		function getlangblocktitle($block_id,$lang)
 442          {
 443              $cols = array('block_id' => $block_id);
 444              if ($lang) $cols['lang'] = $lang;
 445  
 446              $this->db->select($this->blocks_lang_table,'title',$cols,__LINE__,__FILE__);
 447                  
 448              return $this->db->next_record() ? $this->db->f('title') : false;
 449          }
 450  
 451  		function getblockstate($block_id)
 452          {
 453              $cols = array('block_id' => $block_id);
 454  
 455              $this->db->select($this->content_table,'state',$cols,__LINE__,__FILE__);
 456                  
 457              return $this->db->next_record() ? $this->db->f('title') : false;
 458          }
 459  
 460          /**
 461           * Save block-data: sort-order and by whom viewable 
 462           * @param object $block block-object
 463           * @return boolean true on success, false otherwise
 464           */
 465  		function saveblockdata($block)
 466          {
 467              return $this->db->update($this->blocks_table,array(
 468                      'sort_order'  => $block->sort_order,
 469                      'viewable'    => $block->view,
 470                  ),array('block_id' => $block->id),__LINE__,__FILE__);
 471          }
 472  
 473          /**
 474           * Save block-data: the language dependent block-title
 475           * @param int $block_id block-id
 476           * @param string $title block-title in $lang
 477           * @param string $lang 2-char language id
 478           * @return boolean true on success, false otherwise
 479           */
 480  		function saveblockdatalang($block_id,$title,$lang)
 481          {
 482              return $this->db->insert($this->blocks_lang_table,array(
 483                      'title' => $title
 484                  ),array(
 485                      'block_id'  => $block_id,
 486                      'lang'    => $lang,
 487                  ),__LINE__,__FILE__);
 488          }
 489  
 490          /**
 491           * Save block-data: the language independent versionised block-content
 492           * @param int $block_id block-id
 493           * @param int $version_id version-id
 494           * @param array $data array of block's content/arguments
 495           * @return boolean true on success, false otherwise
 496           */
 497  		function saveversiondata($block_id,$version_id,$data)
 498          {
 499              //this is necessary because double slashed data breaks while serialized
 500              if (isset($data))
 501              {
 502                  $this->remove_magic_quotes($data);
 503              }
 504              return $this->db->update($this->content_table,array(
 505                      'arguments' => serialize($data),
 506                  ),array(
 507                      'version_id' => $version_id,
 508                      'block_id'   => $block_id,
 509                  ),__LINE__,__FILE__);
 510          }
 511  
 512          /**
 513           * Save block-state
 514           * @param int $block_id block-id
 515           * @param int $version_id version-id
 516           * @param int $state state 
 517           * @return boolean true on success, false otherwise
 518           */
 519  		function saveversionstate($block_id,$version_id,$state)
 520          {
 521              return $this->db->update($this->content_table,array(
 522                      'state' => $state
 523                  ),array(
 524                      'version_id'  => $version_id,
 525                      'block_id'    => $block_id,
 526                  ),__LINE__,__FILE__);
 527          }
 528  
 529          /**
 530           * Save block-data: the language dependent versionised of block-content
 531           * @param int $version_id version-id
 532           * @param array $data array of language-dependent versionised block-content/arguments
 533           * @param string $lang 2-char language id
 534           * @return boolean true on success, false otherwise
 535           */
 536  		function saveversiondatalang($version_id,$data,$lang)
 537          {
 538              //this is necessary because double slashed data breaks while serialized
 539              if (isset($data))
 540              {
 541                  $this->remove_magic_quotes($data);
 542              }
 543              return $this->db->insert($this->content_lang_table,array(
 544                      'arguments_lang' => serialize($data)
 545                  ),array(
 546                      'version_id'  => $version_id,
 547                      'lang'      => $lang,
 548                  ),__LINE__,__FILE__);
 549          }
 550  
 551          /**
 552           * Run string or each value of an array thought stripslashes, if magic_quotes_gpc is on
 553           * @param string/array $data string or array of strings
 554           */
 555  		function remove_magic_quotes(&$data)
 556          {
 557              if (!get_magic_quotes_gpc())
 558              {
 559                  return;
 560              }
 561              if (is_array($data))
 562              {
 563                  foreach($data as $key => $val)
 564                  {
 565                      $this->remove_magic_quotes($data[$key]);
 566                  }
 567              }
 568              else
 569              {
 570                  $data = stripslashes($data);
 571              }
 572          }
 573  
 574          /**
 575           * Commits changes on a block: moves from state prepublish to publish and preunpublish to archive
 576           * @param int $block_id block-id
 577           */
 578  		function commit($block_id)
 579          {
 580              foreach(array(
 581                  SITEMGR_STATE_PREPUBLISH => SITEMGR_STATE_PUBLISH,
 582                  SITEMGR_STATE_PREUNPUBLISH => SITEMGR_STATE_ARCHIVE
 583              ) as $from => $to)
 584              {
 585                  $this->db->update($this->content_table,array(
 586                          'state' => $to
 587                      ),array(
 588                          'state' => $from,
 589                          'block_id' => $block_id,
 590                      ), __LINE__,__FILE__);
 591              }
 592          }
 593  
 594          /**
 595           * Reactivates a block: moves from state archive to draft
 596           * @param int $block_id block-id
 597           */
 598  		function reactivate($block_id)
 599          {
 600              $this->db->update($this->content_table,array(
 601                      'state' => SITEMGR_STATE_DRAFT
 602                  ),array(
 603                      'state' => SITEMGR_STATE_ARCHIVE,
 604                      'block_id' => $block_id,
 605                  ), __LINE__,__FILE__);
 606          }
 607  }


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