Skip to content
Snippets Groups Projects
Select Git revision
  • d24a83b0815d62a6d654db96d205d57192a16a24
  • master default
  • disable-new-requests
  • fix-bulletin-view-missing-notes-error
  • add-missing-queue-managers
  • projects-task-53
  • projects-task-51
  • projects-task-43
  • projects-task-24
  • projects-task-31
  • projects-task-32
  • projects-task-8
  • project-setup-docs
  • projects-task-28
  • projects-task-27
  • projects-task-9
  • projects-task-7
  • mass-update-course-codes-in-sections
  • wdn-four
  • learning-outcomes
  • additional-bulletin-pages
  • svn-redesign
  • svn-popups
  • svn-trunk
  • svn-performance
  • svn-tim
26 results

CommentsModel.php

Blame
  • CommentsModel.php 7.48 KiB
    <?php
    
    class Requests_CommentsModel extends Unl_Model
    {
        static public function find($id)
        {
            $db = Zend_Registry::get('db');
    
            $select = new Zend_Db_Select($db);
            $select->from(array('c' => 'creqRequestComments'));
            if (Unl_Util::isArray($id)) {
            	if (count($id) == 0) {
            		return new Unl_Model_Collection(__CLASS__);
            	}
                $select->where('c.requestCommentId IN(?)', $id);
            } else {
                $select->where('c.requestCommentId = ?', $id);
            }
    
            $records = $db->query($select)->fetchAll();
            $objects = new Unl_Model_Collection(__CLASS__);
            $commentUsers = array();
            foreach ($records as $record) {
                $object = Unl_Model_Registry::getInstance()->getOrAdd(new self($record));
                $objectId = $object->getId();
                $objects[$objectId] = $object;
                $commentUsers[$objectId] = $record['user'];
            }
    
            $users = Auth_UserModel::find($commentUsers);
    
            foreach ($objects as $object) {
            	$userId = $object->_data['user'];
            	$object->_data['userObject'] = $users[$userId];
            }
    
            if (Unl_Util::isArray($id)) {
                return $objects;
            } else {
                return $objects->pop();
            }
        }
    
        static public function findByRequest($request, $hideLowPriority = false)
        {
            $db = Zend_Registry::get('db');
            $requestId = $request->getId();
    
            if (Unl_Util::isArray($requestId) && count($requestId) == 0) {
                return new Unl_Model_Collection(__CLASS__);
            } else if (!Unl_Util::isArray($requestId) && !$requestId) {
                return null;
            }
    
            $select = new Zend_Db_Select($db);
            $select->from(array('c' => 'creqRequestComments'), array('requestCommentId', 'request'));
            if (Unl_Util::isArray($requestId)) {
                $select->where('c.request IN(?)', $requestId);
            } else {
                $select->where('c.request = ?', $requestId);
            }
            if ($hideLowPriority) {
                $select->where('c.visibility NOT IN("editorial", "self")');
            }
    
            $records = $db->query($select)->fetchAll();
            $requestComments = array();
            $commentIds = array();
            foreach ($records as $record) {
            	$rId = $record['request'];
            	$cId = $record['requestCommentId'];
            	$requestComments[$rId][] = $cId;
            	$commentIds[] = $cId;
            }
    
            self::find($commentIds);
    
            $objects = array();
            if (Unl_Util::isArray($requestId)) {
            	foreach ($requestId as $rId) {
            		$objects[$rId] = new Unl_Model_Collection(__CLASS__);
            	}
            } else {
            	$objects[$requestId] = new Unl_Model_Collection(__CLASS__);
            }
    
            foreach ($requestComments as $rId => $cIds) {
            	foreach ($cIds as $cId) {
            		$objects[$rId][$cId] = Unl_Model_Registry::getInstance()->get(__CLASS__, $cId);
            		$objects[$rId][$cId]->_setClean();
            	}
            }
    
            if (Unl_Util::isArray($requestId)) {
                return $objects;
            } else {
                return $objects[$requestId];
            }
        }
    
        /**
         * Gets a freshly created model object
         *
         * @return Requests_CommentsModel
         */
        static function fetchNew()
        {
        	return new self(array());
        }
    
        static public function save($models)
        {
            $modelsToInsert = new Unl_Model_Collection(__CLASS__);
        	$modelsToUpdate = new Unl_Model_Collection(__CLASS__);
    
        	if (!Unl_Util::isArray($models)) {
        	    $model = $models;
        	    $models = new Unl_Model_Collection(__CLASS__);
        	    $models[$model->getId()] = $model;
        	}
    
        	foreach ($models as $model) {
        		if ($model->getId()) {
        			if ($model->_cleanData != $model->_data) {
        			    $modelsToUpdate[] = $model;
        			}
        		} else {
        			$modelsToInsert[] = $model;
        		}
        	}
    
            if (count($modelsToInsert) > 0) {
                self::_insert($modelsToInsert);
            }
    
            if (count($modelsToUpdate) > 0) {
                self::_update($modelsToUpdate);
            }
    
            foreach ($models as $model)
            {
                $model->_setClean();
            }
        }
    
        static protected function _update(Unl_Model_Collection $models)
        {
        	$db = Zend_Registry::get('db');
    
            $sql = 'CREATE TEMPORARY TABLE creqRequestCommentsUpdate '
                 . 'SELECT * FROM creqRequestComments LIMIT 0';
            $db->query($sql);
    
            $sql = 'INSERT INTO creqRequestCommentsUpdate VALUES ';
            $sqlParts = array();
        	foreach ($models as $model) {
        		$sqlParts[] = $db->quoteInto('(?, ', $model->_data['requestCommentId'])
                            . $db->quoteInto('?, ' , $model->_data['user'])
                            . $db->quoteInto('?, ' , $model->_data['request'])
                            . $db->quoteInto('?, ' , $model->_data['comment'])
                            . $db->quoteInto('?, ' , $model->_data['postTime'])
                            . $db->quoteInto('?)'  , $model->_data['visibility']);
        	}
        	$sql .= implode(', ', $sqlParts);
        	$db->query($sql);
    
        	$sql = 'UPDATE creqRequestComments AS a, '
        	     . '       creqRequestCommentsUpdate AS b '
        	     . 'SET a.user       = b.user, '
                 . '    a.request    = b.request, '
                 . '    a.comment    = b.comment, '
                 . '    a.postTime   = b.postTime, '
                 . '    a.visibility = b.visibility '
                 . 'WHERE a.requestCommentId = b.requestCommentId ';
            $db->query($sql);
        }
    
        static protected function _insert(Unl_Model_Collection $models)
        {
            $db = Zend_Registry::get('db');
    
        	$sql = 'INSERT INTO creqRequestComments (user, request, comment, postTime, visibility) VALUES ';
            $sqlParts = array();
            foreach ($models as $model) {
                $sqlParts[] = $db->quoteInto('(?, ', $model->_data['user'])
                            . $db->quoteInto('?, ' , $model->_data['request'])
                            . $db->quoteInto('?, ' , $model->_data['comment'])
                            . $db->quoteInto('?, ' , $model->_data['postTime'])
                            . $db->quoteInto('?)'  , $model->_data['visibility']);
            }
            $sql .= implode(', ', $sqlParts);
            $db->query($sql);
        }
    
        public function getId()
        {
        	return $this->_data['requestCommentId'];
        }
    
        /**
         * Returns the time the comment was posted.
         *
         * @return Zend_Date
         */
        public function getTime()
        {
        	$time = new Zend_Date($this->_data['postTime']);
        	return $time;
        }
    
        /**
         * Sets the date the comment was posted
         *
         * @param Zend_Date $time
         */
        public function setTime(Zend_Date $time)
        {
        	$this->_data['postTime'] = $time->getTimestamp();
        }
    
        public function getVisibility()
        {
        	return $this->_data['visibility'];
        }
    
        public function setVisibility($visibility)
        {
        	$this->_data['visibility'] = $visibility;
        }
    
        public function getText()
        {
        	return $this->_data['comment'];
        }
    
        public function setText($text)
        {
        	$this->_data['comment'] = $text;
        }
    
        /**
         * Gets the user model for the user who authored the comment
         *
         * @return Auth_UserModel
         */
        public function getUser()
        {
        	return $this->_data['userObject'];
        }
    
        public function setUser(Auth_UserModel $user)
        {
        	$this->_data['user'] = $user->getId();
        	$this->_data['userObject'] = $user;
        }
    
        public function setRequest(Requests_RequestModel $request)
        {
        	$this->_data['request'] = $request->getId();
        }
    
    }