/**
  * Copy task list then redirect to edit
  *
  * @access public
  * @param void
  * @return null
  */
 function copy_list()
 {
     $task_list = ProjectTaskLists::findById(get_id());
     if (!$task_list instanceof ProjectTaskList) {
         flash_error(lang('task list dnx'));
         $this->redirectTo('task', 'index');
     }
     // if
     if (!$task_list->canAdd(logged_user(), active_project())) {
         flash_error(lang('no access permissions'));
         $this->redirectTo('task', 'index');
     }
     // if
     try {
         DB::beginWork();
         $source_task_list = $task_list;
         $task_list = new ProjectTaskList();
         $task_list->setName($source_task_list->getName() . ' (' . lang('copy') . ')');
         $task_list->setPriority($source_task_list->getPriority());
         $task_list->setDescription($source_task_list->getDescription());
         $task_list->setMilestoneId($source_task_list->getMilestoneId());
         $task_list->setDueDate($source_task_list->getDueDate());
         $task_list->setIsPrivate($source_task_list->getIsPrivate());
         $task_list->setOrder($source_task_list->getOrder());
         $task_list->setProjectId($source_task_list->getProjectId());
         $task_list->save();
         $task_count = 0;
         $source_tasks = $source_task_list->getTasks();
         if (is_array($source_tasks)) {
             foreach ($source_tasks as $source_task) {
                 $task = new ProjectTask();
                 $task->setText($source_task->getText());
                 $task->setAssignedToUserId($source_task->getAssignedToUserId());
                 $task->setAssignedToCompanyId($source_task->getAssignedToCompanyId());
                 $task->setOrder($source_task->getOrder());
                 $task->setDueDate($source_task->getDueDate());
                 $task_list->attachTask($task);
                 $task_count++;
             }
         }
         ApplicationLogs::createLog($task_list, active_project(), ApplicationLogs::ACTION_ADD);
         DB::commit();
         flash_success(lang('success copy task list', $source_task_list->getName(), $task_list->getName(), $task_count));
         //$this->redirectToUrl($task_list->getEditUrl());
         $this->redirectTo('task', 'index');
     } catch (Exception $e) {
         DB::rollback();
         tpl_assign('error', $e);
     }
     // try
 }
 /**
  * Copy project
  *
  * @param void
  * @return null
  */
 function copy()
 {
     trace(__FILE__, "copy():begin");
     if (!Project::canAdd(logged_user())) {
         flash_error(lang('no access permissions'));
         $this->redirectToReferer(get_url('dashboard'));
     }
     // if
     $this->setTemplate('copy_project');
     $this->setLayout('administration');
     $project_data = array_var($_POST, 'project');
     tpl_assign('project_data', $project_data);
     // Submitted...
     if (is_array($project_data)) {
         $source = Projects::findById($project_data['source']);
         if (!$source instanceof Project) {
             flash_error(lang('project dnx'));
             $this->redirectTo('administration', 'projects');
         }
         // if
         try {
             $shift_dates = isset($project_data['shift_dates']) ? $project_data['shift_dates'] == 'checked' : false;
             $copy_details = isset($project_data['copy_details']) ? $project_data['copy_details'] == 'checked' : false;
             $copy_tasks = isset($project_data['copy_tasks']) ? $project_data['copy_tasks'] == 'checked' : false;
             $copy_milestones = isset($project_data['copy_milestones']) ? $project_data['copy_milestones'] == 'checked' : false;
             $copy_messages = isset($project_data['copy_messages']) ? $project_data['copy_messages'] == 'checked' : false;
             $copy_links = isset($project_data['copy_links']) ? $project_data['copy_links'] == 'checked' : false;
             $copy_files = isset($project_data['copy_files']) ? $project_data['copy_files'] == 'checked' : false;
             $copy_users = isset($project_data['copy_users']) ? $project_data['copy_users'] == 'checked' : false;
             $copy_pages = isset($project_data['copy_pages']) ? $project_data['copy_pages'] == 'checked' : false;
             DB::beginWork();
             $project = new Project();
             $new_name = lang('projects copy new name', $source->getName());
             $new_name .= date(' z H:i:s');
             $project->setName($new_name);
             if ($copy_details) {
                 $project->setDescription($source->getDescription());
                 $project->setPriority($source->getPriority());
                 $project->setShowDescriptionInOverview($source->getShowDescriptionInOverview());
             }
             $project->save();
             $project_id = $project->getId();
             $add_seconds = 0;
             if (isset($project_data['add_days'])) {
                 $add_days = 0 + trim($project_data['add_days']);
                 $add_seconds = $add_days * 24 * 60 * 60;
             }
             $source_created_on = $source->getCreatedOn();
             //var_dump($source_created_on);
             $milestone_map = array(0 => 0);
             // project milestones
             if ($copy_milestones) {
                 $source_milestones = $source->getAllMilestones();
                 if (is_array($source_milestones)) {
                     foreach ($source_milestones as $source_milestone) {
                         $milestone = new ProjectMilestone();
                         //$milestone->copy($source_milestone);
                         $milestone->setName($source_milestone->getName());
                         $milestone->setDescription($source_milestone->getDescription());
                         if ($shift_dates) {
                             trace(__FILE__, "copy():shift dates");
                             $milestone->setDueDate(DateTimeValueLib::now());
                             $seconds = $source_milestone->getDueDate()->difference($source_created_on);
                             $milestone->getDueDate()->advance($seconds);
                         } else {
                             $milestone->setDueDate($source_milestone->getDueDate());
                         }
                         $milestone->getDueDate()->advance($add_seconds);
                         $milestone->setIsPrivate($source_milestone->getIsPrivate());
                         $milestone->setAssignedToUserId($source_milestone->getAssignedToUserId());
                         $milestone->setAssignedToCompanyId($source_milestone->getAssignedToCompanyId());
                         $milestone->setProjectId($project_id);
                         $milestone->save();
                         $milestone_map[$source_milestone->getId()] = $milestone->getId();
                     }
                     // foreach
                 }
                 // if
             }
             // if
             // project tasks
             if ($copy_tasks) {
                 $source_task_lists = $source->getAllTaskLists();
                 if (is_array($source_task_lists)) {
                     foreach ($source_task_lists as $source_task_list) {
                         $task_list = new ProjectTaskList();
                         //$task_list->copy($source_milestone);
                         $task_list->setName($source_task_list->getName());
                         $task_list->setPriority($source_task_list->getPriority());
                         $task_list->setDescription($source_task_list->getDescription());
                         if ($copy_milestones) {
                             $task_list->setMilestoneId($milestone_map[$source_task_list->getMilestoneId()]);
                         }
                         $task_list->setDueDate($source_task_list->getDueDate());
                         if ($task_list->getDueDate() instanceof DateTimeValue) {
                             if ($shift_dates) {
                                 trace(__FILE__, "copy():task list shift dates");
                                 $task_list->setDueDate(DateTimeValueLib::now());
                                 $seconds = $source_task_list->getDueDate()->difference($source_created_on);
                                 $task_list->getDueDate()->advance($seconds);
                             }
                             $task_list->getDueDate()->advance($add_seconds);
                         }
                         $task_list->setIsPrivate($source_task_list->getIsPrivate());
                         $task_list->setOrder($source_task_list->getOrder());
                         $task_list->setProjectId($project_id);
                         $task_list->save();
                         $source_tasks = $source_task_list->getTasks();
                         if (is_array($source_tasks)) {
                             foreach ($source_tasks as $source_task) {
                                 $task = new ProjectTask();
                                 $task->setOrder($source_task->getOrder());
                                 $task->setDueDate($source_task->getDueDate());
                                 if ($task->getDueDate() instanceof DateTimeValue) {
                                     if ($shift_dates) {
                                         trace(__FILE__, "copy():task shift dates");
                                         $task->setDueDate(DateTimeValueLib::now());
                                         $seconds = $source_task->getDueDate()->difference($source_created_on);
                                         $task->getDueDate()->advance($seconds);
                                     }
                                     $task->getDueDate()->advance($add_seconds);
                                 }
                                 $task->setText($source_task->getText());
                                 $task->getAssignedToUserId($source_task->getAssignedToUserId());
                                 $task->getAssignedToCompanyId($source_task->getAssignedToCompanyId());
                                 $task_list->attachTask($task);
                             }
                         }
                     }
                     // foreach
                 }
                 // if
             }
             // if
             // project messages
             if ($copy_messages) {
                 $source_messages = $source->getAllMessages();
                 if (is_array($source_messages)) {
                     foreach ($source_messages as $source_message) {
                         $message = new ProjectMessage();
                         //$message->copy($source_message);
                         $message->setTitle($source_message->getTitle());
                         $message->setText($source_message->getText());
                         $message->setAdditionalText($source_message->getAdditionalText());
                         if ($copy_milestones) {
                             $message->setMilestoneId($milestone_map[$source_message->getMilestoneId()]);
                         }
                         $message->setIsImportant($source_message->getIsImportant());
                         $message->setIsPrivate($source_message->getIsPrivate());
                         $message->setCommentsEnabled($source_message->getCommentsEnabled());
                         $message->setAnonymousCommentsEnabled($source_message->getAnonymousCommentsEnabled());
                         $message->setProjectId($project_id);
                         $message->save();
                     }
                     // foreach
                 }
                 // if
             }
             // if
             // project links
             if ($copy_links) {
                 $source_links = ProjectLinks::getAllProjectLinks($source);
                 if (is_array($source_links)) {
                     foreach ($source_links as $source_link) {
                         $link = new ProjectLink();
                         //$folder->copy($source_link);
                         $link->setTitle($source_link->getTitle());
                         $link->setUrl($source_link->getUrl());
                         $link->setProjectId($project_id);
                         $link->save();
                     }
                     // foreach
                 }
                 // if
             }
             // if
             // project folders & files
             if ($copy_files) {
                 $folder_map = array(0 => 0);
                 $source_folders = $source->getFolders();
                 if (is_array($source_folders)) {
                     foreach ($source_folders as $source_folder) {
                         $folder = new ProjectFolder();
                         //$folder->copy($source_folder);
                         $folder->setName($source_folder->getName());
                         $folder->setProjectId($project_id);
                         $folder->save();
                         $folder_map[$source_folder->getId()] = $folder->getId();
                     }
                     // foreach
                 }
                 // if
                 $source_files = ProjectFiles::getAllFilesByProject($source);
                 if (is_array($source_files)) {
                     foreach ($source_files as $source_file) {
                         $file = new ProjectFile();
                         $file->setProjectId($project_id);
                         $file->setFolderId($folder_map[$source_file->getFolderId()]);
                         $file->setFileName($source_file->getFileName());
                         $file->setDescription($source_file->getDescription());
                         $file->setIsPrivate($source_file->getIsPrivate());
                         $file->setIsImportant($source_file->getIsImportant());
                         $file->setIsLocked($source_file->getIsLocked());
                         $file->setIsVisible($source_file->getIsVisible());
                         $file->setExpirationTime($source_file->getExpirationTime());
                         $file->setCommentsEnabled($source_file->getCommentsEnabled());
                         $file->setAnonymousCommentsEnabled($source_file->getAnonymousCommentsEnabled());
                         $file->save();
                         $source_revision = $source_file->getLastRevision();
                         if ($source_revision instanceof ProjectFileRevision) {
                             $revision = new ProjectFileRevision();
                             $revision->setFileId($file->getId());
                             $revision->setRevisionNumber($source_revision->getRevisionNumber());
                             $revision->setRepositoryId($source_revision->getRepositoryId());
                             $revision->setFilesize($source_revision->getFilesize());
                             $revision->setFilename($source_revision->getFileName());
                             $revision->setTypeString($source_revision->getTypeString());
                             $revision->setThumbFilename($source_revision->getThumbFilename());
                             $revision->setFileTypeId($source_revision->getFileTypeId());
                             $revision->setComment($source_revision->getComment());
                             $revision->save();
                         }
                     }
                     // foreach
                 }
                 // if
             }
             // if
             if ($copy_pages) {
                 $source_pages = Wiki::getAllProjectPages($source);
                 if (is_array($source_pages)) {
                     foreach ($source_pages as $source_page) {
                         $page = new WikiPage();
                         $page->setProjectId($project_id);
                         $page->setProjectIndex($source_page->getProjectIndex());
                         $page->setProjectSidebar($source_page->getProjectSidebar());
                         if (plugin_active('tags')) {
                             //$page->setTags($source_page->getTagNames());
                         }
                         //Make a new revision of this page
                         $revision = $page->makeRevision();
                         $source_revision = $source_page->getLatestRevision();
                         //Set attributes
                         $revision->setName($source_revision->getName());
                         $revision->setContent($source_revision->getContent());
                         $revision->setLogMessage($source_revision->getLogMessage());
                         //Save the page
                         $page->save();
                     }
                     // foreach
                 }
                 // if
             }
             // if
             if ($copy_users) {
                 $source_companies = ProjectCompanies::instance()->getCompaniesByProject($source);
                 if (is_array($source_companies)) {
                     foreach ($source_companies as $source_company) {
                         $project_company = new ProjectCompany();
                         $project_company->setCompanyId($source_company->getId());
                         $project_company->setProjectId($project_id);
                         $project_company->save();
                     }
                     // foreach
                 }
                 $source_users = ProjectUsers::instance()->getUsersByProject($source);
                 if (is_array($source_users)) {
                     foreach ($source_users as $source_user) {
                         $project_user = new ProjectUser();
                         $project_user->setUserId($source_user->getId());
                         $project_user->setProjectId($project_id);
                         $project_user->save();
                     }
                     // foreach
                 }
             }
             /*
                       $permissions = array_keys(PermissionManager::getPermissionsText());
                       $auto_assign_users = owner_company()->getAutoAssignUsers();
                       
                       // We are getting the list of auto assign users. If current user is not in the list
                       // add it. He's creating the project after all...
                       if (is_array($auto_assign_users)) {
                         $auto_assign_logged_user = false;
                         foreach ($auto_assign_users as $user) {
                           if ($user->getId() == logged_user()->getId()) {
                             $auto_assign_logged_user = true;
                           }
                         } // if
                         if (!$auto_assign_logged_user) {
                           $auto_assign_users[] = logged_user();
                         }
                       } else {
                         $auto_assign_users[] = logged_user();
                       } // if
                       
                       foreach ($auto_assign_users as $user) {
                         $project_user = new ProjectUser();
                         $project_user->setProjectId($project->getId());
                         $project_user->setUserId($user->getId());
                         if (is_array($permissions)) {
                           foreach ($permissions as $permission) {
                             $user = Users::findById($project_user->getUserId());
                             $user->setProjectPermission($project,$permission,true);
                           }
                         } // if
                         $project_user->save();
                       } // foreach
             */
             ApplicationLogs::createLog($project, null, ApplicationLogs::ACTION_ADD, false, true);
             DB::commit();
             flash_success(lang('success copy project', $source->getName(), $project->getName()));
             $this->redirectToUrl($project->getPermissionsUrl());
         } catch (Exception $e) {
             echo $e->getMessage();
             tpl_assign('error', $e);
             DB::rollback();
         }
         // try
     }
     // if (submitted)
 }