/**
 * 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);
}
 /**
  * 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
 }