/**
  * This function will return paginated result. Result is an array where first element is 
  * array of returned object and second populated pagination object that can be used for 
  * obtaining and rendering pagination data using various helpers.
  * 
  * Items and pagination array vars are indexed with 0 for items and 1 for pagination
  * because you can't use associative indexing with list() construct
  *
  * @access public
  * @param array $arguments Query argumens (@see find()) Limit and offset are ignored!
  * @param integer $items_per_page Number of items per page
  * @param integer $current_page Current page number
  * @return array
  */
 function paginate($arguments = null, $items_per_page = 10, $current_page = 1)
 {
     if (isset($this) && instance_of($this, 'ObjectHandins')) {
         return parent::paginate($arguments, $items_per_page, $current_page);
     } else {
         return ObjectHandins::instance()->paginate($arguments, $items_per_page, $current_page);
         //$instance =& ObjectHandin::instance();
         //return $instance->paginate($arguments, $items_per_page, $current_page);
     }
     // if
 }
 /**
  * Return manager instance
  *
  * @access protected
  * @param void
  * @return ObjectHandins
  */
 function manager()
 {
     if (!$this->manager instanceof ObjectHandins) {
         $this->manager = ObjectHandins::instance();
     }
     return $this->manager;
 }
 /**
  * Update, delete and add new handins
  *
  * @access public
  * @param void
  * @return null
  */
 function update_handins()
 {
     $this->setTemplate('add_handins');
     $manager_class = array_var($_GET, 'manager');
     $object_id = get_id('object_id');
     $obj = get_object_by_manager_and_id($object_id, $manager_class);
     if (!$obj instanceof ProjectDataObject) {
         flash_error(lang('object dnx'));
         ajx_current("empty");
         return;
     }
     // if
     if ($obj->canEdit()) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     // if
     $new_handins = array_var($_POST, 'new_handins');
     $update_handins = array_var($_POST, 'update_handins');
     $delete_handins = array_var($_POST, 'delete_handins');
     if (is_array(array_var($_POST, 'new_handins')) || is_array(array_var($_POST, 'update_handins')) || is_array(array_var($_POST, 'delete_handins'))) {
         try {
             DB::beginWork();
             //add new handins
             foreach ($new_handins as $handin) {
                 $handin = new ObjectHandin();
                 $handin->setFromAttributes($handin);
                 $handin->setRelObjectId($object_id);
                 $handin->setRelObjectManager($manager_class);
                 $handin->save();
             }
             foreach ($update_handins as $handin) {
                 $handin = ObjectHandins::getHandin(array_var($handin, 'id'));
                 $handin->setFromAttributes($handin);
                 $handin->save();
             }
             foreach ($delete_handins as $handin) {
                 $handin = ObjectHandins::getHandin(array_var($handin, 'id'));
                 $handin->delete();
             }
             tpl_assign('handins', ObjectHandins::getAllHandinsByObject($obj));
             ApplicationLogs::createLog($obj, $obj->getWorkspaces(), ApplicationLogs::ACTION_EDIT);
             DB::commit();
             flash_success(lang('success add handins'));
             $this->redirectToReferer($obj->getObjectUrl());
         } catch (Exception $e) {
             DB::rollback();
             flash_error($e->getMessage());
             ajx_current("empty");
         }
         //
     }
     // if
 }
 /**
  * Edit task
  *
  * @access public
  * @param void
  * @return null
  */
 function edit_task()
 {
     if (logged_user()->isGuest()) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     $this->setTemplate('add_task');
     $task = ProjectTasks::findById(get_id());
     if (!$task instanceof ProjectTask) {
         flash_error(lang('task list dnx'));
         ajx_current("empty");
         return;
     }
     // if
     if (!$task->canEdit(logged_user())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     // if
     $task_data = array_var($_POST, 'task');
     if (!is_array($task_data)) {
         $this->getRepeatOptions($task, $occ, $rsel1, $rsel2, $rsel3, $rnum, $rend, $rjump);
         $tag_names = $task->getTagNames();
         $task_data = array('title' => array_var($_POST, 'title', $task->getTitle()), 'text' => $task->getText(), 'milestone_id' => array_var($_POST, 'milestone_id', $task->getMilestoneId()), 'due_date' => getDateValue(array_var($_POST, 'task_due_date'), $task->getDueDate()), 'start_date' => getDateValue(array_var($_POST, 'task_start_date', $task->getStartDate())), 'parent_id' => $task->getParentId(), 'project_id' => array_var($_POST, 'project_id', $task->getProjectId()), 'tags' => is_array($tag_names) && count($tag_names) ? implode(', ', $tag_names) : '', 'is_private' => $task->isPrivate(), 'assigned_to' => array_var($_POST, 'assigned_to', $task->getAssignedToCompanyId() . ':' . $task->getAssignedToUserId()), 'priority' => array_var($_POST, 'priority', $task->getPriority()), 'send_notification' => array_var($_POST, 'notify') == 'true', 'time_estimate' => $task->getTimeEstimate(), 'forever' => $task->getRepeatForever(), 'rend' => $rend, 'rnum' => $rnum, 'rjump' => $rjump, 'rsel1' => $rsel1, 'rsel2' => $rsel2, 'rsel3' => $rsel3, 'occ' => $occ, 'repeat_by' => $task->getRepeatBy(), 'object_subtype' => array_var($_POST, "object_subtype", $task->getObjectSubtype() != 0 ? $task->getObjectSubtype() : config_option('default task co type')));
         // array
         $handins = ObjectHandins::getAllHandinsByObject($task);
         $id = 0;
         if ($handins) {
             foreach ($handins as $handin) {
                 $task_data['handin' . $id] = array('title' => $handin->getTitle(), 'assigned_to' => $handin->getResponsibleCompanyId() . ':' . $handin->getResponsibleUserId());
                 // array
                 $id = $id + 1;
                 if ($id > 3) {
                     break;
                 }
             }
             // foreach
         }
         // if
     }
     // if
     tpl_assign('task', $task);
     tpl_assign('task_data', $task_data);
     if (is_array(array_var($_POST, 'task'))) {
         //MANAGE CONCURRENCE WHILE EDITING
         $upd = array_var($_POST, 'updatedon');
         if ($upd && $task->getUpdatedOn()->getTimestamp() > $upd && !array_var($_POST, 'merge-changes') == 'true') {
             ajx_current('empty');
             evt_add("handle edit concurrence", array("updatedon" => $task->getUpdatedOn()->getTimestamp(), "genid" => array_var($_POST, 'genid')));
             return;
         }
         if (array_var($_POST, 'merge-changes') == 'true') {
             $this->setTemplate('view_list');
             $edited_task = ProjectTasks::findById($task->getId());
             ajx_set_no_toolbar(true);
             ajx_set_panel(lang('tab name', array('name' => $edited_task->getTitle())));
             tpl_assign('task_list', $edited_task);
             ajx_extra_data(array("title" => $edited_task->getTitle(), 'icon' => 'ico-task'));
             return;
         }
         $old_owner = $task->getAssignedTo();
         if (array_var($task_data, 'parent_id') == $task->getId()) {
             flash_error(lang("task own parent error"));
             ajx_current("empty");
             return;
         }
         $old_is_private = $task->isPrivate();
         $old_project_id = $task->getProjectId();
         $project_id = array_var($_POST, 'ws_ids', 0);
         if ($old_project_id != $project_id) {
             $newProject = Projects::findById($project_id);
             if (!$newProject instanceof Project || !$task->canAdd(logged_user(), $newProject)) {
                 flash_error(lang('no access permissions'));
                 ajx_current("empty");
                 return;
             }
         }
         $task_data['due_date'] = getDateValue(array_var($_POST, 'task_due_date'));
         $task_data['start_date'] = getDateValue(array_var($_POST, 'task_start_date'));
         try {
             $err_msg = $this->setRepeatOptions($task_data);
             if ($err_msg) {
                 flash_error($err_msg);
                 ajx_current("empty");
                 return;
             }
             if (!isset($task_data['parent_id'])) {
                 $task_data['parent_id'] = 0;
             }
             $was_template = $task->getIsTemplate();
             $task->setFromAttributes($task_data);
             $task->setIsTemplate($was_template);
             // is_template value must not be changed from ui
             // Set assigned to
             $assigned_to = explode(':', array_var($task_data, 'assigned_to', ''));
             $company_id = array_var($assigned_to, 0, 0);
             $user_id = array_var($assigned_to, 1, 0);
             $can_assign = can_assign_task_to_company_user(logged_user(), $task, $company_id, $user_id);
             if ($can_assign !== true) {
                 flash_error($can_assign);
                 return;
             }
             $task->setAssignedToCompanyId($company_id);
             $task->setAssignedToUserId($user_id);
             if (!logged_user()->isMemberOfOwnerCompany()) {
                 $task->setIsPrivate($old_is_private);
             }
             $totalMinutes = array_var($task_data, 'time_estimate_hours') * 60 + array_var($task_data, 'time_estimate_minutes');
             $task->setTimeEstimate($totalMinutes);
             //Add handins
             $handins = array();
             for ($i = 0; $i < 4; $i++) {
                 if (isset($task_data["handin{$i}"]) && is_array($task_data["handin{$i}"]) && trim(array_var($task_data["handin{$i}"], 'title')) != '') {
                     $assigned_to = explode(':', array_var($task_data["handin{$i}"], 'assigned_to', ''));
                     $handins[] = array('title' => array_var($task_data["handin{$i}"], 'title'), 'responsible_company_id' => array_var($assigned_to, 0, 0), 'responsible_user_id' => array_var($assigned_to, 1, 0));
                     // array
                 }
                 // if
             }
             // for
             if ($task->getParentId() > 0 && $task->hasChild($task->getParentId())) {
                 flash_error(lang('task child of child error'));
                 ajx_current("empty");
                 return;
             }
             DB::beginWork();
             $task->save();
             $task->setTagsFromCSV(array_var($task_data, 'tags'));
             $object_controller = new ObjectController();
             $object_controller->add_to_workspaces($task, !$task->getIsTemplate());
             $object_controller->link_to_new_object($task);
             $object_controller->add_subscribers($task);
             $object_controller->add_custom_properties($task);
             $object_controller->add_reminders($task);
             // apply values to subtasks
             $subtasks = $task->getAllSubTasks();
             $project = $task->getProject();
             $milestone_id = $task->getMilestoneId();
             $apply_ws = array_var($task_data, 'apply_ws_subtasks') == "checked";
             $apply_ms = array_var($task_data, 'apply_milestone_subtasks') == "checked";
             $apply_at = array_var($task_data, 'apply_assignee_subtasks', '') == "checked";
             foreach ($subtasks as $sub) {
                 $modified = false;
                 if ($apply_at || !$sub->getAssignedTo() instanceof ApplicationDataObject) {
                     $sub->setAssignedToCompanyId($company_id);
                     $sub->setAssignedToUserId($user_id);
                     $modified = true;
                 }
                 if ($apply_ws) {
                     $sub->setProject($project);
                     $modified = true;
                 }
                 if ($apply_ms) {
                     $sub->setMilestoneId($milestone_id);
                     $modified = true;
                 }
                 if ($modified) {
                     $sub->save();
                 }
             }
             $task->resetIsRead();
             ApplicationLogs::createLog($task, $task->getWorkspaces(), ApplicationLogs::ACTION_EDIT);
             DB::commit();
             try {
                 if (array_var($task_data, 'send_notification') == 'checked') {
                     $new_owner = $task->getAssignedTo();
                     if ($new_owner instanceof User) {
                         Notifier::taskAssigned($task);
                     }
                     // if
                 }
                 // if
             } catch (Exception $e) {
             }
             // try
             flash_success(lang('success edit task list', $task->getTitle()));
             ajx_current("back");
         } catch (Exception $e) {
             DB::rollback();
             flash_error($e->getMessage());
             ajx_current("empty");
         }
         // try
     }
     // if
 }
 /**
  * Drop all tasks that are in this list
  *
  * @access public
  * @param void
  * @return boolean
  */
 function deleteHandins()
 {
     $q = DB::escapeField('rel_object_id') . ' = ' . DB::escape($this->getId()) . ' AND ' . DB::escapeField('rel_object_manager') . ' = ' . DB::escape(get_class($this->manager()));
     return ObjectHandins::delete($q);
 }