[ Index ]
 

Code source de Plume CMS 1.2.2

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/manager/inc/ -> class.news.php (source)

   1  <?php
   2  /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
   3  /*
   4  # ***** BEGIN LICENSE BLOCK *****
   5  # This file is part of Plume CMS, a website management application.
   6  # Copyright (C) 2001-2005 Loic d'Anterroches and contributors.
   7  #
   8  # Plume CMS is free software; you can redistribute it and/or modify
   9  # it under the terms of the GNU General Public License as published by
  10  # the Free Software Foundation; either version 2 of the License, or
  11  # (at your option) any later version.
  12  #
  13  # Plume CMS is distributed in the hope that it will be useful,
  14  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16  # GNU General Public License for more details.
  17  #
  18  # You should have received a copy of the GNU General Public License
  19  # along with this program; if not, write to the Free Software
  20  # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  21  #
  22  # ***** END LICENSE BLOCK ***** */
  23  
  24  require_once dirname(__FILE__).'/class.resource.php';
  25  
  26  define('PX_RESOURCE_MANAGER_NEWS', 'news');
  27  
  28  /**
  29   * Data storage of a news.
  30   */
  31  class News extends Resource
  32  {
  33      /** Store the details, associated website, etc. */
  34      var $details = null;
  35  
  36      /**
  37       * Constructor.
  38       */
  39      function News($data='')
  40      {
  41          parent::Resource($data);
  42          $this->details = new recordset();
  43      }
  44  
  45      /**
  46       * Load the News.
  47       *
  48       * @param mixed Identifier or resource id ('')
  49       * @return bool Success
  50       */
  51      function load($id='')
  52      {
  53          if (false === parent::load($id)) 
  54              //categories and authors
  55              return false;
  56          
  57          if (PX_RESOURCE_MANAGER_NEWS != $this->f('type_id'))
  58              return false;
  59  
  60          if (false === $this->loadDetails()) 
  61              return false;
  62  
  63          $this->isModified = False;
  64          $this->runPostLoadHook();
  65          return true;
  66      }
  67  
  68      /**
  69       * Get the details of a news.
  70       * The details are the associated link and website.
  71       *
  72       * @return bool Success
  73       */
  74      function loadDetails()
  75      {
  76          $this->getConnection();
  77          $r = 'SELECT * FROM '.$this->con->pfx.'news 
  78              WHERE resource_id=\''.$this->f('resource_id').'\'';
  79          if (($rs = $this->con->select($r)) !== false) {
  80              $this->details = $rs;
  81              return true;
  82          } else {
  83              $this->con->setError();
  84              return false;
  85          }
  86      }
  87  
  88      /**
  89       * Return the content of the news as a string ready for indexation.
  90       *   
  91       * @param string Format of the string (html, wiki, text)
  92       * @return string The content of the news as a string
  93       */
  94      function getAsString($format = 'html')
  95      {
  96          $string = str_repeat($this->f('title').' ', 5);
  97          $string .= ' '.str_repeat($this->f('subject').' ', 3);
  98          $string .= ' '.$this->f('description');
  99          $string .= ' '.$this->details->f('news_titlewebsite');
 100          $string .= ' '.$this->details->f('news_linkwebsite');
 101          return $string;
 102      }
 103  
 104      /**
 105       * Set the default values for the news.
 106       *
 107       * @param object User object to have the preferences
 108       * @return bool True
 109       */
 110      function setDefaults($user)
 111      {
 112          parent::setDefaults($user);
 113          if ($user->getPref('news_status')) {
 114              $this->setField('status', $user->getPref('news_status'));
 115          } else {
 116              $this->setField('status', PX_RESOURCE_STATUS_INEDITION);
 117          }
 118          $this->setField('category_id', $user->getPref('news_category_id'));
 119          $this->setField('subtype_id', $user->getPref('news_subtype'));
 120  
 121          $this->isModified = true;
 122          return true;
 123      }
 124  
 125      /**
 126       * Set the basic data of a news.
 127       * The basic data are the one saved in the table `resources`.
 128       * There is only a check of the data, use the commit() method
 129       * to save in the database.
 130       *
 131       * @param string Title of the news
 132       * @param string Subject or keywords
 133       * @param string Content
 134       * @param string Format of the content
 135       * @param int Status
 136       * @param timestamp Date of publication
 137       * @param timestamp Date of end of publication
 138       * @param bool Use a date of end of publication
 139       * @param int Comment support
 140       * @param int Subtype of the news
 141       */
 142      function set($title, $subject, $content, $format, $status, $datestart,
 143                   $dateend, $useenddate, $comment_support, $subtype)
 144      {
 145          //Test in a row the field to return errors for all 
 146          //the litigious data, not only the first one.
 147          if (strlen($title) == 0) {
 148              $this->setError(__('You need to give a title.'), 400); 
 149          }
 150          if (false === ($datestart = date::clean($datestart))) {
 151              $this->setError(__('The publication date is wrong.'), 400);
 152          }
 153          if (!empty($useenddate)) $dateend = date::EOT();
 154          if (false === ($dateend = date::clean($dateend))) {
 155              $this->setError(__('The off-line date is wrong.'), 400);
 156          }
 157          if (empty($subtype)) {
 158              $this->setError(__('A type of resource is needed.'), 400); 
 159          }          
 160          $this->setField('subtype_id', $subtype);
 161          $this->setField('subject', $subject);
 162          $this->setField('title', $title);
 163          $this->setField('description', '='.$format."\n".$content);
 164          $this->setField('publicationdate', $datestart);
 165          $this->setField('enddate', $dateend);
 166          $this->setField('comment_support', (int) $comment_support);
 167          $this->setField('status', $status);
 168  
 169          $this->isModified = true;
 170          if (false !== ($error = $this->error())) {
 171              return false;
 172          }
 173          return true;
 174      }
 175  
 176      /**
 177       * Set the details of a news.
 178       *
 179       * @param string Associated website title
 180       * @param string Associated website URL
 181       * @return bool Success
 182       */
 183      function setDetails($title, $link)
 184      {
 185          if (!empty($title)) {
 186              if (!preg_match('#^(http|ftp|news|https)://#i', $link)) {
 187                  $this->setError(__('The associated link must start with http:// or a valid protocol.'), 400);
 188              }
 189          }   
 190          $this->details->setField('news_titlewebsite', trim($title));
 191          $this->details->setField('news_linkwebsite', trim($link));
 192          $this->isModified = true;
 193          if (false !== ($error = $this->error())) {
 194              return false;
 195          }
 196          return true;
 197      }
 198  
 199      /**
 200       * Check the integrity of the news.
 201       *
 202       * The error is set if error found.
 203       *
 204       * @return bool Success
 205       */
 206      function check()
 207      {
 208          if (strlen($this->f('title')) == 0) {
 209              $this->setError(__('You need to give a title.'), 400); 
 210          }
 211          if (false === date::clean($this->f('publicationdate'))) {
 212              $this->setError(__('The publication date is wrong.'), 400);
 213          }
 214          if (false === date::clean($this->f('enddate'))) {
 215              $this->setError(__('The off-line date is wrong.'), 400);
 216          }
 217          if (strlen($this->f('subtype_id')) == 0) {
 218              $this->setError(__('A type of resource is needed.'), 400);
 219          }
 220          if (strlen($this->f('news_titlewebsite')) > 0) {
 221              if (strlen($this->f('news_linkwebsite'))
 222                  && !preg_match('/^(http|ftp|news|https):\/\//', 
 223                                 $this->f('news_linkwebsite'))) {
 224                  $this->setError(__('The associated link must start with http:// or a valid protocol.'), 400);
 225              }
 226          }      
 227          
 228          if (false !== $this->error()) {
 229              return false;
 230          }
 231          return true;
 232      }
 233  
 234      /**
 235       * Get the path to the news.
 236       *
 237       * @param string Force type of path ('')
 238       * @return string The path
 239       */
 240      function getPath($type='')
 241      {
 242          // Old website compatibility hack
 243          if (preg_match('#^\d+\-#', $this->f('path'))) {
 244              return parent::getPath($type);
 245          } else {
 246              $path = $this->f('path');
 247              $this->setField('path', $this->f('resource_id').'-'.$path);
 248              $newpath = parent::getPath($type);
 249              $this->setField('path', $path);
 250              return $newpath;
 251          }
 252      }
 253  
 254  
 255      /* ===================================================================== *
 256       *                                                                       *
 257       *                Methods for rendering the pages.                       *
 258       *                                                                       *
 259       * Note: All standalone methods.                                         *
 260       * ===================================================================== */
 261  
 262      /**
 263       * Action to display a category.
 264       *
 265       * @param string Server query string
 266       * @return int Success code
 267       */
 268      function action($query)
 269      {
 270          Hook::register('onInitTemplate', 'News', 'hookOnInitTemplate');
 271          // Easy access
 272          $GLOBALS['_PX_render']['last'] = '';
 273          $last =& $GLOBALS['_PX_render']['last']; 
 274          $GLOBALS['_PX_render']['website'] = '';
 275          $website =& $GLOBALS['_PX_render']['website']; 
 276          $GLOBALS['_PX_render']['cat'] = '';
 277          $cat =& $GLOBALS['_PX_render']['cat']; 
 278          $GLOBALS['_PX_render']['news'] = '';
 279          $news =& $GLOBALS['_PX_render']['news']; 
 280          $GLOBALS['_PX_render']['ct'] = '';
 281          $ct =& $GLOBALS['_PX_render']['ct']; 
 282  
 283          // Parse query string to find the matching news
 284          list($news_id, $category_path) = News::parseQueryString($query);
 285  
 286          // Load the matching news
 287          // If news does not exists, returns error code
 288          // Will be catched up by the 404 at the end
 289          $sql = SQL::getOnlineResourceInCat($news_id, $category_path, 
 290                                             config::f('website_id'));
 291          $con =& pxDBConnect();
 292          if (($news = $con->select($sql, 'News')) !== false) {
 293              if (!$news->isEmpty()) {
 294                  $news->load();
 295              } else {
 296                  return 404;
 297              }
 298          } else {
 299              $GLOBALS['_PX_render']['error']->setError('MySQL: '
 300                                                        .$con->error(), 500);
 301              return 404;
 302          }
 303  
 304          include_once dirname(__FILE__).'/class.cache.php';
 305          $cache = new Cache(urlencode('news%%'.$category_path.'%%'.$news_id));
 306          $cache->setCacheDirectory(config::getCacheDir());
 307          $cache->debug = config::f('debug');
 308  
 309          $GLOBALS['_PX_render']['ct'] = $news->comments;
 310          $GLOBALS['_PX_render']['res_id'] = $news_id;
 311          header(FrontEnd::getHeader($news->f('subtype_template')));
 312          // Load the template
 313          include config::f('manager_path').'/templates/'
 314              .config::f('theme_id').'/'.$news->f('subtype_template');
 315          return 200;
 316      }
 317  
 318      /**
 319       * Hook on the initialization of the templates.
 320       *
 321       * @param string Name of the calling hook
 322       * @param array Default parameters (not used)
 323       * @return bool Success
 324       */
 325      function hookOnInitTemplate($hook, $param)
 326      {
 327          if (config::f('action') == 'News') {
 328              $GLOBALS['_PX_render']['cat'] = FrontEnd::getCategory($GLOBALS['_PX_render']['news']->f('category_id'));;
 329              $GLOBALS['_PX_render']['website'] = FrontEnd::getWebsite();
 330              if (config::f('order_res_manual')) {
 331                  $order = 'ORDER BY %sresources.title ASC';
 332              } else {
 333                  $order = 'ORDER BY %sresources.publicationdate DESC';
 334              }
 335              $GLOBALS['_PX_render']['res'] = FrontEnd::getResources($GLOBALS['_PX_render']['news']->f('category_id'),
 336                                                                     '', '', 1, $order);
 337              $GLOBALS['_PX_render']['ct_enabled'] = false;
 338              if ((config::f('comment_support') == 1) 
 339                  or (config::f('comment_support') == 2 && 
 340                      $GLOBALS['_PX_render']['news']->f('comment_support') == 1)) {
 341                  $GLOBALS['_PX_render']['ct_enabled'] = true;
 342              }
 343          }
 344          return true;
 345      }
 346  
 347      /**
 348       * Parse query string.
 349       *
 350       * @param string Query string
 351       * @return array (news id, category path)
 352       */
 353      function parseQueryString($query)
 354      {
 355          $category = '';
 356          $id = '';
 357          if (preg_match('#^(.*/)(\d+)\-([^\.]*)$#i', $query, $match)) {
 358              $category = $match[1];
 359              $id = (int) $match[2];
 360          }
 361          return array($id, $category);
 362      }
 363  
 364  
 365      /* ===================================================================== *
 366       *                                                                       *
 367       *                Methods modifying data in the database.                *
 368       *                                                                       *
 369       * ===================================================================== */
 370  
 371      /**
 372       * Save the data into the database.
 373       *
 374       * @return bool Success
 375       */
 376      function commit()
 377      {
 378          if (false === $this->isModified) {
 379              return true;
 380          }
 381  
 382          //Real INSERT/UPDATE into the tables
 383          $this->getConnection();
 384          $update = (0 < (int) $this->f('resource_id')) ? true : false;
 385  
 386          if ($update) {
 387              $req = 'UPDATE '.$this->con->pfx.'resources SET
 388                  subject         = \''.$this->con->esc($this->f('subject')).'\',
 389                  comment_support  = \''.$this->con->esc($this->f('comment_support')).'\',
 390                  subtype_id      = \''.$this->con->esc($this->f('subtype_id')).'\',
 391                  title           = \''.$this->con->esc($this->f('title')).'\',
 392                  description     = \''.$this->con->esc($this->f('description')).'\',
 393                  publicationdate = \''.$this->con->esc($this->f('publicationdate')).'\',
 394                  modifdate       = \''.date::stamp().'\',
 395                  enddate         = \''.$this->con->esc($this->f('enddate')).'\',
 396                  status          = \''.$this->con->esc($this->f('status')).'\'
 397                  WHERE resource_id = \''.$this->con->esc($this->f('resource_id')).'\'';
 398  
 399          } else {
 400              $req = 'INSERT INTO '.$this->con->pfx.'resources SET
 401                  website_id      = \''.$this->con->esc($this->f('website_id')).'\',
 402                  type_id         = \''.PX_RESOURCE_MANAGER_NEWS.'\',
 403                  subtype_id      = \''.$this->con->esc($this->f('subtype_id')).'\',
 404                  comment_support  = \''.$this->con->esc($this->f('comment_support')).'\',
 405                  user_id         = \''.$this->con->esc($this->f('user_id')).'\',
 406                  subject         = \''.$this->con->esc($this->f('subject')).'\',
 407                  creatorname     = \'\',
 408                  creatoremail    = \'\',
 409                  creatorwebsite  = \'\',
 410                  publisher       = \'\',
 411                  lang_id         = \''.config::f('lang').'\',
 412                  title           = \''.$this->con->esc($this->f('title')).'\',
 413                  description     = \''.$this->con->esc($this->f('description')).'\',
 414                  creationdate    = \''.date::stamp().'\',
 415                  publicationdate = \''.date::stamp().'\',
 416  
 417                  modifdate       = \''.date::stamp().'\',
 418                  enddate         = \''.date::EOT().'\',
 419                  status          = \''.$this->con->esc($this->f('status')).'\',
 420                  size            = \'\',
 421                  version         = 1,
 422                  comment         = \'\',
 423                  misc            = \'\',
 424                  format          = \'text/html\',
 425                  dctype          = \'Text\',
 426                  dccoverage      = \'\',
 427                  rights          = \'\'';
 428          }
 429     
 430          if (!$this->con->execute($req)) {
 431              $this->setError('MySQL: '.$this->con->error(), 500);
 432              return false;
 433          }
 434  
 435          if (!$update) {
 436              //Insert, need to get the id of the news as the path and 
 437              //the identifier are based on it. Sadly, it is not possible to get
 438              //the value directly in the insert statement:
 439              // <http://dev.mysql.com/doc/mysql/en/INSERT.html>
 440              if (false == ($id = $this->con->getLastID())) {
 441                  $this->setError('MySQL: '.$this->con->error(), 500);
 442                  return false;
 443              }
 444              $this->setField('resource_id', $id);
 445              $this->setField('identifier', 'news-'.$id);
 446              $this->setField('path', $id.'-'.$this->con->esc(text::str2url($this->f('title'))));
 447              $req = 'UPDATE '.$this->con->pfx.'resources SET 
 448                  identifier = \''.$this->f('identifier').'\',
 449                  path = \''.$this->f('path').'\'
 450                  WHERE resource_id = \''.$this->con->esc($id).'\'';
 451              if (!$this->con->execute($req)) {
 452                  $this->setError('MySQL: '.$this->con->error(), 500);
 453                  return false;
 454              }
 455          }
 456  
 457          if (empty($id)) $id = $this->f('resource_id');
 458  
 459          if ($this->addAuthor($this->f('user_id'))
 460              && $this->addToCategory($this->f('category_id'))
 461              && $this->commitDetails()
 462              && $this->load($id)) {
 463              $this->runPostCommitHook();
 464              return true;
 465          } else {
 466              return false;
 467          }
 468      }
 469  
 470  
 471  
 472      /**
 473       * Save the details of a news.
 474       *
 475       * @return bool Success
 476       */
 477      function commitDetails()
 478      {
 479          $this->getConnection();
 480          
 481          //check if need to updated or insert
 482          if (0 < (int) $this->details->f('resource_id')) {
 483              $update = true;
 484          } else {
 485              $update = false;
 486          }
 487  
 488          if ($update) {
 489              $req = 'UPDATE '.$this->con->pfx.'news SET
 490                  news_serial = \''.$this->con->esc(md5($this->f('title'))).'\',
 491                  news_titlewebsite = \''.$this->con->esc($this->details->f('news_titlewebsite')).'\',
 492                  news_linkwebsite = \''.$this->con->esc($this->details->f('news_linkwebsite')).'\'
 493                  WHERE resource_id = \''.$this->con->esc($this->f('resource_id')).'\'';
 494          } else {
 495              $req = 'INSERT INTO '.$this->con->pfx.'news SET
 496                  news_serial = \''.$this->con->esc(md5($this->f('title'))).'\',
 497                  news_titlewebsite = \''.$this->con->esc($this->details->f('news_titlewebsite')).'\',
 498                  news_linkwebsite = \''.$this->con->esc($this->details->f('news_linkwebsite')).'\',
 499                  resource_id = \''.$this->con->esc($this->f('resource_id')).'\'';
 500          }
 501          if (!$this->con->execute($req)) {
 502              $this->setError('MySQL: '.$this->con->error(), 500);
 503              return false;
 504          }          
 505          $this->loadDetails(); //Synchro
 506          return true;
 507      }
 508  
 509      /**
 510       * Remove the news from the database.
 511       *
 512       * @return bool Success
 513       */
 514      function remove()
 515      {
 516          $id = $this->f('resource_id');
 517          if (empty($id)) {
 518              return true;
 519          }
 520                                             
 521          $delReq = 'DELETE FROM '.$this->con->pfx.'resources WHERE
 522                     resource_id = \''.$this->con->esc($id).'\'';
 523          if (!$this->con->execute($delReq)) {
 524              $this->setError('MySQL: '.$this->con->error(), 500);
 525              return false;
 526          }
 527                      
 528          $delReq = 'DELETE FROM '.$this->con->pfx.'categoryasso WHERE
 529                     identifier = \''.$this->f('identifier').'\'';
 530          if (!$this->con->execute($delReq)) {
 531              $this->setError('MySQL: '.$this->con->error(), 500);
 532              return false;
 533          }
 534          
 535          $delReq = 'DELETE FROM '.$this->con->pfx.'authorasso WHERE
 536                     resource_id = \''.$this->con->esc($id).'\'';
 537          if (!$this->con->execute($delReq)) {
 538              $this->setError('MySQL: '.$this->con->error(), 500);
 539              return false;
 540          }
 541          
 542          $delReq = 'DELETE FROM '.$this->con->pfx.'news WHERE
 543                     resource_id = \''.$this->con->esc($id).'\'';
 544          if (!$this->con->execute($delReq)) {
 545              $this->setError('MySQL: '.$this->con->error(), 500);
 546              return false;
 547          }
 548  
 549          return true;
 550      }
 551  
 552      /**
 553       * Remove details.
 554       * Remove the detailed informations from the `news` table for 
 555       * the current news.
 556       *
 557       * @return bool Success
 558       */
 559      function removeDetails()
 560      {
 561          if (0 < (int) $this->f('resource_id')
 562              && $this->f('resource_id') == $this->details->f('resource_id')
 563              ) {
 564              $this->getConnection();
 565              $req = 'DELETE FROM '.$this->con->pfx.'news WHERE 
 566                 resource_id = \''.$this->con->esc($this->f('resource_id')).'\'';
 567              if (!$this->con->execute($req)) {
 568                  $this->setError('MySQL: '.$this->con->error(), 500);
 569                  return false;
 570              }          
 571              $this->loadDetails(); //Synchro
 572              return true;
 573          } else {
 574              $this->setError(__('Error: No details to be removed for the current news.'), 400);
 575              return false;
 576          }
 577      }
 578  }
 579  
 580  ?>


Généré le : Mon Nov 26 11:57:01 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics