/**
  * Return all projects that this company is member of
  *
  * @access public
  * @param Company $company
  * @param string $additional_conditions Additional SQL conditions
  * @return array
  */
 static function getProjectsByCompany(Company $company, $additional_conditions = null)
 {
     if ($company->isOwner()) {
         return Projects::getAll();
     }
     $projects_table = Projects::instance()->getTableName(true);
     $project_companies_table = ProjectCompanies::instance()->getTableName(true);
     $projects = array();
     $sql = "SELECT {$projects_table}.* FROM {$projects_table}, {$project_companies_table} WHERE ({$projects_table}.`id` = {$project_companies_table}.`project_id` AND {$project_companies_table}.`company_id` = " . DB::escape($company->getId()) . ')';
     if (trim($additional_conditions) != '') {
         $sql .= " AND ({$additional_conditions})";
     }
     $rows = DB::executeAll($sql);
     if (is_array($rows)) {
         foreach ($rows as $row) {
             $projects[] = Projects::instance()->loadFromRow($row);
         }
         // foreach
     }
     // if
     return count($projects) ? $projects : null;
 }
Exemplo n.º 2
0
/**
 * Renders select project box
 *
 * @param string $name
 * @param Project $project
 * @param integer $selected ID of selected milestone
 * @param array $attributes Array of additional attributes
 * @return string
 * @throws InvalidInstanceError
 */
function select_project($name, $projectname = null, $selected = null, $attributes = null)
{
    if (is_array($attributes)) {
        if (!isset($attributes['class'])) {
            $attributes['class'] = 'select_project';
        }
    } else {
        $attributes = array('class' => 'select_project');
    }
    // if
    $options = array(option_tag(lang('none'), 0));
    if (is_null($projectname)) {
        $projects = Projects::getAll();
    } else {
        $projects = Projects::getAll();
    }
    if (is_array($projects)) {
        foreach ($projects as $project) {
            $option_attributes = $project->getId() == $selected ? array('selected' => 'selected') : null;
            $options[] = option_tag($project->getName(), $project->getId(), $option_attributes);
        }
        // foreach
    }
    // if
    return select_box($name, $options, $attributes);
}
Exemplo n.º 3
0
 /**
  * Update company permissions
  *
  * @param void
  * @return null
  */
 function update_permissions()
 {
     if (!logged_user()->isAdministrator(owner_company())) {
         flash_error(lang('no access permissions'));
         $this->redirectTo('dashboard');
     }
     // if
     $company = Companies::findById(get_id());
     if (!$company instanceof Company) {
         flash_error(lang('company dnx'));
         $this->redirectToReferer(get_url('administration'));
     }
     // if
     if ($company->isOwner()) {
         flash_error(lang('error owner company has all permissions'));
         $this->redirectToReferer(get_url('administration'));
     }
     // if
     $projects = Projects::getAll(Projects::ORDER_BY_NAME);
     if (!is_array($projects) || !count($projects)) {
         flash_error(lang('no projects in db'));
         $this->redirectToUrl($company->getViewUrl());
     }
     // if
     tpl_assign('projects', $projects);
     tpl_assign('company', $company);
     if (array_var($_POST, 'submitted') == 'submitted') {
         $counter = 0;
         $logged_user = logged_user();
         // reuse...
         foreach ($projects as $project) {
             if (!$logged_user->isProjectUser($project)) {
                 continue;
             }
             // if
             $new_value = array_var($_POST, 'project_' . $project->getId()) == 'checked';
             $relation = ProjectCompanies::findById(array('project_id' => $project->getId(), 'company_id' => $company->getId()));
             // findById
             $current_value = $relation instanceof ProjectCompany;
             try {
                 if ($current_value != $new_value) {
                     if ($new_value) {
                         $relation = new ProjectCompany();
                         $relation->setProjectId($project->getId());
                         $relation->setCompanyId($company->getId());
                         $relation->save();
                     } else {
                         $relation->delete();
                     }
                     // if
                     $counter++;
                 }
                 // if
             } catch (Exception $e) {
                 die($e->__toString());
             }
             // if
         }
         // foreach
         flash_success(lang('success update company permissions', $counter));
         $this->redirectToUrl($company->getViewUrl());
     }
     // if
 }
