/**
  * Add project
  *
  * @param void
  * @return null
  */
 function add()
 {
     $this->setTemplate('add_project');
     $this->setLayout('administration');
     if (!logged_user()->canManageProjects()) {
         flash_error(lang('no access permissions'));
         $this->redirectToReferer(get_url('dashboard'));
     }
     // if
     $project = new Project();
     $project_data = array_var($_POST, 'project');
     $page_name = 'project_overview';
     $page_attachments = PageAttachments::getAttachmentsByPageNameAndProject($page_name, $project);
     $redirect_to = urldecode(array_var($_GET, 'redirect_to'));
     tpl_assign('project', $project);
     tpl_assign('project_data', $project_data);
     tpl_assign('page_attachments', $page_attachments);
     tpl_assign('redirect_to', $redirect_to);
     // Submitted...
     if (is_array($project_data)) {
         $project->setFromAttributes($project_data);
         $default_folders = array();
         if (plugin_active('files')) {
             $default_folders_config = str_replace(array("\r\n", "\r"), array("\n", "\n"), config_option('default_project_folders', ''));
             if (trim($default_folders_config) == '') {
                 $default_folders = array();
             } else {
                 $default_folders = explode("\n", $default_folders_config);
             }
             // if
         }
         // if
         $default_ticket_categories = array();
         if (plugin_active('tickets')) {
             $default_ticket_categories_config = str_replace(array("\r\n", "\r"), array("\n", "\n"), config_option('tickets_default_categories', ''));
             if (trim($default_ticket_categories_config) == '') {
                 $default_ticket_categories = array();
             } else {
                 $default_ticket_categories = explode("\n", $default_ticket_categories_config);
             }
             // if
         }
         // if
         try {
             DB::beginWork();
             $project->save();
             $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 $auto_assign_user) {
                 $project_user = new ProjectUser();
                 $project_user->setProjectId($project->getId());
                 $project_user->setUserId($auto_assign_user->getId());
                 $project_user->save();
                 if (is_array($permissions)) {
                     foreach ($permissions as $permission) {
                         $auto_assign_user->setProjectPermission($project, $permission, true);
                     }
                 }
                 // if
             }
             // foreach
             if (count($default_folders)) {
                 $added_folders = array();
                 foreach ($default_folders as $default_folder) {
                     $folder_name = trim($default_folder);
                     if ($folder_name == '') {
                         continue;
                     }
                     // if
                     if (in_array($folder_name, $added_folders)) {
                         continue;
                     }
                     // if
                     $folder = new ProjectFolder();
                     $folder->setProjectId($project->getId());
                     $folder->setName($folder_name);
                     $folder->save();
                     $added_folders[] = $folder_name;
                 }
                 // foreach
             }
             // if
             if (count($default_ticket_categories)) {
                 $added_categories = array();
                 foreach ($default_ticket_categories as $default_ticket_category) {
                     $category_name = trim($default_ticket_category);
                     if ($category_name == '') {
                         continue;
                     }
                     // if
                     if (in_array($category_name, $added_categories)) {
                         continue;
                     }
                     // if
                     $folder = new ProjectCategory();
                     $folder->setProjectId($project->getId());
                     $folder->setName($category_name);
                     $folder->save();
                     $added_categories[] = $category_name;
                 }
                 // foreach
             }
             // if
             $efqm_project = isset($project_data['efqm_project']) ? $project_data['efqm_project'] == '1' : false;
             if ($efqm_project) {
                 // insert 9 milestones with task lists
                 $efqm_template = array('efqm leadership' => array('a', 'b', 'c', 'd', 'e'), 'efqm strategy' => array('a', 'b', 'c', 'd'), 'efqm people' => array('a', 'b', 'c', 'd', 'e'), 'efqm partnership and resources' => array('a', 'b', 'c', 'd', 'e'), 'efqm processes products services' => array('a', 'b', 'c', 'd', 'e'), 'efqm customer results' => array('a', 'b'), 'efqm people results' => array('a', 'b'), 'efqm society results' => array('a', 'b'), 'efqm key results' => array('a', 'b'));
                 foreach ($efqm_template as $criteria => $subcriteria) {
                     $milestone = new ProjectMilestone();
                     $milestone->setProjectId($project->getId());
                     $milestone->setName(lang($criteria));
                     $milestone->setGoal(config_option('initial goal', 80));
                     $milestone->setDueDate(DateTimeValueLib::now());
                     $offset_in_days = config_option('due date offset', 90);
                     $milestone->getDueDate()->advance(60 * 60 * 24 * $offset_in_days);
                     $milestone->save();
                     foreach ($subcriteria as $subname) {
                         $task_list = new ProjectTaskList();
                         $task_list->setMilestoneId($milestone->getId());
                         $task_list->setProjectId($project->getId());
                         $task_list->setName(lang($criteria) . ' ' . $subname);
                         $task_list->setDueDate($milestone->getDueDate());
                         $task_list->setScore(config_option('initial score', 50));
                         $task_list->save();
                     }
                 }
             }
             ApplicationLogs::createLog($project, null, ApplicationLogs::ACTION_ADD, false, true);
             DB::commit();
             flash_success(lang('success add project', $project->getName()));
             $this->redirectToUrl($project->getPermissionsUrl());
         } catch (Exception $e) {
             tpl_assign('error', $e);
             DB::rollback();
         }
         // try
     }
     // if
 }