function createWorkspace($ws_name, $parentWS_ids = null) { try { DB::beginWork(); $color = rand(0, 24); $project_data = array('name' => $ws_name, 'description' => '', 'show_description_in_overview' => false, 'color' => $color); $project = new Project(); $project->setFromAttributes($project_data); $project->save(); $permission_columns = ProjectUsers::getPermissionColumns(); $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 $project->clearUsers(); foreach ($auto_assign_users as $user) { $project_user = new ProjectUser(); $project_user->setProjectId($project->getId()); $project_user->setUserId($user->getId()); if (is_array($permission_columns)) { foreach ($permission_columns as $permission) { $project_user->setColumnValue($permission, true); } } // if $project_user->save(); } // foreach $this->setParents($project, $parentWS_ids); $id_parent = $project->getPID($project->getDepth() - 1); $proj_id = $project->getId(); ImportLogger::instance()->log("Workspace created: {$proj_id} {$ws_name} [{$id_parent}]"); print "Workspace created: {$proj_id} {$ws_name} [{$id_parent}]\r\n"; DB::commit(); } catch (Exception $e) { print "ERROR: {$e}\r\n"; DB::rollback(); } return $proj_id; }
/** * Return project permission for specific user if he is on project. In case of any error $default is returned * * @access public * @param Project $project * @param string $permission Permission name * @param boolean $default Default value * @return boolean */ function getProjectPermission(Project $project, $permission, $default = false) { static $valid_permissions = null; if (is_null($valid_permissions)) { $valid_permissions = ProjectUsers::getPermissionColumns(); } // if if (!in_array($permission, $valid_permissions)) { return $default; } // if $project_user = ProjectUsers::findById(array('project_id' => $project->getId(), 'user_id' => $this->getId())); // findById if (!$project_user instanceof ProjectUser) { return $default; } // if $getter = 'get' . Inflector::camelize($permission); return $project_user->{$getter}(); }
/** * Add project * * @param void * @return null */ function add() { $this->setTemplate('add_project'); $this->setLayout('administration'); if (!Project::canAdd(logged_user())) { flash_error(lang('no access permissions')); $this->redirectToReferer(get_url('dashboard')); } // if $project = new Project(); $project_data = array_var($_POST, 'project'); tpl_assign('project', $project); tpl_assign('project_data', $project_data); // Submited... if (is_array($project_data)) { $project->setFromAttributes($project_data); $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 try { DB::beginWork(); $project->save(); $permissions = ProjectUsers::getPermissionColumns(); $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 $user) { $project_user = new ProjectUser(); $project_user->setProjectId($project->getId()); $project_user->setUserId($user->getId()); if (is_array($permissions)) { foreach ($permissions as $permission) { $project_user->setColumnValue($permission, true); } } // if $project_user->save(); } // 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 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 }