[ Index ]

Source Code Reference for V1.00

title

Body

[close]

/modules/forums/ -> forums.class.php (source)

   1  <?php /* $Id: forums.class.php 137 2008-04-04 16:12:02Z pedroix $ $URL: https://web2project.svn.sourceforge.net/svnroot/web2project/trunk/modules/forums/forums.class.php $ */
   2  if (!defined('W2P_BASE_DIR')) {
   3      die('You should not access this file directly.');
   4  }
   5  
   6  require_once ($AppUI->getSystemClass('libmail'));
   7  require_once ($AppUI->getLibraryClass('PEAR/BBCodeParser'));
   8  $bbparser = new HTML_BBCodeParser();
   9  
  10  $filters = array('- Filters -');
  11  
  12  if ($a == 'viewer') {
  13      array_push($filters, 'My Watched', 'Last 30 days');
  14  } else {
  15      array_push($filters, 'My Forums', 'My Watched', 'My Projects', 'My Company', 'Inactive Projects');
  16  }
  17  
  18  class CForum extends CW2pObject {
  19      var $forum_id = null;
  20      var $forum_project = null;
  21      var $forum_status = null;
  22      var $forum_owner = null;
  23      var $forum_name = null;
  24      var $forum_create_date = null;
  25      var $forum_last_date = null;
  26      var $forum_last_id = null;
  27      var $forum_message_count = null;
  28      var $forum_description = null;
  29      var $forum_moderated = null;
  30  
  31  	function CForum() {
  32          // empty constructor

  33          parent::CW2pObject('forums', 'forum_id');
  34      }
  35  
  36  	function bind($hash) {
  37          if (!is_array($hash)) {
  38              return "CForum::bind failed";
  39          } else {
  40              $q = new DBQuery;
  41              $q->bindHashToObject($hash, $this);
  42              $q->clear();
  43              return null;
  44          }
  45      }
  46  
  47  	function check() {
  48          if ($this->forum_id === null) {
  49              return 'forum_id is NULL';
  50          }
  51          // TODO MORE

  52          return null; // object is ok

  53      }
  54  
  55  	function store() {
  56          $msg = $this->check();
  57          if ($msg) {
  58              return 'CForum::store-check failed ' . $msg;
  59          }
  60          if ($this->forum_id) {
  61              $q = new DBQuery;
  62              $ret = $q->updateObject('forums', $this, 'forum_id', false); // ! Don't update null values

  63              $q->clear();
  64              if ($this->forum_name) {
  65                  // when adding messages, this functon is called without first setting 'forum_name'

  66                  addHistory('forums', $this->forum_id, 'update', $this->forum_name);
  67              }
  68          } else {
  69              $date = new CDate();            
  70              $this->forum_create_date = $date->format(FMT_DATETIME_MYSQL);
  71              $q = new DBQuery;
  72              $ret = $q->insertObject('forums', $this, 'forum_id');
  73              $q->clear();
  74              addHistory('forums', $this->forum_id, 'add', $this->forum_name);
  75          }
  76          if (!$ret) {
  77              return 'CForum::store failed ' . db_error();
  78          } else {
  79              return null;
  80          }
  81      }
  82  
  83  	function delete() {
  84          $q = new DBQuery;
  85          $q->setDelete('forum_visits');
  86          $q->addWhere('visit_forum = ' . (int)$this->forum_id);
  87          $q->exec(); // No error if this fails, it is not important.

  88          $q->clear();
  89  
  90          $q->setDelete('forums');
  91          $q->addWhere('forum_id = ' . (int)$this->forum_id);
  92          if (!$q->exec()) {
  93              $q->clear();
  94              return db_error();
  95          }
  96          $q->clear();
  97          $q->setDelete('forum_messages');
  98          $q->addWhere('message_forum = ' . (int)$this->forum_id);
  99          if (!$q->exec()) {
 100              $result = db_error();
 101          } else {
 102              addHistory('forums', $this->forum_id, 'delete', $this->forum_name);
 103              $result = null;
 104          }
 105          $q->clear();
 106          return $result;
 107      }
 108  
 109  	function getAllowedRecords($uid, $fields = '*', $orderby = '', $index = null, $extra = null) {
 110          global $AppUI;
 111          require_once ($AppUI->getModuleClass('projects'));
 112          $oPrj = new CProject();
 113  
 114          $aPrjs = $oPrj->getAllowedRecords($uid, 'projects.project_id, project_name', '', null, null, 'projects');
 115          if (count($aPrjs)) {
 116              $buffer = '(forum_project IN (' . implode(',', array_keys($aPrjs)) . ') OR forum_project IS NULL OR forum_project = \'\' OR forum_project = 0)';
 117  
 118              if ($extra['where'] != '') {
 119                  $extra['where'] = $extra['where'] . ' AND ' . $buffer;
 120              } else {
 121                  $extra['where'] = $buffer;
 122              }
 123          } else {
 124              // There are no allowed projects, so only allow forums with no project associated.

 125              if ($extra['where'] != '') {
 126                  $extra['where'] = $extra['where'] . ' AND (forum_project IS NULL OR forum_project = \'\' OR forum_project = 0) ';
 127              } else {
 128                  $extra['where'] = '(forum_project IS NULL OR forum_project = \'\' OR forum_project = 0)';
 129              }
 130          }
 131          return parent::getAllowedRecords($uid, $fields, $orderby, $index, $extra);
 132  
 133      }
 134  }
 135  
 136  class CForumMessage {
 137      var $message_id = null;
 138      var $message_forum = null;
 139      var $message_parent = null;
 140      var $message_author = null;
 141      var $message_editor = null;
 142      var $message_title = null;
 143      var $message_date = null;
 144      var $message_body = null;
 145      var $message_published = null;
 146  
 147  	function CForumMessage() {
 148          // empty constructor

 149      }
 150  
 151  	function bind($hash) {
 152          if (!is_array($hash)) {
 153              return 'CForumMessage::bind failed';
 154          } else {
 155              $q = new DBQuery;
 156              $q->bindHashToObject($hash, $this);
 157              $q->clear();
 158              return null;
 159          }
 160      }
 161  
 162  	function check() {
 163          if ($this->message_id === null) {
 164              return 'message_id is NULL';
 165          }
 166          // TODO MORE

 167          return null; // object is ok

 168      }
 169  
 170  	function store() {
 171          $msg = $this->check();
 172          if ($msg) {
 173              return 'CForumMessage::store-check failed ' . $msg;
 174          }
 175          $q = new DBQuery;
 176          if ($this->message_id) {
 177              // First we need to remove any forum visits for this message

 178              // otherwise nobody will see that it has changed.

 179              $q->setDelete('forum_visits');
 180              $q->addWhere('visit_message = ' . (int)$this->message_id);
 181              $q->exec(); // No error if this fails, it is not important.

 182              $q->clear();
 183              $ret = $q->updateObject('forum_messages', $this, 'message_id', false); // ! Don't update null values

 184              $q->clear();
 185          } else {
 186              $date = new CDate();            
 187              $this->message_date = $date->format(FMT_DATETIME_MYSQL);
 188              
 189              $new_id = $q->insertObject('forum_messages', $this, 'message_id'); ## TODO handle error now

 190              echo db_error(); ## TODO handle error better

 191              $q->clear();
 192  
 193              $q->addTable('forum_messages');
 194              $q->addQuery('count(message_id), MAX(message_date)');
 195              $q->addWhere('message_forum = ' . (int)$this->message_forum);
 196  
 197              $res = $q->exec();
 198              echo db_error(); ## TODO handle error better

 199              $reply = $q->fetchRow();
 200              $q->clear();
 201  
 202              //update forum descriptor

 203              $forum = new CForum();
 204              $forum->forum_id = $this->message_forum;
 205              $forum->forum_message_count = $reply[0];
 206              $forum->forum_last_date = $reply[1];
 207              $forum->forum_last_id = $this->message_id;
 208  
 209              $forum->store(); ## TODO handle error now

 210  
 211              return $this->sendWatchMail(false);
 212          }
 213  
 214          if (!$ret) {
 215              return 'CForumMessage::store failed ' . db_error();
 216          } else {
 217              return null;
 218          }
 219      }
 220  
 221  	function delete() {
 222          $q = new DBQuery;
 223          $q->setDelete('forum_visits');
 224          $q->addWhere('visit_message = ' . (int)$this->message_id);
 225          $q->exec(); // No error if this fails, it is not important.

 226          $q->clear();
 227  
 228          $q->addTable('forum_messages');
 229          $q->addQuery('message_forum');
 230          $q->addWhere('message_id = ' . (int)$this->message_id);
 231          $forumId = $q->loadResult();
 232          $q->clear();
 233  
 234          $q->setDelete('forum_messages');
 235          $q->addWhere('message_id = ' . (int)$this->message_id);
 236          if (!$q->exec()) {
 237              $result = db_error();
 238          } else {
 239              $result = null;
 240          }
 241          $q->clear();
 242  
 243          $q->addTable('forum_messages');
 244          $q->addQuery('COUNT(message_id)');
 245          $q->addWhere('message_forum = ' . (int)$forumId);
 246          $messageCount = $q->loadResult();
 247          $q->clear();
 248  
 249          $q->addTable('forums');
 250          $q->addUpdate('forum_message_count', $messageCount);
 251          $q->addWhere('forum_id = ' . (int)$forumId);
 252          $q->exec();
 253          $q->clear();
 254  
 255          return $result;
 256      }
 257  
 258  	function sendWatchMail($debug = false) {
 259          global $AppUI, $debug, $w2Pconfig;
 260          $subj_prefix = $AppUI->_('forumEmailSubj', UI_OUTPUT_RAW);
 261          $body_msg = $AppUI->_('forumEmailBody', UI_OUTPUT_RAW);
 262  
 263          // Get the message from details.

 264          $q = new DBQuery;
 265          $q->addTable('users', 'u');
 266          $q->addQuery('contact_email, contact_first_name, contact_last_name');
 267          $q->addJoin('contacts', 'con', 'contact_id = user_contact', 'inner');
 268          $q->addWhere('user_id = ' . (int)$this->message_author);
 269          $res = $q->exec();
 270          if ($row = $q->fetchRow()) {
 271              $message_from = $row['contact_first_name'] . ' ' . $row['contact_last_name'] . '<' . $row['contact_email'] . '>';
 272          } else {
 273              $message_from = 'Unknown user';
 274          }
 275          // Get the forum name;

 276          $q->clear();
 277          $q->addTable('forums');
 278          $q->addQuery('forum_name');
 279          $q->addWhere('forum_id = \'' . $this->message_forum . '\'');
 280          $res = $q->exec();
 281          if ($row = $q->fetchRow()) {
 282              $forum_name = $row['forum_name'];
 283          } else {
 284              $forum_name = 'Unknown';
 285          }
 286  
 287          // SQL-Query to check if the message should be delivered to all users (forced)

 288          // In positive case there will be a (0,0,0) row in the forum_watch table

 289          $q->clear();
 290          $q->addTable('forum_watch');
 291          $q->addQuery('*');
 292          $q->addWhere('watch_user = 0 AND watch_forum = 0 AND watch_topic = 0');
 293          $resAll = $q->exec();
 294          $AllCount = db_num_rows($resAll);
 295  
 296          $q->clear();
 297          $q->addTable('users');
 298          $q->addQuery('DISTINCT contact_email, user_id, contact_first_name, contact_last_name');
 299          $q->leftJoin('contacts', 'con', 'contact_id = user_contact');
 300  
 301          if ($AllCount < 1) {
 302          //message is only delivered to users that checked the forum watch

 303              $q->addTable('forum_watch');
 304              $q->addWhere('user_id = watch_user AND (watch_forum = ' . (int)$this->message_forum . ' OR watch_topic = ' . (int)$this->message_parent . ')');
 305          }
 306  
 307          if (!($res = $q->exec(ADODB_FETCH_ASSOC))) {
 308              $q->clear();
 309              return;
 310          }
 311          if (db_num_rows($res) < 1) {
 312              return;
 313          }
 314  
 315          $mail = new Mail;
 316          $mail->Subject($subj_prefix . ' ' . $this->message_title, isset($GLOBALS['locale_char_set']) ? $GLOBALS['locale_char_set'] : '');
 317  
 318          $body = $body_msg;
 319  
 320          $body .= "\n\n" . $AppUI->_('Forum', UI_OUTPUT_RAW) . ': ' . $forum_name;
 321          $body .= "\n" . $AppUI->_('Subject', UI_OUTPUT_RAW) . ': ' . $this->message_title;
 322          $body .= "\n" . $AppUI->_('Message From', UI_OUTPUT_RAW) . ': ' . $message_from;
 323          $body .= "\n\n" . W2P_BASE_URL . '/index.php?m=forums&a=viewer&forum_id=' . $this->message_forum;
 324          $body .= "\n\n" . $this->message_body;
 325  
 326          $mail->Body($body, isset($GLOBALS['locale_char_set']) ? $GLOBALS['locale_char_set'] : '');
 327  
 328          while ($row = $q->fetchRow()) {
 329              if ($mail->ValidEmail($row['contact_email'])) {
 330                  $mail->To($row['contact_email'], true);
 331                  $mail->Send();
 332              }
 333          }
 334          $q->clear();
 335          return;
 336      }
 337  }
 338  ?>


Generated: Fri Jan 9 03:00:02 2009 Cross-referenced by PHPXref 0.7