/**
 * Render select page control
 * 
 * Parameters:
 * 
 * - project - Parent project
 * - value - ID of selected page
 *
 * @param array $params
 * @param Smarty $smarty
 * @return string
 */
function smarty_function_select_page($params, &$smarty)
{
    $project = array_var($params, 'project', null, true);
    if (!instance_of($project, 'Project')) {
        return new InvalidParamError('project', $project, '$project is expected to be an instance of Project class', true);
    }
    // if
    $user = array_var($params, 'user');
    if (!instance_of($user, 'User')) {
        return new InvalidParamError('user', $user, '$user is exepcted to be an instance of User class', true);
    }
    // if
    $options = array();
    $value = array_var($params, 'value', null, true);
    $skip = array_var($params, 'skip');
    $categories = Categories::findByModuleSection($project, PAGES_MODULE, 'pages');
    if (is_foreachable($categories)) {
        foreach ($categories as $category) {
            $option_attributes = $category->getId() == $value ? array('selected' => true) : null;
            $options[] = option_tag($category->getName(), $category->getId(), $option_attributes);
            $pages = Pages::findByCategory($category, STATE_VISIBLE, $user->getVisibility());
            if (is_foreachable($pages)) {
                foreach ($pages as $page) {
                    smarty_function_select_page_populate_options($page, $value, $user, $skip, $options, '- ');
                }
                // foreach
            }
            // if
        }
        // foreach
    }
    // if
    return select_box($options, $params);
}
/**
 * Render select category control
 * 
 * Supported paramteres:
 * 
 * - all HTML attributes
 * - project - Parent project, required
 * - module - Module
 * - controller - Controller name
 * - value - ID of selected category
 * - optional - If false there will be no -- none -- option
 *
 * @param array $params
 * @param Smarty $smarty
 * @return string
 */
