/**
  * Update project
  *
  * @param void
  * @return null
  */
 function edit()
 {
     $this->wireframe->print_button = false;
     if ($this->request->isApiCall() && !$this->request->isSubmitted()) {
         $this->httpError(HTTP_ERR_BAD_REQUEST, null, true, true);
     }
     // if
     if ($this->active_project->isNew()) {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     if (!$this->active_project->canEdit($this->logged_user)) {
         $this->httpError(HTTP_ERR_FORBIDDEN);
     }
     // if
     $project_data = $this->request->post('project');
     if (!is_array($project_data)) {
         $project_data = array('name' => $this->active_project->getName(), 'overview' => $this->active_project->getOverview(), 'default_visibility' => $this->active_project->getDefaultVisibility(), 'leader_id' => $this->active_project->getLeaderId(), 'group_id' => $this->active_project->getGroupId(), 'company_id' => $this->active_project->getCompanyId(), 'default_visibility' => $this->active_project->getDefaultVisibility(), 'starts_on' => $this->active_project->getStartsOn());
     }
     // if
     $this->smarty->assign('project_data', $project_data);
     if ($this->request->isSubmitted()) {
         db_begin_work();
         $old_name = $this->active_project->getName();
         $this->active_project->setAttributes($project_data);
         if ($this->active_project->isModified('leader_id') && $this->active_project->getLeaderId()) {
             $leader = Users::findById($this->active_project->getLeaderId());
             if (instance_of($leader, 'User')) {
                 $this->active_project->setLeader($leader);
             }
             // if
         }
         // if
         if ($this->active_project->isModified('company_id')) {
             cache_remove('project_icons');
         }
         // if
         $save = $this->active_project->save();
         if ($save && !is_error($save)) {
             db_commit();
             if ($this->request->isApiCall()) {
                 $this->serveData($this->active_project, 'project');
             } else {
                 flash_success('Project :name has been updated', array('name' => $old_name));
                 $this->redirectToUrl($this->active_project->getOverviewUrl());
             }
             // if
         } else {
             db_rollback();
             if ($this->request->isApiCall()) {
                 $this->serveData($save);
             } else {
                 $this->smarty->assign('errors', $save);
             }
             // if
         }
         // if
     }
     // if
 }
 /**
  * Index page action
  *
  */
 function index()
 {
     if (!module_loaded('tickets')) {
         $this->redirectTo('public_submit_unavailable');
     }
     // if
     $ticket_data = $this->request->post('ticket');
     $this->smarty->assign(array('captcha_url' => ROOT_URL . '/captcha.php?id=' . md5(time()), "ticket_data" => $ticket_data));
     if ($this->request->isSubmitted()) {
         $errors = new ValidationErrors();
         if ($this->captcha_enabled) {
             //$captcha_value = array_var($_SESSION, CAPTCHA_SESSION_ID);
             if (!Captcha::Validate($ticket_data['captcha'])) {
                 $errors->addError(lang('Code you entered is not valid'), 'captcha');
                 $this->smarty->assign('errors', $errors);
             }
             // if
         }
         // if
         if (!$errors->hasErrors()) {
             $submitter = new AnonymousUser($ticket_data['created_by_name'], $ticket_data['created_by_email']);
             db_begin_work();
             $ticket = new Ticket();
             attach_from_files($ticket, $submitter);
             $ticket->setAttributes($ticket_data);
             $ticket->setProjectId($this->active_project->getId());
             $ticket->setVisibility(VISIBILITY_NORMAL);
             $ticket->setState(STATE_VISIBLE);
             $ticket->setCreatedBy($submitter);
             $save = $ticket->save();
             if (!$save || is_error($save)) {
                 unset($ticket_data['captcha']);
                 db_rollback();
                 $this->smarty->assign(array('ticket_data' => $ticket_data, 'errors' => $save));
             } else {
                 Subscriptions::subscribeUsers(array($this->active_project->getLeaderId()), $ticket);
                 db_commit();
                 $ticket->ready();
                 $this->redirectTo('public_submit_success');
             }
             // if
         }
         // if
     }
     // if
 }
 /**
  * Returns true if this person is leader of specified project
  *
  * @param Project $project
  * @return boolean
  */
 function isProjectLeader($project)
 {
     return $this->getId() == $project->getLeaderId();
 }
 /**
  * Copy project items into a destination project
  *
  * @param Project $to
  * @return null
  */
 function copyItems(&$to)
 {
     // Prepare time diff
     $source_starts_on = $this->getStartsOn();
     if (!instance_of($source_starts_on, 'DateValue')) {
         $source_starts_on = $this->getCreatedOn();
     }
     // if
     $target_starts_on = $to->getStartsOn();
     if (!instance_of($target_starts_on, 'DateValue')) {
         $target_starts_on = $to->getCreatedOn();
     }
     // if
     $diff = $target_starts_on->getTimestamp() - $source_starts_on->getTimestamp();
     // Migrate project users
     $project_users = ProjectUsers::findByProject($this);
     if (is_foreachable($project_users)) {
         foreach ($project_users as $project_user) {
             if ($to->getLeaderId() != $project_user->getUserId()) {
                 $user = $project_user->getUser();
                 if (instance_of($user, 'User')) {
                     $to->addUser($user, $project_user->getRole(), $project_user->getPermissions());
                 }
                 // if
             }
             // if
         }
         // foreach
     }
     // if
     // We need to move milestones in order to get milestones map
     $milestones_map = null;
     $milestones = Milestones::findAllByProject($this, VISIBILITY_PRIVATE);
     if (is_foreachable($milestones)) {
         $milestones_map = array();
         foreach ($milestones as $milestone) {
             $copied_milestone = $milestone->copyToProject($to);
             if (instance_of($copied_milestone, 'Milestone')) {
                 $copied_milestone->advance($diff, true);
                 $milestones_map[$milestone->getId()] = $copied_milestone;
             }
             // if
         }
         // foreach
     }
     // if
     // Now move categories
     $categories_map = null;
     $categories = Categories::findByProject($this);
     if (is_foreachable($categories)) {
         foreach ($categories as $category) {
             $copied_category = $category->copyToProject($to, null, null, false);
             if (instance_of($copied_category, 'Category')) {
                 $categories_map[$category->getId()] = $copied_category;
             }
             // if
         }
         // foreach
     }
     // if
     // Let the modules to their thing
     event_trigger('on_copy_project_items', array(&$this, &$to, $milestones_map, $categories_map));
     // Now, lets update due dates
     $completable_types = get_completable_project_object_types();
     if (is_foreachable($completable_types)) {
         foreach ($completable_types as $k => $type) {
             if (strtolower($type) == 'milestone') {
                 unset($completable_types[$k]);
             }
             // if
         }
         // foreach
         if (count($completable_types) > 0) {
             $rows = db_execute_all('SELECT id, due_on FROM ' . TABLE_PREFIX . 'project_objects WHERE project_id = ? AND type IN (?) AND due_on IS NOT NULL', $to->getId(), $completable_types);
             if (is_foreachable($rows)) {
                 foreach ($rows as $row) {
                     $id = (int) $row['id'];
                     $new_date = date(DATE_MYSQL, strtotime($row['due_on']) + $diff);
                     db_execute('UPDATE ' . TABLE_PREFIX . 'project_objects SET due_on = ? WHERE id = ?', $new_date, $id);
                     cache_remove("acx_project_objects_id_{$id}");
                 }
                 // foreach
             }
             // if
         }
         // if
     }
     // if
     // Refresh tasks count, just in case...
     $to->refreshTasksCount();
 }
 /**
  * Import pending email as discussion
  *
  * @param IncomingMail $incoming_mail
  * @param Project $project
  * @param User $user
  * @return Discussion
  */
 function importPendingEmailAsDiscussion(&$incoming_mail, &$project, &$user)
 {
     $discussion = new Discussion();
     $discussion->setProjectId($project->getId());
     $discussion->setCreatedBy($user);
     $discussion->setCreatedOn($incoming_mail->getCreatedOn());
     $discussion->setVisibility(VISIBILITY_NORMAL);
     $discussion->setState(STATE_VISIBLE);
     $discussion->setSource(OBJECT_SOURCE_EMAIL);
     $discussion->setName($incoming_mail->getSubject());
     $discussion->setBody($incoming_mail->getBody());
     IncomingMailImporter::attachFilesToProjectObject($incoming_mail, $discussion);
     $save = $discussion->save();
     if ($save && !is_error($save)) {
         $subscibed_users = array($project->getLeaderId());
         if (instance_of($user, 'User')) {
             $subscibed_users[] = $user->getId();
         }
         // if
         Subscriptions::subscribeUsers($subscibed_users, $discussion);
         $discussion->ready();
         return $discussion;
     }
     // if
     return $save;
 }