* @license PHP License * @package WB * @subpackage content */ /** * Load base class */ WBClass::load( 'WBContent' ); /** * Content component: Soho Project Manager * * Project manager for SOHO * * @version 1.0.0 * @package WB * @subpackage content */ class WBContent_Soho extends WBContent { /** * my parameter list * @var array */ protected $config = array( 'action' => 'effortlist', 'customerid' => '', 'projectid' => '', 'effortid' => '', 'goto' => '0', 'limit' => '20', 'requiredgroup' => 'staff', 'requiredgroupmanager' => 'manager' ); /** * table * @var WBDatasource_Table */ protected $table; /** * remember id of saved item * @var array */ private $saved = null; /** * constructor * * */ public function __construct() { parent::__construct(); $this->table = WBClass::create('WBDatasource_Table'); } /** * run * * run component * * @todo become user-aware for blog categories * @return array parameter list */ public function run() { // group required if (!$this->isUserInGroup($this->config['requiredgroup'])) { $this->config['action'] = 'anonymous'; return $this->config; } // manager? $clause = array(); if (!$this->isUserInGroup($this->config['requiredgroupmanager'])) { $clause[] = array( 'field' => $this->table->getIdentifier('user'), 'value' => $this->user->getId() ); if (strncmp('effort', $this->config['action'], 6) != 0) { $this->config['action'] = 'effortlist'; } } // verify primary key if (!empty($this->config['effortid'])) { $data = $this->table->get('sohoeffort', $this->config['effortid'], null, $clause); if (count($data) != 1) { $this->config['effortid'] = ''; } } $this->tmpl->addGlobalVars($this->config); return $this->config; } /** * receive output * * fetch output of this content component * * @return string */ public function getString() { $values = array(); switch ($this->config['action']) { case 'anonymous': $this->loadTemplates('anonymous'); return $this->tmpl->getParsedTemplate('snippet'); break; case 'customerrm': if (!empty($this->config['customerid'])) { $this->table->delete('sohocustomer', $this->config['customerid']); } $this->config['customerid'] = ''; // fall through case 'customerlist': $this->loadTemplates('customerlist'); $list = $this->table->get('sohocustomer'); $this->tmpl->addRows('customer_list_entry', $list); break; case 'customeredit': $values = $this->table->get('sohocustomer', $this->config['customerid']); if (!count($values) == 1) { $values = array(array()); $this->config['customerid'] = ''; } $this->tmpl->addGlobalVar('customerid', $this->config['customerid']); $values = $values[0]; return $this->processForm('customer', $values); break; case 'customeradd': $this->config['customerid'] = ''; return $this->processForm('customer', $values); break; case 'projectlist': $this->loadTemplates('projectlist'); $clause = array(); if (!empty($this->config['customerid'])) { $clause[] = array( 'field' => $this->table->getIdentifier('sohocustomer'), 'value' => $this->config['customerid'] ); } $list = $this->table->get('sohoproject', null, null, $clause); $this->tmpl->addRows('project_list_entry', $list); break; case 'projectrm': if (!empty($this->config['projectid'])) { $this->table->delete('sohoproject', $this->config['projectid']); } // fall through case 'projectadd': $this->config['projectid'] = '__new'; $values = array( 'customerid' => $this->config['customerid'] ); $this->tmpl->addGlobalVar('customerid', $this->config['customerid']); return $this->processForm('project', $values); break; case 'projectedit': $values = $this->table->get('sohoproject', $this->config['projectid']); if (!count($values) == 1) { $values = array(array()); $this->config['customerid'] = ''; } $this->tmpl->addGlobalVar('customerid', $this->config['customerid']); $this->tmpl->addGlobalVar('projectid', $this->config['projectid']); $values = $values[0]; return $this->processForm('project', $values); break; case 'effortrm': if (!empty($this->config['effortid'])) { $this->table->delete('sohoeffort', $this->config['effortid']); } // fall through case 'effortedit': // fall through default: $html = $this->processForm('effort'); if ($this->saved) { $this->req->set('save'); $this->tmpl->addGlobalVar('effort_saved', $this->saved); $html = $this->processForm('effort'); } $this->tmpl->addGlobalVar('effort_form', $html); $this->loadTemplates('effortlist'); break; } $this->displayEffortList(); return $this->tmpl->getParsedTemplate('snippet'); } /** * Validation completed * * Save effort record * * @param patForms $form * @param array $values */ protected function onEffortValid($form, $values) { $save = array( $this->table->getIdentifier('sohoproject') => $values['projectid'], 'actual' => $values['actual'], 'created' => $values['created'], 'description' => $values['description'] ); $id = $this->config['effortid']; if (empty($id)) { $id = '__new'; $save[$this->table->getIdentifier('user')] = $this->user->getId(); } $this->saved = $this->table->save('sohoeffort', $id, $save); return true; } /** * Validation completed * * Save project data * * @param patForms $form * @param array $values */ protected function onProjectValid($form, $values) { $save = array( $this->table->getIdentifier('sohocustomer') => $values['customerid'], 'project' => $values['project'], 'projectdescription' => $values['projectdescription'], 'active' => intval($values['active']) ); $id = $this->config['projectid']; if (empty($id)) { $id = '__new'; } $this->table->save('sohoproject', $id, $save); return true; } /** * Validation completed * * Save customer data * * @param patForms $form * @param array $values */ protected function onCustomerValid($form, $values) { $save = array( 'customer' => $values['customer'], 'customerdescription' => $values['customerdescription'] ); $id = $this->config['customerid']; if (empty($id)) { $id = '__new'; } $this->table->save('sohocustomer', $id, $save); return true; } /** * Fetch list of form elements * * Load form element definition using parent class and add project ids * * @param string name of the xml- and template-filename * @return array $elements */ protected function getFormElementList($name) { $list = parent::getFormElementList($name); switch ($name) { case 'effort'; // add project list ids $list['projectid']['attributes']['values'] = array(); break; case 'project': // add project list ids $list['customerid']['attributes']['values'] = array(); break; default: return $list; } // insert project ids $cid = $this->table->getIdentifier('sohocustomer'); $pid = $this->table->getIdentifier('sohoproject'); $cus = $this->table->get('sohocustomer'); foreach ($cus as $customer) { // add customer ids if ($name == 'project') { $list['customerid']['attributes']['values'][] = array( 'label' => $customer['customer'], 'value' => $customer[$cid] ); continue; } // add project ids $clause = array(); $clause[] = array( 'field' => 'active', 'value' => 1 ); $clause[] = array( 'field' => $cid, 'value' => $customer[$cid] ); $pro = $this->table->get('sohoproject', null, null, $clause); if (empty($pro)) { continue; } $values = array( 'label' => $customer['customer'], 'values' => array() ); foreach ($pro as $project) { $values['values'][] = array( 'label' => $project['project'], 'value' => $project[$pid] ); } $list['projectid']['attributes']['values'][] = $values; } return $list; } /** * add list of efforts to template * * Wnenever there is a template that might hold efforts, fill it will recent efforts */ protected function displayEffortList() { if (!$this->tmpl->exists('effort_list_entry')) { return; } $clause = array(); $clause[] = array( 'field' => 'actual', 'relation' => 'gt', 'value' => -1 ); if (!$this->isUserInGroup($this->config['requiredgroupmanager'])) { $clause[] = array( 'field' => $this->table->getIdentifier('user'), 'value' => $this->user->getId() ); } $options = array(); if ($this->config['limit'] && intval($this->config['limit']) > 0) { $options['limit'] = intval($this->config['limit']); } $options['column'] = array( array('table' => 'sohoproject', 'field' => '*'), array('table' => 'sohoeffort', 'field' => '*') ); $options['join'] = array(); $options['join'][] = array( 'table' => 'sohoproject', 'using' => 'projectid' ); $pager = $this->table->getPager($this->part . __CLASS__, 'sohoeffort', null, $clause, $options); $this->tmpl->addGlobalVars($pager->browse($this->config['goto']), 'pager_'); $this->tmpl->addRows('effort_list_entry', $pager->get()); } /** * location of form config * * Return sub directory where form element definitions are located * * @return string folder */ protected function getFormConfigDir() { return 'soho/form'; } } ?>