/**
  * View single ticket
  * 
  * @param int $id
  * @param string $alias
  */
 public function actionViewTicket($id, $alias)
 {
     if ($id && ($ticket = Tickets::model()->with(array('reporter', 'assigned', 'status', 'type', 'category', 'ticketpriority', 'version', 'fixed', 'ticketmilestone', 'comments', 'comments.commentreporter', 'comments.changes', 'lastcomment'))->byCommentDate()->findByPk($id))) {
         // Initiate the ticket comment model
         $comment = new TicketComments();
         $props = new TicketProps();
         $ticketChanges = array();
         $commentPassed = false;
         $propsPassed = false;
         $propertiesPassed = false;
         // Make sure if we submit the form that we enter a comment
         if (isset($_POST['TicketComments'])) {
             $comment->setAttributes($_POST['TicketComments']);
             $comment->ticketid = $ticket->id;
             if ($comment->validate()) {
                 $commentPassed = true;
             }
         }
         // Make suer we submitted the form
         if (isset($_POST['TicketProps'])) {
             $props->setAttributes($_POST['TicketProps']);
             if ($props->validate()) {
                 $propsPassed = true;
                 // did we change the title
                 if ($props->title != $ticket->title) {
                     $ticketChanges[] = Yii::t('tickets', '<strong>Title</strong> Changed from <em>{old}</em> To <em>{new}</em>', array('{old}' => $ticket->title, '{new}' => $props->title));
                 }
                 // did we change the keywords
                 if ($props->keywords != $ticket->keywords) {
                     $ticketChanges[] = Yii::t('tickets', '<strong>Keywords</strong> Changed from <em>{old}</em> To <em>{new}</em>', array('{old}' => $ticket->keywords, '{new}' => $props->keywords));
                 }
                 // did we change the assigned to
                 if ($props->assignedtoid && $props->assignedtoid != $ticket->assignedtoid) {
                     // Load the users
                     $oldUser = Users::model()->findByPk($ticket->assignedtoid);
                     $newUser = Users::model()->find('username=:username', array(':username' => $props->assignedtoid));
                     $ticketChanges[] = Yii::t('tickets', '<strong>Assigned</strong> Changed from <em>{old}</em> To <em>{new}</em>', array('{old}' => $oldUser ? $oldUser->getLink() : "''", '{new}' => $newUser ? $newUser->getLink() : "''"));
                 }
                 // Did we change the status
                 if ($props->status && $props->status != $ticket->ticketstatus) {
                     $oldStatus = TicketStatus::model()->findByPk($ticket->ticketstatus);
                     $newStatus = TicketStatus::model()->findByPk($props->status);
                     $ticketChanges[] = Yii::t('tickets', '<strong>Status</strong> Changed from <em>{old}</em> To <em>{new}</em>', array('{old}' => $oldStatus ? $oldStatus->getLink() : "''", '{new}' => $newStatus ? $newStatus->getLink() : "''"));
                     $ticket->ticketstatus = $props->status;
                 }
                 // Did we change the type
                 if ($props->type && $props->type != $ticket->tickettype) {
                     $oldType = TicketType::model()->findByPk($ticket->tickettype);
                     $newType = TicketType::model()->findByPk($props->type);
                     $ticketChanges[] = Yii::t('tickets', '<strong>Type</strong> Changed from <em>{old}</em> To <em>{new}</em>', array('{old}' => $oldType ? $oldType->getLink() : "''", '{new}' => $newType ? $newType->getLink() : "''"));
                     $ticket->tickettype = $props->type;
                 }
                 // Did we change the category
                 if ($props->category && $props->category != $ticket->ticketcategory) {
                     $oldCategory = TicketCategory::model()->findByPk($ticket->ticketcategory);
                     $newCategory = TicketCategory::model()->findByPk($props->category);
                     $ticketChanges[] = Yii::t('tickets', '<strong>Category</strong> Changed from <em>{old}</em> To <em>{new}</em>', array('{old}' => $oldCategory ? $oldCategory->getLink() : "''", '{new}' => $newCategory ? $newCategory->getLink() : "''"));
                     $ticket->ticketcategory = $props->category;
                 }
                 // Did we change the version
                 if ($props->version && $props->version != $ticket->ticketversion) {
                     $oldVersion = TicketVersion::model()->findByPk($ticket->ticketversion);
                     $newVersion = TicketVersion::model()->findByPk($props->version);
                     $ticketChanges[] = Yii::t('tickets', '<strong>Version</strong> Changed from <em>{old}</em> To <em>{new}</em>', array('{old}' => $oldVersion ? $oldVersion->getVersionLink() : "''", '{new}' => $newVersion ? $newVersion->getVersionLink() : "''"));
                     $ticket->ticketversion = $props->version;
                 }
                 // Did we change the fixedin
                 if ($props->fixedin && $props->fixedin != $ticket->fixedin) {
                     $oldFixed = TicketVersion::model()->findByPk($ticket->fixedin);
                     $newFixed = TicketVersion::model()->findByPk($props->fixedin);
                     $ticketChanges[] = Yii::t('tickets', '<strong>Fixed In Version</strong> Changed from <em>{old}</em> To <em>{new}</em>', array('{old}' => $oldFixed ? $oldFixed->getFixedinLink() : "''", '{new}' => $newFixed ? $newFixed->getFixedinLink() : "''"));
                     $ticket->fixedin = $props->fixedin;
                 }
                 // Did we change the priority
                 if ($props->priority && $props->priority != $ticket->priority) {
                     $oldPriority = TicketPriority::model()->findByPk($ticket->priority);
                     $newPriority = TicketPriority::model()->findByPk($props->priority);
                     $ticketChanges[] = Yii::t('tickets', '<strong>Priority</strong> Changed from <em>{old}</em> To <em>{new}</em>', array('{old}' => $oldPriority ? $oldPriority->getLink() : "''", '{new}' => $newPriority ? $newPriority->getLink() : "''"));
                     $ticket->priority = $props->priority;
                 }
                 // Assign props data to ticket
                 $ticket->title = $props->title;
                 $ticket->keywords = $props->keywords;
                 // Reset the ticket assigned to
                 if ($props->assignedtoid) {
                     $ticket->assignedtoid = $props->assignedtoid;
                 } else {
                     $ticket->assignedtoid = (int) $ticket->assignedtoid;
                 }
                 // Make sure we have some changes or at least entered comment
                 if (!$comment->content && !count($ticketChanges)) {
                     $commentPassed = false;
                     Functions::setFlash(Yii::t('tickets', 'You must enter a comment or make some changes to the ticket.'));
                 }
                 // validate ticket
                 if ($ticket->validate()) {
                     $propertiesPassed = true;
                 }
             }
         }
         // Only if all flags were ok then save and update
         if (isset($_POST['submit']) && $commentPassed && $propsPassed && $propertiesPassed) {
             // Save both and redirect
             $comment->save();
             // Did we change something?
             if (count($ticketChanges)) {
                 foreach ($ticketChanges as $change) {
                     $changes = new TicketChanges();
                     $changes->ticketid = $ticket->id;
                     $changes->commentid = $comment->id;
                     $changes->content = $change;
                     $changes->save();
                 }
             }
             // Save ticket
             $ticket->lastcommentid = $comment->id;
             $ticket->update();
             // Set flash
             Functions::setFlash(Yii::t('tickets', 'Tickets: Ticket Updated'));
             $this->redirect($ticket->getLink('', array(), true));
         }
         // Reset the ticket assigned to
         if ($props->assignedtoid) {
             if (is_int($props->assignedtoid)) {
                 $username = Users::model()->findByPk($props->assignedtoid);
             } else {
                 $username = Users::model()->find('username=:username', array(':username' => $props->assignedtoid));
             }
             $props->assignedtoid = $username ? $username->username : '';
         } else {
             $props->assignedtoid = '';
         }
         // Assign keywords
         if (!$props->keywords && $ticket->keywords) {
             $props->keywords = $ticket->keywords;
         }
         // Assign Title
         if (!$props->title && $ticket->title) {
             $props->title = $ticket->title;
         }
         // Show the view screen
         $this->pageTitle[] = Yii::t('tickets', 'Viewing Ticket - {title}', array('{title}' => CHtml::encode($ticket->title)));
         $this->render('showticket', array('props' => $props, 'ticket' => $ticket, 'comment' => $comment));
     } else {
         $this->redirect(array('/tickets'));
     }
 }
 /**
  * Return number of ticket changes
  *
  * @param void
  * @return integer
  */
 function countChanges()
 {
     if ($this->changes_count === false) {
         $this->changes_count = TicketChanges::countByTicket($this);
     }
     // if
     return $this->changes_count;
 }