linea21-modules
[ class tree: linea21-modules ] [ index: linea21-modules ] [ all elements ]

Source for file class.comment.php

Documentation is available at class.comment.php

  1. <?php
  2. /**
  3.  * @package linea21.modules
  4.  * @subpackage comment
  5.  * @author Simon Georget <simon@linea21.com>
  6.  * @version $id SVN
  7.  * @access public
  8.  * @license http://opensource.org/licenses/gpl-3.0.html
  9.  *  Plugin Management
  10.  */
  11.  
  12. class comment {
  13.  
  14.     private $params = array();
  15.     protected $dispatcher = null;
  16.  
  17.  
  18.     public function __construct($a = array())
  19.     {
  20.         $this->dispatcher = $GLOBALS['dispatcher'];
  21.         $this->params = $a;
  22.     }
  23.  
  24.     public function __call($method, $arguments)
  25.     {
  26.         $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'comment.extensible_function', array(
  27.                 'method'    => $method,
  28.                 'arguments' => $arguments
  29.         )));
  30.         if (!$event->isProcessed())
  31.         {
  32.             throw new Exception(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
  33.         }
  34.  
  35.         return $event->getReturnValue();
  36.     }
  37.  
  38.  
  39.     /**
  40.      * comment::checkDataIntegrity()
  41.      * Vérification intégrité des données
  42.      *
  43.      * @access public
  44.      * @param array $a 
  45.      * @return boolean true
  46.      *  si verifié, sinon string 'message d'erreur'
  47.      */
  48.  
  49.     public function checkDataIntegrity($a)
  50.     {
  51.         // Filter data event + return value
  52.         $r = $this->dispatcher->filter(new sfEvent($this, 'comment.before_datacheck', array('data' => $a)), $a);
  53.         $a = $r->getReturnValue();
  54.  
  55.         // if not authenticated
  56.         if(!isset($_SESSION['authenticated'])) {
  57.             if (strlen($a['name']) < 3) return _t('comment','no_name');
  58.             if (strlen($a['email']) < 3) return _t('comment','no_email');
  59.             $result = $this->_checkEmailValidity($a['email']);
  60.             if (is_string($result)) return $result;
  61.         }
  62.  
  63.         if (strlen($a['body']) < 3) return _t('comment','no_body');
  64.  
  65.         // Notify the beginning of the current method
  66.         $this->dispatcher->notify(new sfEvent($this, 'comment.after_datacheck', array('data' => $table_scale)));
  67.  
  68.         return true;
  69.     }
  70.  
  71.     /**
  72.      * comment::_checkEmailValidity()
  73.      * Email validation
  74.      *
  75.      * @access private
  76.      * @param string $email 
  77.      * @return string $result
  78.      *  return 1 si valide sinon message d'erreur (string)
  79.      */
  80.     function _checkEmailValidity($email)
  81.     {
  82.  
  83.         $is_valid = validEmail($email);
  84.  
  85.         if(!$is_valid) {
  86.             return sprintf(_t('contact','email_error'), $email);
  87.         } else {
  88.             return true;
  89.         }
  90.  
  91.     }
  92.  
  93.     public function add($a, $sql_object) {
  94.  
  95.         // Filter data event + return value
  96.         $r = $this->dispatcher->filter(new sfEvent($this, 'comment.before_add', array('data' => $a)), $a);
  97.         $a = $r->getReturnValue();
  98.          
  99.         $a=$sql_object->DBescape($a);
  100.  
  101.         if ($a['name'] != '') {
  102.             $a['name'] = strip_input(trim($a['name']), false);
  103.         }
  104.         if ($a['email'] != '') {
  105.             $a['email'] = strip_input(trim($a['email']), false);
  106.         }
  107.         if ($a['url'] != '') {
  108.             $a['url'] = strip_input(trim($a['url']), false);
  109.         }
  110.  
  111.         if(COMMENT_MODERATION  == 1) {
  112.             $a['status']='D';
  113.             // if user is logged-in, do we moderate or not?
  114.             if(!empty($a['user_id']) && COMMENT_MODERATE_REGISTERED  == 0) $a['status']='P';
  115.         }
  116.         else $a['status']='P';
  117.  
  118.         $q = "INSERT INTO " . T_COMM . " (comment_module, comment_module_id, comment_user_id, comment_name, comment_email, comment_url, comment_body, comment_notification, comment_status, comment_date_crea) VALUES('" . $a['module'] . "', '" . $a['module_id'] . "', '" . $a['user_id'] . "', '" . $a['name'] . "', '" . $a['email'] . "', '" . $a['url'] . "', '" . $a['body'] . "', '" . $a['notification'] . "', '" . $a['status']. "', now());";
  119.  
  120.         $last_id = $sql_object->DBInsert ($q, 1);
  121.  
  122.         // Notify the end of the current method
  123.         $this->dispatcher->notify(new sfEvent($this, 'comment.after_add', array('data' => $a, 'id' => $last_id)));
  124.  
  125.         return $last_id;
  126.     }
  127.  
  128.     public function get($options, $sql_object) {
  129.          
  130.         // Notify the end of the current method
  131.         $this->dispatcher->notify(new sfEvent($this, 'comment.get', array('data' => $options)));
  132.  
  133.         $data = $sql_object->DBSelect(SQL_getComments($options));
  134.  
  135.         return $data;
  136.     }
  137.  
  138.     public function getCommentItem($options, $sql_object) {
  139.          
  140.         // Notify the end of the current method
  141.         $this->dispatcher->notify(new sfEvent($this, 'comment.get_comment_item', array('data' => $options)));
  142.  
  143.         $data = $sql_object->DBSelect(SQL_getCommentItem($options));
  144.  
  145.         return $data;
  146.     }
  147.  
  148.     public function getSubscribers($options, $sql_object) {
  149.  
  150.         // Notify the end of the current method
  151.         $this->dispatcher->notify(new sfEvent($this, 'comment.get_subscribers', array('data' => $options)));
  152.          
  153.         $data = array();
  154.  
  155.         $tmp1 = $sql_object->DBSelect(SQL_getRegisteredSubscribers($options, $sql_object));
  156.         $tmp2 = $sql_object->DBSelect(SQL_getNonRegisteredSubscribers($options, $sql_object));
  157.         if(!is_array($tmp1)) $tmp1 = array();
  158.         if(is_array($tmp2)) $tmp = array_merge($tmp1, $tmp2);
  159.  
  160.         // we ensure there is no duplicate emails
  161.         foreach($tmp as $el) {
  162.             if(!in_array($el['user_email'], $data)) {
  163.                 array_push($data, $el['user_email']);
  164.             }
  165.         }
  166.  
  167.         return $data;
  168.     }
  169.  
  170.     public function getByModule($options, $sql_object) {
  171.          
  172.         // Notify the end of the current method
  173.         $this->dispatcher->notify(new sfEvent($this, 'comment.get_by_module', array('data' => $options)));
  174.  
  175.         $data = $sql_object->DBSelect(SQL_getModuleComments($options));
  176.  
  177.         return $data;
  178.     }
  179.  
  180.     /*
  181.      * get all comments awaiting for moderation
  182.     */
  183.     public function getAllByModule($status, $sql_object, $limit = false, $sort = false) {
  184.  
  185.         // Notify the end of the current method
  186.         $this->dispatcher->notify(new sfEvent($this, 'comment.get_all_by_module', array('status' => $status, 'limit' => $limit, 'sort' => $sort)));
  187.          
  188.         $r = array();
  189.  
  190.         if(MOD_NEWS == 1) {
  191.             $tmp = $this->getByModule(array('module' => 'news', 'status' => $status, 'limit' => $limit), $sql_object, 'module');
  192.             if(is_array($tmp)) $r = array_merge($r, $tmp);
  193.         }
  194.         if(MOD_PROJECT == 1) {
  195.             $tmp = $this->getByModule(array('module' => 'project', 'status' => $status, 'limit' => $limit), $sql_object, 'module');
  196.             if(is_array($tmp)) $r = array_merge($r, $tmp);
  197.         }
  198.         if(MOD_WORKSHOP == 1) {
  199.             $tmp = $this->getByModule(array('module' => 'workgroups', 'status' => $status, 'limit' => $limit), $sql_object, 'module');
  200.             if(is_array($tmp)) $r = array_merge($r, $tmp);
  201.         }
  202.         if(MOD_WORKSHOP == 1) {
  203.             $tmp = $this->getByModule(array('module' => 'report', 'status' => $status, 'limit' => $limit), $sql_object, 'module');
  204.             if(is_array($tmp)) $r = array_merge($r, $tmp);
  205.         }
  206.         if(MOD_WORKSHOP == 1) {
  207.             $tmp = $this->getByModule(array('module' => 'files', 'status' => $status, 'limit' => $limit), $sql_object, 'module');
  208.             if(is_array($tmp)) $r = array_merge($r, $tmp);
  209.         }
  210.         if(MOD_SDI == 1) {
  211.             $tmp = $this->getByModule(array('module' => 'indicator', 'status' => $status, 'limit' => $limit), $sql_object, 'module');
  212.             if(is_array($tmp)) $r = array_merge($r, $tmp);
  213.         }
  214.         if(MOD_PUBLICATION == 1) {
  215.             $tmp = $this->getByModule(array('module' => 'publication', 'status' => $status, 'limit' => $limit), $sql_object, 'module');
  216.             if(is_array($tmp)) $r = array_merge($r, $tmp);
  217.         }
  218.         $tmp = $this->getByModule(array('module' => 'contribute', 'status' => $status, 'limit' => $limit), $sql_object, 'module');
  219.         if(is_array($tmp)) $r = array_merge($r, $tmp);
  220.  
  221.         if($sort == 'date') {
  222.             // on obtient une liste de colonnes
  223.             foreach ($r as $key => $row) {
  224.                 $date[$key]  = $row['comment_date_timestamp'];
  225.             }
  226.  
  227.             // Tri des données par date décroissante
  228.             // Ajoute $r en tant que dernier paramètre, pour trier par la clé commune
  229.             if(isset($date) && is_array($date)) array_multisort($date, SORT_DESC, $r);
  230.         }
  231.         if($limit != false) {
  232.             $r = array_slice($r, 0, $limit);
  233.         }
  234.  
  235.         return $r;
  236.     }
  237.  
  238.  
  239.     public function modify() {
  240.         // TODO
  241.     }
  242.  
  243.     public function isNew($id, $sql_object) {
  244.          
  245.         // Notify the end of the current method
  246.         $this->dispatcher->notify(new sfEvent($this, 'comment.is_new', array('id' => $id)));
  247.  
  248.         $id = $sql_object->DBEscape($id);
  249.         $q = "SELECT comment_status FROM " . T_COMM . " WHERE  comment_id = '".$id."';";
  250.         $r = $sql_object->DBSelect($q);
  251.  
  252.         if($r[0]['comment_status'] == 'D') {
  253.             return true;
  254.         } else {
  255.             return false;
  256.         }
  257.  
  258.     }
  259.  
  260.  
  261.  
  262.     public function unsubscribe($a, $sql_object) {
  263.  
  264.         // Notify the end of the current method
  265.         $this->dispatcher->notify(new sfEvent($this, 'comment.unsubscribe', array('data' => $a)));
  266.          
  267.         // if registered user
  268.         $a = $sql_object->DBEscape($a);
  269.         $q = "SELECT U.user_id, P.profile_email FROM " . T_USER . " AS U LEFT OUTER JOIN " . T_PROFILE . " AS P ON P.profile_id = U.user_profile WHERE  P.profile_email = '".$a['email']."'";
  270.  
  271.         $data = $sql_object->DBSelect($q);
  272.  
  273.         if(count($data) > 0 && $data != 0) {
  274.             // we loop because (in theory) a user can have several accounts associtaed with one unique email
  275.             for($i=0; $i < count($data); $i++) {
  276.                 $q = "UPDATE ". T_COMM . " SET comment_notification = 'N' WHERE comment_module='".$a['module']."' AND comment_module_id='".$a['module_id']."' AND comment_status <> 'D' AND comment_user_id='".$data[$i]['user_id']."'";
  277.                 $r = $sql_object->DBQuery($q);
  278.             }
  279.         }
  280.  
  281.         // if not registered user
  282.         $q = "UPDATE ". T_COMM . " SET comment_notification = 'N' WHERE comment_module='".$a['module']."' AND comment_module_id='".$a['module_id']."' AND comment_status <> 'D' AND comment_email='".$a['email']."'";
  283.         $r = $sql_object->DBQuery($q);
  284.  
  285.         return $r;
  286.  
  287.     }
  288.  
  289.     public function delete($id, $sql_object) {
  290.  
  291.         // Notify the end of the current method
  292.         $this->dispatcher->notify(new sfEvent($this, 'comment.delete', array('id' => $id)));
  293.          
  294.         $id = $sql_object->DBEscape($id);
  295.         $q = "DELETE FROM ". T_COMM . " WHERE comment_id='".$id."'";
  296.         $r = $sql_object->DBQuery($q);
  297.  
  298.         return $r;
  299.     }
  300.  
  301.     public function disapprove($id, $sql_object) {
  302.  
  303.         // Notify the end of the current method
  304.         $this->dispatcher->notify(new sfEvent($this, 'comment.disapprove', array('id' => $id)));
  305.          
  306.         $id = $sql_object->DBEscape($id);
  307.         $q = "UPDATE ". T_COMM . " SET comment_status = 'E' WHERE comment_id='".$id."'";
  308.         $r = $sql_object->DBQuery($q);
  309.  
  310.         return $r;
  311.     }
  312.  
  313.     public function approve($id, $sql_object) {
  314.  
  315.         // Notify the end of the current method
  316.         $this->dispatcher->notify(new sfEvent($this, 'comment.approve', array('id' => $id)));
  317.          
  318.         $id = $sql_object->DBEscape($id);
  319.         $q = "UPDATE ". T_COMM . " SET comment_status = 'P' WHERE comment_id='".$id."'";
  320.         $r = $sql_object->DBQuery($q);
  321.  
  322.         return $r;
  323.  
  324.     }
  325.  
  326.  
  327.  
  328. }
  329.  
  330. ?>

Documentation generated on Thu, 20 Mar 2014 16:46:05 +0100 by phpDocumentor 1.4.1