XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
comment.php
Go to the documentation of this file.
1 <?php
2 // $Id: comment.php 902 2012-01-03 07:09:16Z i.bitcero $
3 // --------------------------------------------------------------
4 // Red México Common Utilities
5 // A framework for Red México Modules
6 // Author: Eduardo Cortés <i.bitcero@gmail.com>
7 // Email: i.bitcero@gmail.com
8 // License: GPL 2.0
9 // --------------------------------------------------------------
10 
11 class RMComment extends RMObject
12 {
16  private $user = array();
17 
18  public function __construct($id=null){
19 
20  $this->db = XoopsDatabaseFactory::getDatabaseConnection();
21  $this->_dbtable = $this->db->prefix("rmc_comments");
22  $this->setNew();
23  $this->initVarsFromTable();
24 
25  if ($id==null){
26  return;
27  }
28 
29  if (!$this->loadValues($id)) return false;
30 
31  $this->unsetNew();
32 
33  }
34 
35  public function id(){
36  return $this->getVar('id_com');
37  }
38 
39  private function load_user(){
40 
41  $db = $this->db;
42 
43  $sql = "SELECT * FROM ".$db->prefix("rmc_comusers")." WHERE id_user=".$this->getVar('user');
44  $result = $db->query($sql);
45  if ($db->getRowsNum($resul)<=0) return;
46 
47  $row = $db->fetchArray($result);
48  $this->user = $row;
49 
50  }
51 
55  public function save(){
56 
57  // To check or modify data before save this
58  $ret = RMEvents::get()->run_event('rmcommon.saving.comment', $this);
59 
60  if ($this->isNew()){
61  $sr = $this->saveToTable();
62  } else {
63  $sr = $this->updateTable();
64  }
65 
66  return $ret && $sr;
67 
68  }
69 
73  public function delete(){
74 
75  if (!$this->deleteFromTable()) return false;
76 
77  // Update comments parent
78  $sql = "UPDATE ".$this->db->prefix("rmc_comments")." SET parent=".$this->getVar('parent')." WHERE parent=".$this->id();
79  if (!$this->db->queryF($sql)) $this->addError($this->db->error());
80 
81  // Reduce user posts number
82  $user = new RMCommentUser($this->getVar('user'));
83  if ($user->isNew()) return true;
84 
85  if($user->getVar('xuid')<=0) return true;
86 
87  $sql = "UPDATE ".$this->db->prefix("users")." SET posts=posts-1 WHERE uid=".$user->getVar('xuid');
88  if(!$this->db->queryF($sql)){
89  $this->addError($this->db->error());
90  return false;
91  }
92 
93  return true;
94 
95  }
96 
97 }