/** * This function will return paginated result. Result is an array where first element is * array of returned object and second populated pagination object that can be used for * obtaining and rendering pagination data using various helpers. * * Items and pagination array vars are indexed with 0 for items and 1 for pagination * because you can't use associative indexing with list() construct * * @access public * @param array $arguments Query argumens (@see find()) Limit and offset are ignored! * @param integer $items_per_page Number of items per page * @param integer $current_page Current page number * @return array */ function paginate($arguments = null, $items_per_page = 10, $current_page = 1) { if (isset($this) && instance_of($this, 'WorkspaceBillings')) { return parent::paginate($arguments, $items_per_page, $current_page); } else { return WorkspaceBillings::instance()->paginate($arguments, $items_per_page, $current_page); //$instance =& WorkspaceBillings::instance(); //return $instance->paginate($arguments, $items_per_page, $current_page); } // if }
/** * Edit project * * @param void * @return null */ function edit() { if (logged_user()->isGuest()) { flash_error(lang('no access permissions')); ajx_current("empty"); return; } $this->setTemplate('add_project'); $project = Projects::findById(get_id()); if (!$project instanceof Project) { flash_error(lang('project dnx')); ajx_current("empty"); return; } // if if (!$project->canEdit(logged_user())) { flash_error(lang('no access permissions')); ajx_current("empty"); return; } // if $project_data = array_var($_POST, 'project'); if (!is_array($project_data)) { $project_data = array('name' => $project->getName(), 'description' => $project->getDescription(), 'show_description_in_overview' => $project->getShowDescriptionInOverview(), 'color' => 0); // array } // if $projects = logged_user()->getActiveProjects(); tpl_assign('project', $project); tpl_assign('projects', $projects); tpl_assign('project_data', $project_data); tpl_assign('billing_amounts', $project->getBillingAmounts()); tpl_assign('subject_matter_experts', ProjectContacts::getContactsByProject($project)); if (is_array(array_var($_POST, 'project'))) { if (array_var($project_data, 'parent_id') == $project->getId()) { flash_error(lang("workspace own parent error")); ajx_current("empty"); return; } if (!isset($project_data['parent_id'])) { $project_data['parent_id'] = $project->getParentId(); } $project->setFromAttributes($project_data); try { DB::beginWork(); if (array_var($project_data, 'parent_id') != $project->getParentId()) { if ($project->getParentWorkspace() instanceof Project && !logged_user()->isProjectUser($project->getParentWorkspace())) { flash_error(lang('no access permissions')); ajx_current("empty"); return; } // if $parent = Projects::findById(array_var($project_data, 'parent_id')); if ($parent) { if (!$project->canSetAsParentWorkspace($parent)) { flash_error(lang('error cannot set workspace as parent', $parent->getName())); ajx_current("empty"); return; } } $project->setParentWorkspace($parent); } $project->save(); /* Billing */ WorkspaceBillings::clearByProject($project); $billings = array_var($project_data, 'billing', null); if ($billings) { foreach ($billings as $billing_id => $billing) { if ($billing['update'] && $billing['value'] && $billing['value'] != 0) { $wb = new WorkspaceBilling(); $wb->setProjectId($project->getId()); $wb->setBillingId($billing_id); $value = $billing['value']; if (strpos($value, ',') && !strpos($value, '.')) { $value = str_replace(',', '.', $value); } $wb->setValue($value); $wb->save(); } } } /* Project contacts */ if (can_manage_contacts(logged_user())) { ProjectContacts::clearByProject($project); $contacts = array_var($project_data, 'contacts', null); if ($contacts) { foreach ($contacts as $contact_data) { $contact = Contacts::findById($contact_data['contact_id']); if ($contact instanceof Contact) { $pc = new ProjectContact(); $pc->setProjectId($project->getId()); $pc->setContactId($contact_data['contact_id']); $pc->setRole($contact_data['role']); $pc->save(); } } } } /* <permissions> */ $permissions = null; $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::clearByProject($project, 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($project->getId()); $relation->setUserId($perm->wsid); $relation->setCheckboxPermissions($perm->pc, $relation->getUserOrGroup()->isGuest() ? false : true); $relation->setRadioPermissions($perm->pr, $relation->getUserOrGroup()->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 /* </permissions> */ $object_controller = new ObjectController(); $object_controller->add_custom_properties($project); ApplicationLogs::createLog($project, null, ApplicationLogs::ACTION_EDIT, false, true); DB::commit(); if (logged_user()->isProjectUser($project)) { $workspace_info = $this->get_workspace_info($project); evt_add("workspace edited", $workspace_info); } flash_success(lang('success edit project', $project->getName())); ajx_current("back"); return; } catch (Exception $e) { DB::rollback(); flash_error($e->getMessage()); ajx_current("empty"); } // try } // if }
/** * Return manager instance * * @access protected * @param void * @return WorkspaceBillings */ function manager() { if (!$this->manager instanceof WorkspaceBillings) { $this->manager = WorkspaceBillings::instance(); } return $this->manager; }
/** * Returns an array with a list of values and information about where they were obtained from * * @param array $billing_category_ids */ function getBillingAmounts($billing_categories = null) { if (!$billing_categories) { $billing_categories = BillingCategories::findAll(); } if ($billing_categories && count($billing_categories) > 0) { $result = array(); $billing_category_ids = array(); foreach ($billing_categories as $category) { $billing_category_ids[] = $category->getId(); } $wsBillingCategories = WorkspaceBillings::findAll(array('conditions' => 'project_id = ' . $this->getId() . ' and billing_id in (' . implode(',', $billing_category_ids) . ')')); if ($wsBillingCategories) { foreach ($wsBillingCategories as $wsCategory) { for ($i = 0; $i < count($billing_categories); $i++) { if ($billing_categories[$i]->getId() == $wsCategory->getBillingId()) { $result[] = array('category' => $billing_categories[$i], 'value' => $wsCategory->getValue(), 'origin' => $this->getId()); array_splice($billing_categories, $i, 1); array_splice($billing_category_ids, $i, 1); break; } } } } if (count($billing_categories) > 0) { if ($this->getParentWorkspace() instanceof Project) { $resultToConcat = $this->getParentWorkspace()->getBillingAmounts($billing_categories); foreach ($resultToConcat as $resultValue) { $result[] = array('category' => $resultValue['category'], 'value' => $resultValue['value'], 'origin' => $resultValue['origin'] == 'default' ? 'default' : 'inherited'); } } else { foreach ($billing_categories as $category) { $result[] = array('category' => $category, 'value' => $category->getDefaultValue(), 'origin' => 'default'); } } } return $result; } else { return null; } }
function delete() { $workspace_billings = WorkspaceBillings::find(array('conditions' => 'billing_id = ' . $this->getId())); if ($workspace_billings) { foreach ($workspace_billings as $billing) { $billing->delete(); } } $users = $this->getCategoryUsers(); if ($users) { foreach ($users as $user) { $user->setDefaultBillingId(0); $user->save(); } } parent::delete(); }