Exemplo n.º 4
0
 /**
  * Return all projects that this company is member of
  *
  * @access public
  * @param void
  * @return array
  */
 function getProjects()
 {
     return $this->isOwner() ? Projects::getAll() : ProjectCompanies::getProjectsByCompany($this);
 }
 /**
  * Edit group
  *
  * @param void
  * @return null
  */
 function edit_group()
 {
     $this->setTemplate('add_group');
     if (!can_manage_security(logged_user())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     // if
     $group = Groups::findById(get_id());
     if (!$group instanceof Group) {
         flash_error(lang('group dnx'));
         $this->redirectTo('administration', 'groups');
     }
     // if
     if (logged_user()->isAdministrator()) {
         $projects = Projects::getAll();
     } else {
         $projects = null;
     }
     $permissions = ProjectUsers::getNameTextArray();
     $group_data = array_var($_POST, 'group');
     if (!is_array($group_data)) {
         $group_data = array('name' => $group->getName(), 'can_edit_company_data' => $group->getCanEditCompanyData(), 'can_manage_security' => $group->getCanManageSecurity(), 'can_manage_workspaces' => $group->getCanManageWorkspaces(), 'can_manage_configuration' => $group->getCanManageConfiguration(), 'can_manage_contacts' => $group->getCanManageContacts(), 'can_manage_templates' => $group->getCanManageTemplates(), 'can_manage_reports' => $group->getCanManageReports(), 'can_manage_time' => $group->getCanManageTime(), 'can_add_mail_accounts' => $group->getCanAddMailAccounts());
         // array
     }
     // if
     $users = GroupUsers::getUsersByGroup($group->getId());
     if ($users) {
         foreach ($users as $usr) {
             $group_data['user[' . $usr->getId() . ']'] = true;
         }
     }
     tpl_assign('group', $group);
     tpl_assign('group_data', $group_data);
     tpl_assign('permissions', $permissions);
     tpl_assign('projects', $projects);
     if (is_array(array_var($_POST, 'group'))) {
         $group->setFromAttributes($group_data);
         if (array_var($group_data, "can_edit_company_data") != 'checked') {
             $group->setCanEditCompanyData(false);
         }
         if (array_var($group_data, "can_manage_security") != 'checked') {
             $group->setCanManageSecurity(false);
         }
         if (array_var($group_data, "can_manage_configuration") != 'checked') {
             $group->setCanManageConfiguration(false);
         }
         if (array_var($group_data, "can_manage_workspaces") != 'checked') {
             $group->setCanManageWorkspaces(false);
         }
         if (array_var($group_data, "can_manage_contacts") != 'checked') {
             $group->setCanManageContacts(false);
         }
         if (array_var($group_data, "can_manage_templates") != 'checked') {
             $group->setCanManageTemplates(false);
         }
         if (array_var($group_data, "can_manage_reports") != 'checked') {
             $group->setCanManageReports(false);
         }
         if (array_var($group_data, "can_manage_time") != 'checked') {
             $group->setCanManageTime(false);
         }
         if (array_var($group_data, "can_add_mail_accounts") != 'checked') {
             $group->setCanAddMailAccounts(false);
         }
         try {
             DB::beginWork();
             //set permissions
             $permissionsString = array_var($_POST, 'permissions');
             if ($permissionsString && $permissionsString != '') {
                 $permissions = json_decode($permissionsString);
             }
             if (is_array($permissions) && count($permissions) > 0) {
                 //Clear old modified permissions
                 $ids = array();
                 foreach ($permissions as $perm) {
                     $ids[] = $perm->wsid;
                 }
                 ProjectUsers::clearByUser($group, implode(',', $ids));
                 //Add new permissions
                 //TODO - Make batch update of these permissions
                 foreach ($permissions as $perm) {
                     if (ProjectUser::hasAnyPermissions($perm->pr, $perm->pc)) {
                         $relation = new ProjectUser();
                         $relation->setProjectId($perm->wsid);
                         $relation->setUserId($group->getId());
                         $relation->setCheckboxPermissions($perm->pc);
                         $relation->setRadioPermissions($perm->pr);
                         $relation->save();
                     }
                     //endif
                     //else if the user has no permissions at all, he is not a project_user. ProjectUser is not created
                 }
                 //end foreach
             }
             // if
             $group->save();
             GroupUsers::clearByGroup($group);
             if (array_var($_POST, 'user')) {
                 foreach (array_var($_POST, 'user') as $user_id => $val) {
                     if ($val == 'checked' && is_numeric($user_id) && Users::findById($user_id) instanceof User) {
                         $gu = new GroupUser();
                         $gu->setGroupId($group->getId());
                         $gu->setUserId($user_id);
                         $gu->save();
                     }
                 }
             }
             ApplicationLogs::createLog($group, null, ApplicationLogs::ACTION_EDIT);
             DB::commit();
             flash_success(lang('success edit group', $group->getName()));
             ajx_current("back");
         } catch (Exception $e) {
             DB::rollback();
             tpl_assign('error', $e);
         }
         // try
     }
     // if
 }
 /**
  * Update company permissions
  *
  * @param void
  * @return null
  */
 function update_permissions()
 {
     if (!logged_user()->isAdministrator(owner_company())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     // if
     $company = Companies::findById(get_id());
     if (!$company instanceof Company) {
         flash_error(lang('company dnx'));
         ajx_current("empty");
         return;
     }
     // if
     if ($company->isOwner()) {
         flash_error(lang('error owner company has all permissions'));
         ajx_current("empty");
         return;
     }
     // if
     $projects = Projects::getAll(Projects::ORDER_BY_NAME);
     if (!is_array($projects) || !count($projects)) {
         flash_error(lang('no projects in db'));
         ajx_current("empty");
         return;
     }
     // if
     tpl_assign('projects', $projects);
     tpl_assign('company', $company);
     if (array_var($_POST, 'submitted') == 'submitted') {
         $counter = 0;
         $logged_user = logged_user();
         // reuse...
         ProjectCompanies::delete('company_id = ' . $company->getId());
         $wsids = array_var($_POST, 'ws_ids', '');
         $selected = Projects::findByCSVIds($wsids);
         $counter = 0;
         foreach ($selected as $ws) {
             $pc = new ProjectCompany();
             $pc->setCompanyId($company->getId());
             $pc->setProjectId($ws->getId());
             $pc->save();
             $counter++;
         }
         flash_success(lang('success update company permissions', $counter));
         ajx_current("back");
     }
     // if
 }
 /**
  * Show update permissions page
  *
  * @param void
  * @return null
  */
 function update_permissions()
 {
     $user = Users::findById(get_id());
     if (!$user instanceof User) {
         flash_error(lang('user dnx'));
         ajx_current("empty");
         return;
     }
     // if
     if (!$user->canUpdatePermissions(logged_user())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     // if
     $company = $user->getCompany();
     if (!$company instanceof Company) {
         flash_error(lang('company dnx'));
         ajx_current("empty");
         return;
     }
     // if
     if (logged_user()->isAdministrator()) {
         $projects = Projects::getAll();
     } else {
         $projects = null;
     }
     $permissions = ProjectUsers::getNameTextArray();
     $redirect_to = array_var($_GET, 'redirect_to');
     if (trim($redirect_to) == '' || !is_valid_url($redirect_to)) {
         $redirect_to = $user->getCardUrl();
     }
     // if
     $user_data = array_var($_POST, 'user');
     if (!is_array($user_data)) {
         $user_data = array('can_edit_company_data' => $user->getCanEditCompanyData(), 'can_manage_security' => $user->getCanManageSecurity(), 'can_manage_workspaces' => $user->getCanManageWorkspaces(), 'can_manage_configuration' => $user->getCanManageConfiguration(), 'can_manage_contacts' => $user->getCanManageContacts(), 'can_manage_templates' => $user->getCanManageTemplates(), 'can_manage_reports' => $user->getCanManageReports(), 'can_manage_time' => $user->getCanManageTime(), 'can_add_mail_accounts' => $user->getCanAddMailAccounts());
         // array
         Hook::fire('add_user_permissions', $user, $user_data);
     }
     // if
     tpl_assign('user_data', $user_data);
     tpl_assign('user', $user);
     tpl_assign('company', $company);
     tpl_assign('projects', $projects);
     tpl_assign('permissions', $permissions);
     tpl_assign('redirect_to', $redirect_to);
     if (array_var($_POST, 'submitted') == 'submitted') {
         $user_data = array_var($_POST, 'user');
         if (!is_array($user_data)) {
             $user_data = array();
         }
         try {
             DB::beginWork();
             $permissionsString = array_var($_POST, 'permissions');
             if ($permissionsString && $permissionsString != '') {
                 $permissions = json_decode($permissionsString);
             }
             if (is_array($permissions) && count($permissions) > 0) {
                 //Clear old modified permissions
                 $ids = array();
                 foreach ($permissions as $perm) {
                     $ids[] = $perm->wsid;
                 }
                 ProjectUsers::clearByUser($user, implode(',', $ids));
                 //Add new permissions
                 //TODO - Make batch update of these permissions
                 foreach ($permissions as $perm) {
                     if (ProjectUser::hasAnyPermissions($perm->pr, $perm->pc)) {
                         $relation = new ProjectUser();
                         $relation->setProjectId($perm->wsid);
                         $relation->setUserId($user->getId());
                         $relation->setCheckboxPermissions($perm->pc, $user->isGuest() ? false : true);
                         $relation->setRadioPermissions($perm->pr, $user->isGuest() ? false : true);
                         $relation->save();
                     }
                     //endif
                     //else if the user has no permissions at all, he is not a project_user. ProjectUser is not created
                 }
                 //end foreach
             }
             // if
             $user->setCanEditCompanyData(false);
             $user->setCanManageSecurity(false);
             $user->setCanManageConfiguration(false);
             $user->setCanManageWorkspaces(false);
             $user->setCanManageContacts(false);
             $user->setCanManageTemplates(false);
             $user->setCanManageReports(false);
             $user->setCanManageTime(false);
             $user->setCanAddMailAccounts(false);
             $other_permissions = array();
             Hook::fire('add_user_permissions', $user, $other_permissions);
             foreach ($other_permissions as $k => $v) {
                 $user->setColumnValue($k, false);
             }
             $user->setFromAttributes($user_data);
             $user->setUpdatedOn(DateTimeValueLib::now());
             $user->save();
             DB::commit();
             flash_success(lang('success user permissions updated'));
             ajx_current("back");
         } catch (Exception $e) {
             DB::rollback();
             flash_error($e->getMessage());
             ajx_current("empty");
         }
     }
     // if
 }
 /**
  * List all company projects
  *
  * @access public
  * @param void
  * @return null
  */
 function projects()
 {
     if (!logged_user()->isCompanyAdmin(owner_company())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     // if
     $projects = null;
     if (can_manage_workspaces(logged_user())) {
         $padres = Projects::getAll('name', 'p2 = 0');
         //traigo todos los nivel 1
     } else {
         $padres = logged_user()->getProjects('name', 'p2 = 0');
     }
     foreach ($padres as $hijo) {
         $projects[] = $hijo;
         $aux = $hijo->getSortedChildren(logged_user());
         if (is_array($aux)) {
             foreach ($aux as $a) {
                 $projects[] = $a;
             }
         }
     }
     tpl_assign('projects', $projects);
 }