/**
  * Constructor
  *
  * @param Request $request
  * @return TasksController
  */
 function __construct($request)
 {
     parent::__construct($request);
     $task_id = $this->request->getId('task_id');
     if ($task_id) {
         $this->active_task = Tasks::findById($task_id);
     }
     // if
     if (instance_of($this->active_task, 'Task')) {
         $this->active_task_parent = $this->active_task->getParent();
         if (instance_of($this->active_task_parent, 'ProjectObject')) {
             $this->active_task_parent->prepareProjectSectionBreadcrumb($this->wireframe);
         }
         // if
     } else {
         $this->active_task = new Task();
         $parent_id = $this->request->getId('parent_id');
         if ($parent_id) {
             $parent = ProjectObjects::findById($parent_id);
             if (instance_of($parent, 'ProjectObject')) {
                 $this->active_task_parent = $parent;
                 $this->active_task_parent->prepareProjectSectionBreadcrumb($this->wireframe);
             }
             // if
         }
         // if
     }
     // if
     if (instance_of($this->active_task_parent, 'ProjectObject')) {
         $this->wireframe->addBreadCrumb($this->active_task_parent->getName(), $this->active_task_parent->getViewUrl());
     } else {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     $this->smarty->assign(array('active_task' => $this->active_task, 'active_task_parent' => $this->active_task_parent, 'page_tab' => $this->active_task->getProjectTab()));
 }
 /**
  * Find project objects in commit message, make them links and
  * save the relations to database
  *
  * @param string $commit_message
  * @param string $commit_author
  * @param integer $revision
  * @param Repository $repository
  * @param Project $project
  * @return string
  */
 function analyze_message($commit_message, $commit_author, $revision, $repository, $project)
 {
     if (define('PURIFY_HTML') && PURIFY_HTML) {
         $commit_message = purify_html($commit_message);
         // Clean!
     }
     // if
     $pattern = '/((complete[d]*)[\\s]+)?(ticket|milestone|discussion|task)[s]*[\\s]+[#]*\\d+/i';
     if (preg_match_all($pattern, $commit_message, $matches)) {
         $i = 0;
         $search = array();
         $replace = array();
         $matches_unique = array_unique($matches['0']);
         foreach ($matches_unique as $key => $match) {
             $match_data = preg_split('/[\\s,]+/', $match, null, PREG_SPLIT_NO_EMPTY);
             // check if the object got completed by this commit
             $object_completed = false;
             if (strpos(strtolower($match_data['0']), 'complete') !== false) {
                 $object_completed = true;
                 unset($match_data['0']);
                 $match_data = array_values($match_data);
             }
             // if
             $object_class_name = $match_data['0'];
             $module_name = Inflector::pluralize($object_class_name);
             $object_id = trim($match_data['1'], '#');
             $search[$i] = $match;
             if (class_exists($module_name) && class_exists($object_class_name)) {
                 $object = null;
                 switch (strtolower($module_name)) {
                     case 'tickets':
                         $object = Tickets::findByTicketId($project, $object_id);
                         break;
                     case 'discussions':
                         $object = Discussions::findById($object_id);
                         break;
                     case 'milestones':
                         $object = Milestones::findById($object_id);
                         break;
                     case 'tasks':
                         $object = Tasks::findById($object_id);
                         break;
                 }
                 // switch
                 if (instance_of($object, $object_class_name)) {
                     $link_already_created = CommitProjectObjects::count("object_id = '" . $object->getId() . "' AND revision = '{$revision}'") > 0;
                     if (!$link_already_created) {
                         $comit_project_object = new CommitProjectObject();
                         $comit_project_object->setProjectId($object->getProjectId());
                         $comit_project_object->setObjectId($object->getId());
                         $comit_project_object->setObjectType(ucfirst($object_class_name));
                         $comit_project_object->setRepositoryId($repository->getId());
                         $comit_project_object->setRevision($revision);
                         db_begin_work();
                         $save = $comit_project_object->save();
                         if ($save && !is_error($save)) {
                             db_commit();
                         } else {
                             db_rollback();
                         }
                         // if save
                     }
                     // if
                     $replace[$i] = ($object_completed ? 'Completed ' : '') . '<a href="' . $object->getViewUrl() . '">' . $match_data['0'] . ' ' . $match_data['1'] . '</a>';
                     // set the object as completed
                     if ($object_completed && !instance_of($object, 'Discussion')) {
                         $completed_by = $repository->getMappedUser($commit_author);
                         $object->complete($completed_by);
                     }
                     // if
                 } else {
                     $replace[$i] = ($object_completed ? 'Completed ' : '') . '<a href="#" class="project_object_missing" title="' . lang('Project object does not exist in this project') . '">' . $match_data['0'] . ' ' . $match_data['1'] . '</a>';
                 }
                 // if instance_of
                 $i++;
             }
             // if module loaded
         }
         // foreach
         return str_ireplace($search, $replace, htmlspecialchars($commit_message));
         // linkify
     }
     // if preg_match
     return $commit_message;
 }
 /**
  * Add AC object links to commit messages
  *
  * @param string
  * @return string
  **/
 private function analyzeCommitMessage($commit_message)
 {
     $pattern = '/(ticket|milestone|discussion|task)[s]*[\\s]+[#]*(\\d+)/i';
     if (preg_match_all($pattern, $commit_message, $matches)) {
         $i = 0;
         $search = array();
         $replace = array();
         $matches_unique = array_unique($matches['0']);
         foreach ($matches_unique as $key => $match) {
             $match_data = preg_split('/[\\s,]+/', $match, null, PREG_SPLIT_NO_EMPTY);
             $object_class_name = $match_data['0'];
             $module_name = Inflector::pluralize($object_class_name);
             $object_id = trim($match_data['1'], '#');
             $search[$i] = $match;
             if (class_exists($module_name) && class_exists($object_class_name)) {
                 $object = null;
                 switch (strtolower($module_name)) {
                     case 'tickets':
                         $object = Tickets::findByTicketId($this->active_project, $object_id);
                         break;
                     case 'discussions':
                         $object = Discussions::findById($object_id);
                         break;
                     case 'milestones':
                         $object = Milestones::findById($object_id);
                         break;
                     case 'tasks':
                         $object = Tasks::findById($object_id);
                         break;
                 }
                 // switch
                 if (instance_of($object, $object_class_name)) {
                     $replace[$i] = '<a href="' . $object->getViewUrl() . '">' . $match_data['0'] . ' ' . $match_data['1'] . '</a>';
                 } else {
                     $replace[$i] = '<a href="#" class="project_object_missing" title="' . lang('Project object does not exist in this project') . '">' . $match_data['0'] . ' ' . $match_data['1'] . '</a>';
                 }
                 // if instance_of
                 $i++;
             }
             // if module loaded
         }
         // foreach
         return str_ireplace($search, $replace, htmlspecialchars($commit_message));
         // linkify
     }
     // if preg_match
     return $commit_message;
 }
/**
 * Hnalde on_project_object_copied event
 *
 * @param ProjectObject $original
 * @param ProjectObject $copy
 * @param Project $destination
 * @param mixed $cascade
 * @return null
 */
function resources_handle_on_project_object_copied(&$original, &$copy, &$destination, $cascade)
{
    if ($original->can_have_subscribers) {
        $subscribers = $original->getSubscribers();
        if (is_foreachable($subscribers)) {
            foreach ($subscribers as $subscriber) {
                if ($subscriber->isProjectMember($destination)) {
                    Subscriptions::subscribe($subscriber, $copy);
                }
                // if
            }
            // foreach
        }
        // if
    }
    // if
    if ($original->can_have_assignees) {
        Assignments::cloneAssignments($original, $copy);
    }
    // if
    if ($original->can_have_attachments) {
        Attachments::cloneAttachments($original, $copy);
    }
    // if
    // Copy child objects
    if ($cascade === true || is_foreachable($cascade)) {
        if ($cascade === true) {
            $rows = db_execute_all('SELECT * FROM ' . TABLE_PREFIX . 'project_objects WHERE parent_id = ?', $original->getId());
        } else {
            $rows = db_execute_all('SELECT * FROM ' . TABLE_PREFIX . 'project_objects WHERE parent_id = ? AND type IN (?)', $original->getId(), $cascade);
        }
        // if
        if (is_foreachable($rows)) {
            // We'll remember original and copy tasks ID-s here so we can move
            // assignments later on, when we have both instances
            $tasks = array();
            foreach ($rows as $row) {
                $subobject_original_id = $row['id'];
                $subobject_original_type = strtolower($row['type']);
                unset($row['id']);
                $row['project_id'] = $destination->getId();
                $row['parent_id'] = $copy->getId();
                $row['milestone_id'] = 0;
                // Copy file
                if ($subobject_original_type == 'attachment') {
                    $path = UPLOAD_PATH . '/' . $row['varchar_field_1'];
                    if (is_file($path)) {
                        $destination_file = get_available_uploads_filename();
                        if (copy($path, $destination_file)) {
                            $row['varchar_field_1'] = basename($destination_file);
                        }
                        // if
                    }
                    // if
                }
                // if
                // Escape values
                foreach ($row as $k => $v) {
                    $row[$k] = db_escape($v);
                }
                // foreach
                db_execute('INSERT INTO ' . TABLE_PREFIX . 'project_objects (' . implode(', ', array_keys($row)) . ') VALUES (' . implode(', ', $row) . ')');
                if ($subobject_original_type == 'task') {
                    $tasks[$subobject_original_id] = db_last_insert_id();
                }
                // if
            }
            // foraech
            if (instance_of($copy, 'Discussion')) {
                $last_comment = $copy->getLastComment();
                $last_comment_datetime = instance_of($last_comment, 'Comment') ? $last_comment->getCreatedOn() : null;
                $copy->setLastCommentOn($last_comment_datetime);
                $copy->save();
            }
            // if
            // Lets move task assinments if we have any tasks
            if (is_foreachable($tasks)) {
                foreach ($tasks as $task_original_id => $task_copy_id) {
                    $task_original = Tasks::findById($task_original_id);
                    $task_copy = Tasks::findById($task_copy_id);
                    if (instance_of($task_original, 'Task') && instance_of($task_copy, 'Task')) {
                        Assignments::cloneAssignments($task_original, $task_copy);
                        Subscriptions::cloneSubscriptions($task_original, $task_copy);
                    }
                    // if
                }
                // foreach
            }
            // if
        }
        // if
    }
    // if
}