/**
  * Process add project form
  *
  * @param void
  * @return null
  */
 function add()
 {
     $this->wireframe->print_button = false;
     if ($this->request->isApiCall() && !$this->request->isSubmitted()) {
         $this->httpError(HTTP_ERR_BAD_REQUEST);
     }
     // if
     if (!Project::canAdd($this->logged_user)) {
         $this->httpError(HTTP_ERR_FORBIDDEN);
     }
     // if
     $project_data = $this->request->post('project');
     if (!is_array($project_data)) {
         $project_data = array('leader_id' => $this->logged_user->getId(), 'default_visibility' => VISIBILITY_PRIVATE);
     }
     // if
     $this->smarty->assign('project_data', $project_data);
     if ($this->request->isSubmitted()) {
         db_begin_work();
         $this->active_project = new Project();
         // just in case
         $this->active_project->setAttributes($project_data);
         $this->active_project->setType(PROJECT_TYPE_NORMAL);
         $this->active_project->setStatus(PROJECT_STATUS_ACTIVE);
         $leader = null;
         if ($this->active_project->getLeaderId()) {
             $leader = Users::findById($this->active_project->getLeaderId());
             if (instance_of($leader, 'User')) {
                 $this->active_project->setLeader($leader);
             }
             // if
         }
         // if
         $project_template = null;
         $project_template_id = array_var($project_data, 'project_template_id');
         if ($project_template_id) {
             $project_template = Projects::findById($project_template_id);
         }
         // if
         $save = $this->active_project->save($project_template);
         if ($save && !is_error($save)) {
             // Add user who created a project and leader
             $this->active_project->addUser($this->logged_user, null, null);
             if (instance_of($leader, 'User')) {
                 $this->active_project->addUser($leader, null, null);
             }
             // if
             // Clone project template
             if (instance_of($project_template, 'Project')) {
                 $project_template->copyItems($this->active_project);
             } else {
                 // Auto assign users...
                 $users = Users::findAutoAssignUsers();
                 if (is_foreachable($users)) {
                     foreach ($users as $user) {
                         $this->active_project->addUser($user, $user->getAutoAssignRole(), $user->getAutoAssignPermissions());
                     }
                     // foreach
                 }
                 // if
                 // Create default categories
                 $category_definitions = array();
                 event_trigger('on_master_categories', array(&$category_definitions));
                 if (is_foreachable($category_definitions)) {
                     foreach ($category_definitions as $category_definition) {
                         $default_categories = $category_definition['value'];
                         if (!is_foreachable($default_categories)) {
                             $default_categories = array('General');
                         }
                         // if
                         foreach ($default_categories as $category_name) {
                             if (trim($category_name) != '') {
                                 $category = new Category();
                                 $category->log_activities = false;
                                 // don't log stuff in DB
                                 $category->setName($category_name);
                                 $category->setProjectId($this->active_project->getId());
                                 $category->setCreatedBy($this->logged_user);
                                 $category->setState(STATE_VISIBLE);
                                 $category->setVisibility(VISIBILITY_NORMAL);
                                 $category->setModule($category_definition['module']);
                                 $category->setController($category_definition['controller']);
                                 $category->save();
                             }
                             // if
                         }
                         // foreach
                     }
                     // foreach
                 }
                 // if
             }
             // if
             db_commit();
             if ($this->request->isApiCall()) {
                 $this->serveData($this->active_project, 'project');
             } else {
                 flash_success('Project ":name" has been created. Use this page to add more people to the project...', array('name' => $this->active_project->getName()));
                 $this->redirectToUrl($this->active_project->getPeopleUrl());
             }
             // if
         } else {
             db_rollback();
             if ($this->request->isApiCall()) {
                 $this->serveData($save);
             } else {
                 $this->smarty->assign('errors', $save);
             }
             // if
         }
         // if
     }
     // if
 }