/**
  * Show and process add page form
  *
  * @param void
  * @return null
  */
 function add()
 {
     $this->wireframe->print_button = false;
     if (!Page::canAdd($this->logged_user, $this->active_project)) {
         $this->httpError(HTTP_ERR_FORBIDDEN, null, true, $this->request->isApiCall());
     }
     // if
     $parent_id = (int) $this->request->get('parent_id');
     $page_data = $this->request->post('page');
     if (!is_array($page_data)) {
         $page_data = array('parent_id' => $this->request->get('parent_id'), 'milestone_id' => $this->request->get('milestone_id'), 'visibility' => $this->active_project->getDefaultVisibility());
     }
     // if
     $this->smarty->assign('page_data', $page_data);
     if ($this->request->isSubmitted()) {
         db_begin_work();
         $this->active_page = new Page();
         attach_from_files($this->active_page, $this->logged_user);
         $this->active_page->setAttributes($page_data);
         $this->active_page->setProjectId($this->active_project->getId());
         if (trim($this->active_page->getCreatedByName()) == '' || trim($this->active_page->getCreatedByEmail()) == '') {
             $this->active_page->setCreatedBy($this->logged_user);
         }
         // if
         $this->active_page->setUpdatedOn(DateTimeValue::now());
         $this->active_page->setUpdatedBy($this->logged_user);
         $this->active_page->setState(STATE_VISIBLE);
         $save = $this->active_page->save();
         if ($save && !is_error($save)) {
             $subscribers = array($this->logged_user->getId());
             if (is_foreachable($this->request->post('notify_users'))) {
                 $subscribers = array_merge($subscribers, $this->request->post('notify_users'));
             } else {
                 $subscribers[] = $this->active_project->getLeaderId();
             }
             // if
             if (!in_array($this->active_project->getLeaderId(), $subscribers)) {
                 $subscribers[] = $this->active_project->getLeaderId();
             }
             // if
             Subscriptions::subscribeUsers($subscribers, $this->active_page);
             db_commit();
             $this->active_page->ready();
             //BOF: mod
             $this->active_page->register_departments(!empty($page_data['departments']) ? $page_data['departments'] : array());
             //EOF: mod
             //BOF:mod 20110614
             $assignees_flag_data = $this->request->post('assignee');
             $this->active_page->register_assignees_flag($assignees_flag_data, true);
             //EOF:20110614
             if ($this->request->isApiCall()) {
                 $this->serveData($this->active_page, 'page');
             } else {
                 flash_success('Page ":name" has been created', array('name' => $this->active_page->getName()));
                 $this->redirectToUrl($this->active_page->getViewUrl());
             }
             // if
         } else {
             db_rollback();
             if ($this->request->isApiCall()) {
                 $this->serveData($save);
             } else {
                 $this->smarty->assign('errors', $save);
             }
             // if
         }
         // if
     }
     // if
 }