function smarty_function_select_category($params, &$smarty)
{
    static $ids = array();
    $project = array_var($params, 'project', null, true);
    if (!instance_of($project, 'Project')) {
        return new InvalidParamError('project', $project, 'Project parameter is required for select category helper and it needs to be an instance of Project class', true);
    }
    // if
    $user = array_var($params, 'user', null, true);
    $module = trim(array_var($params, 'module', null, true));
    if ($module == '') {
        return new InvalidParamError('module', $module, 'Module parameter is required for select category helper', true);
    }
    // if
    $controller = trim(array_var($params, 'controller', null, true));
    if ($controller == '') {
        return new InvalidParamError('controller', $controller, 'Controller parameter is required for select category helper', true);
    }
    // if
    $id = array_var($params, 'id', null, true);
    if (empty($id)) {
        $counter = 1;
        do {
            $id = "select_category_{$counter}";
            $counter++;
        } while (in_array($id, $ids));
    }
    // if
    $params['id'] = $id;
    $value = array_var($params, 'value', null, true);
    $optional = array_var($params, 'optional', true, true);
    $options = array();
    if ($optional) {
        $options[] = option_tag(lang('-- None --'), '');
    }
    // if
    $categories = Categories::findByModuleSection($project, $module, $controller);
    if (is_foreachable($categories)) {
        foreach ($categories as $category) {
            $option_attributes = array('class' => 'object_option');
            if ($category->getId() == $value) {
                $option_attributes['selected'] = true;
            }
            // if
            $options[] = option_tag($category->getName(), $category->getId(), $option_attributes);
        }
        // foreach
    }
    // if
    if (instance_of($user, 'User') && Category::canAdd($user, $project)) {
        $params['add_object_url'] = Category::getQuickAddUrl($project, $controller, $module);
        $params['object_name'] = 'category';
        $params['add_object_message'] = lang('Please insert new category name');
        $options[] = option_tag('', '');
        $options[] = option_tag(lang('New Category...'), '', array('class' => 'new_object_option'));
    }
    // if
    return select_box($options, $params) . '<script type="text/javascript">$("#' . $id . '").new_object_from_select();</script>';
}
 /**
  * List of tickets
  *
  */
 function index()
 {
     $this->addBreadcrumb(lang('List'));
     if ($this->active_category->isLoaded()) {
         $tickets = Milestones::groupByMilestone(Tickets::findOpenByCategory($this->active_category, STATE_VISIBLE, $this->logged_user->getVisibility()), STATE_VISIBLE, $this->logged_user->getVisibility());
     } else {
         $tickets = Milestones::groupByMilestone(Tickets::findOpenByProject($this->active_project, STATE_VISIBLE, $this->logged_user->getVisibility()), STATE_VISIBLE, $this->logged_user->getVisibility());
     }
     // if
     $this->smarty->assign(array('categories' => Categories::findByModuleSection($this->active_project, TICKETS_MODULE, 'tickets'), 'tickets' => $tickets, 'pagination_url' => assemble_url('mobile_access_view_tickets', array('project_id' => $this->active_project->getId())), 'page_back_url' => assemble_url('mobile_access_view_project', array('project_id' => $this->active_project->getId()))));
 }
 /**
  * List of files
  *
  */
 function index()
 {
     $this->addBreadcrumb(lang('List'));
     $per_page = 10;
     $page = $this->getPaginationPage();
     if (!$this->active_category->isNew()) {
         list($files, $pagination) = Files::paginateByCategory($this->active_category, $page, $per_page, STATE_VISIBLE, $this->logged_user->getVisibility());
     } else {
         list($files, $pagination) = Files::paginateByProject($this->active_project, $page, $per_page, STATE_VISIBLE, $this->logged_user->getVisibility());
     }
     // if
     $this->smarty->assign(array('files' => $files, 'pagination' => $pagination, 'categories' => Categories::findByModuleSection($this->active_project, 'files', 'files'), 'pagination_url' => assemble_url('mobile_access_view_files', array('project_id' => $this->active_project->getId())), 'page_back_url' => assemble_url('mobile_access_view_project', array('project_id' => $this->active_project->getId()))));
 }
 /**
  * Show categories in this section
  *
  * @param void
  * @return null
  */
 function categories()
 {
     $categories = Categories::findByModuleSection($this->active_project, $this->active_module, $this->getControllerName());
     if ($this->request->isApiCall()) {
         $this->serveData($categories, 'categories');
     } else {
         $this->setTemplate(array('module' => RESOURCES_MODULE, 'controller' => 'categories', 'template' => 'list'));
         $this->smarty->assign(array('categories' => $categories, 'can_add_category' => Category::canAdd($this->logged_user, $this->active_project)));
     }
     // if
 }
 /**
  * Export files
  *
  * @param void
  * @return null
  */
 function export()
 {
     $object_visibility = array_var($_GET, 'visibility', VISIBILITY_NORMAL);
     $exportable_modules = explode(',', array_var($_GET, 'modules', null));
     if (!is_foreachable($exportable_modules)) {
         $exportable_modules = null;
     }
     // if
     require_once PROJECT_EXPORTER_MODULE_PATH . '/models/ProjectExporterOutputBuilder.class.php';
     $output_builder = new ProjectExporterOutputBuilder($this->active_project, $this->smarty, $this->active_module, $exportable_modules);
     if (!$output_builder->createOutputFolder()) {
         $this->serveData($output_builder->execution_log, 'execution_log', null, FORMAT_JSON);
     }
     // if
     $output_builder->createAttachmentsFolder();
     $module_categories = Categories::findByModuleSection($this->active_project, $this->active_module, $this->active_module);
     $module_objects = Files::findByProject($this->active_project, STATE_VISIBLE, $object_visibility);
     $output_builder->setFileTemplate($this->active_module, $this->controller_name, 'index');
     $output_builder->smarty->assign('categories', $module_categories);
     $output_builder->smarty->assign('objects', $module_objects);
     $output_builder->outputToFile('index');
     // export files by categories
     if (is_foreachable($module_categories)) {
         foreach ($module_categories as $module_category) {
             if (instance_of($module_category, 'Category')) {
                 $objects = Files::find(array('conditions' => array('parent_id = ? AND project_id = ? AND type = ? AND state >= ? AND visibility >= ?', $module_category->getId(), $this->active_project->getId(), 'File', STATE_VISIBLE, $object_visibility), 'order' => 'boolean_field_1, datetime_field_1 DESC'));
                 $output_builder->smarty->assign(array('current_category' => $module_category, 'objects' => $objects));
                 $output_builder->outputToFile('category_' . $module_category->getId());
             }
             // if
         }
         // foreach
     }
     // if
     // export files
     if (is_foreachable($module_objects)) {
         $output_builder->setFileTemplate($this->active_module, $this->controller_name, 'object');
         foreach ($module_objects as $module_object) {
             if (instance_of($module_object, 'File')) {
                 $revisions = $module_object->getRevisions();
                 $output_builder->outputAttachments($revisions);
                 $comments = $module_object->getComments($object_visibility);
                 $output_builder->outputObjectsAttachments($comments);
                 $output_builder->smarty->assign('object', $module_object);
                 $output_builder->smarty->assign('comments', $comments);
                 $output_builder->outputToFile('file_' . $module_object->getId());
             }
             // if
         }
         // foreach
     }
     // if
     $this->serveData($output_builder->execution_log, 'execution_log', null, FORMAT_JSON);
 }
 function move()
 {
     $new_parent_id = $this->request->post('new_parent_id');
     $new_parent_type = $this->request->post('new_parent_type');
     $new_parent_url = '';
     $move_mode = false;
     if (!empty($new_parent_id) && !empty($new_parent_type)) {
         $move_mode = true;
         $parent_obj = $sql_part = null;
         switch ($new_parent_type) {
             case 'milestone':
                 $parent_obj = new MileStone($new_parent_id);
                 break;
             case 'ticket':
                 $parent_obj = new Ticket($new_parent_id);
                 break;
             case 'page':
                 $parent_obj = new Page($new_parent_id);
                 break;
         }
         if ($parent_obj) {
             $body = $this->active_task->getBody();
             $doc = new DOMDocument();
             if ($doc->loadHTML($body)) {
                 $anc_tags = $doc->getElementsByTagName('a');
                 $new_parent_url = '';
                 foreach ($anc_tags as $anc) {
                     if ($anc->nodeValue == 'View Task in Full') {
                         $href = $anc->getAttribute('href');
                         $fragment = substr($href, strpos($href, '#'));
                         $anc->setAttribute('href', $parent_obj->getViewUrl() . $fragment);
                         break;
                     }
                 }
                 if (!empty($fragment)) {
                     $body_tag = $doc->getElementsByTagName('body');
                     $body = $doc->saveXML($body_tag->item(0)->firstChild);
                     $comment_id = str_replace('#comment', '', $fragment);
                 }
             }
             $link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
             mysql_select_db(DB_NAME);
             $query = "update \n\t\t\t\t\t\t\thealingcrystals_project_objects \n\t\t\t\t\t\t  set \n\t\t\t\t\t\t\tproject_id='" . $parent_obj->getProjectId() . "', \n\t\t\t\t\t\t\tmilestone_id='" . $parent_obj->getMilestoneId() . "', \n\t\t\t\t\t\t\tparent_id='" . $parent_obj->getId() . "', \n\t\t\t\t\t\t\tparent_type='" . $parent_obj->getType() . "', \n\t\t\t\t\t\t\tbody = '" . mysql_real_escape_string($body) . "' \n\t\t\t\t\t\t  where\tid='" . $this->active_task->getId() . "'";
             mysql_query($query, $link);
             if (!empty($comment_id)) {
                 $comment_query = "update \n\t\t\t\t\t\t\thealingcrystals_project_objects \n\t\t\t\t\t\t  set \n\t\t\t\t\t\t\tproject_id='" . $parent_obj->getProjectId() . "', \n\t\t\t\t\t\t\tmilestone_id='" . $parent_obj->getMilestoneId() . "', \n\t\t\t\t\t\t\tparent_id='" . $parent_obj->getId() . "', \n\t\t\t\t\t\t\tparent_type='" . $parent_obj->getType() . "', \n\t\t\t\t\t\t\tposition=null\n\t\t\t\t\t\t  where\tid='" . $comment_id . "'";
                 mysql_query($comment_query, $link);
             }
             mysql_close($link);
             $new_parent_url = $parent_obj->getViewUrl() . '#task' . $this->active_task->getId();
             $cache_id = TABLE_PREFIX . 'project_objects_id_' . $this->active_task->getId();
             $cache_obj = cache_get($cache_id);
             if ($cache_obj) {
                 cache_remove($cache_id);
             }
         }
     } else {
         $listing = array();
         switch ($this->active_task_parent->getType()) {
             case 'Milestone':
                 //$listing = Milestones::findByProject($this->active_project, $this->logged_user);
                 $listing = Milestones::findActiveByProject_custom($this->active_project);
                 break;
             case 'Ticket':
                 $listing = Tickets::findOpenByProjectByNameSort($this->active_project, STATE_VISIBLE, $this->logged_user->getVisibility());
                 break;
             case 'Page':
                 $categories = Categories::findByModuleSection($this->active_project, 'pages', 'pages');
                 $listing = Pages::findByCategories($categories, STATE_VISIBLE, $this->logged_user->getVisibility());
                 /*foreach($categories as $category){
                 			$listing = array_merge($listing, Pages::findByCategory($category, STATE_VISIBLE, $this->logged_user->getVisibility()));
                 		}*/
                 break;
         }
         $this->smarty->assign(array('teams' => Projects::findNamesByUser($this->logged_user), 'listing' => $listing, 'task_parent_id' => $this->active_task_parent->getId()));
     }
     $this->smarty->assign('new_parent_url', $new_parent_url);
     $this->smarty->assign('move_mode', $move_mode);
 }
 /**
  * List of pages
  *
  */
 function index()
 {
     $per_page = 20;
     $page = (int) $this->request->get('page');
     if ($page < 1) {
         $page = 1;
     }
     // if
     $pagination = null;
     if ($this->active_category->isLoaded()) {
         $this->addBreadcrumb($this->active_category->getName());
         $pages = Pages::findByCategory($this->active_category, STATE_VISIBLE, $this->logged_user->getVisibility());
     } else {
         $this->addBreadcrumb(lang('Recently Modified'));
         list($pages, $pagination) = Pages::paginateByProject($this->active_project, $page, $per_page, STATE_VISIBLE, $this->logged_user->getVisibility());
     }
     // if
     $this->smarty->assign(array('pages' => $pages, 'pagination' => $pagination, 'categories' => Categories::findByModuleSection($this->active_project, PAGES_MODULE, 'pages'), 'pagination_url' => assemble_url('mobile_access_view_pages', array('project_id' => $this->active_project->getId())), 'page_back_url' => assemble_url('mobile_access_view_project', array('project_id' => $this->active_project->getId()))));
 }
 /**
  * Reorder pages
  * 
  * @param void
  * @return null
  */
 function reorder()
 {
     $this->skip_layout = $this->request->isAsyncCall();
     if (!$this->active_category->isLoaded()) {
         $this->httpError(HTTP_ERR_BAD_REQUEST);
     }
     // if
     $ordered_pages = $this->request->post('ordered_pages');
     $layout_pages = Pages::findByCategory($this->active_category, STATE_VISIBLE, $this->logged_user->getVisibility());
     js_assign('is_async_call', $this->request->isAsyncCall());
     $this->smarty->assign(array('pages' => $layout_pages, 'opened' => implode(',', $opened), 'reorder_pages_url' => assemble_url('project_pages_reorder', array('project_id' => $this->active_project->getId(), 'category_id' => $this->active_category->getId()))));
     if ($this->request->isSubmitted()) {
         if (is_foreachable($ordered_pages)) {
             $sorted_pages = array();
             $positions = array();
             foreach ($ordered_pages as $ordered_page_id => $ordered_page_parent_id) {
                 $ordered_page_parent_id = $ordered_page_parent_id ? $ordered_page_parent_id : $this->active_category->getId();
                 $position = array_var($positions, $ordered_page_parent_id, 1);
                 $sorted_pages[$ordered_page_id] = array('position' => $position, 'parent_id' => $ordered_page_parent_id);
                 $position++;
                 $positions[$ordered_page_parent_id] = $position;
             }
             // if
             $pages = Pages::findByIds(array_keys($ordered_pages), STATE_VISIBLE, $this->logged_user->getVisibility());
             if (is_foreachable($pages)) {
                 foreach ($pages as $page) {
                     if (isset($sorted_pages[$page->getId()])) {
                         $page->setPosition(array_var($sorted_pages[$page->getId()], 'position'));
                         $parent_id = array_var($sorted_pages[$page->getId()], 'parent_id');
                         if ($parent_id) {
                             $page->setParentId($parent_id);
                         }
                         // if
                         $page->save();
                     }
                     // if
                 }
                 // foreach
             }
             // if
         }
         // if
         if ($this->request->isAsyncCall()) {
             $per_page = 30;
             $page = (int) $this->request->get('page');
             if ($page < 1) {
                 $page = 1;
             }
             // if
             if ($this->active_category->isLoaded()) {
                 $this->setTemplate(array('module' => PAGES_MODULE, 'controller' => 'pages', 'template' => 'category'));
                 $this->smarty->assign(array('pages' => Pages::findByCategory($this->active_category, STATE_VISIBLE, $this->logged_user->getVisibility()), 'categories' => Categories::findByModuleSection($this->active_project, PAGES_MODULE, 'pages'), 'can_manage_categories' => $this->logged_user->isProjectLeader($this->active_project) || $this->logged_user->isProjectManager()));
             }
             // if
         }
         // if
     }
     // if submitted
 }
 function get_collection()
 {
     $parent_id = $this->request->post('parent_id');
     $team_obj = new Project($parent_id);
     $object_type = $this->request->post('object_type');
     $listing = array();
     switch ($object_type) {
         case 'milestone':
             //$listing = Milestones::findByProject($team_obj, $this->logged_user);
             $listing = Milestones::findActiveByProject_custom($team_obj);
             break;
         case 'ticket':
             $listing = Tickets::findOpenByProjectByNameSort($team_obj, STATE_VISIBLE, $this->logged_user->getVisibility());
             break;
         case 'page':
             $categories = Categories::findByModuleSection($team_obj, 'pages', 'pages');
             $listing = Pages::findByCategories($categories, STATE_VISIBLE, $this->logged_user->getVisibility());
             /*foreach($categories as $category){
             			$listing = array_merge($listing, Pages::findByCategory($category, STATE_VISIBLE, $this->logged_user->getVisibility()));
             		}*/
             break;
     }
     $this->smarty->assign('options', $listing);
 }
 /**
  * Export tickets
  *
  * @param void
  * @return null
  */
 function export()
 {
     $object_visibility = array_var($_GET, 'visibility', VISIBILITY_NORMAL);
     $exportable_modules = explode(',', array_var($_GET, 'modules', null));
     if (!is_foreachable($exportable_modules)) {
         $exportable_modules = null;
     }
     // if
     require_once PROJECT_EXPORTER_MODULE_PATH . '/models/ProjectExporterOutputBuilder.class.php';
     $output_builder = new ProjectExporterOutputBuilder($this->active_project, $this->smarty, $this->active_module, $exportable_modules);
     if (!$output_builder->createOutputFolder()) {
         $this->serveData($output_builder->execution_log, 'execution_log', null, FORMAT_JSON);
     }
     // if
     $output_builder->createAttachmentsFolder();
     $module_categories = Categories::findByModuleSection($this->active_project, $this->active_module, $this->active_module);
     $module_objects = Tickets::findByProject($this->active_project, null, STATE_VISIBLE, $object_visibility);
     $output_builder->setFileTemplate($this->active_module, $this->controller_name, 'index');
     $output_builder->smarty->assign('categories', $module_categories);
     $output_builder->smarty->assign('objects', $module_objects);
     $output_builder->outputToFile('index');
     // export tickets by categories
     if (is_foreachable($module_categories)) {
         foreach ($module_categories as $module_category) {
             if (instance_of($module_category, 'Category')) {
                 $output_builder->smarty->assign(array('current_category' => $module_category, 'objects' => Tickets::findByProject($this->active_project, $module_category, STATE_VISIBLE, $object_visibility)));
                 $output_builder->outputToFile('category_' . $module_category->getId());
             }
             // if
         }
         // foreach
     }
     // if
     // export tickets
     if (is_foreachable($module_objects)) {
         $output_builder->setFileTemplate($this->active_module, $this->controller_name, 'object');
         foreach ($module_objects as $module_object) {
             if (instance_of($module_object, 'Ticket')) {
                 $output_builder->outputAttachments($module_object->getAttachments());
                 $comments = $module_object->getComments($object_visibility);
                 $output_builder->outputObjectsAttachments($comments);
                 if (module_loaded('timetracking')) {
                     $timerecords = TimeRecords::findByParent($module_object, null, STATE_VISIBLE, $object_visibility);
                     $total_time = TimeRecords::calculateTime($timerecords);
                 } else {
                     $timerecords = null;
                     $total_time = 0;
                 }
                 // if
                 $output_builder->smarty->assign(array('timerecords' => $timerecords, 'total_time' => $total_time, 'object' => $module_object, 'comments' => $comments));
                 $output_builder->outputToFile('ticket_' . $module_object->getId());
             }
             // if
         }
         // foreach
     }
     // if
     $this->serveData($output_builder->execution_log, 'execution_log', null, FORMAT_JSON);
 }