/** * This method composes the form and returns the output * * @return string Output of the form */ public function execute() { // Fetch the model needed for this form. We will need it because authorization can depend on the model itself if ($this->id) { $factory = One_Repository::getFactory($this->scheme->getName()); $model = $factory->selectOne($this->id); } else { $model = One::make($this->scheme->getName()); } if (is_null($model)) { throw new One_Exception('Could not generate a form for scheme "' . $this->scheme->getName() . '" with id ' . $this->id); } $this->authorize($this->scheme->getName(), $model->id); $session = One_Repository::getSession(); $formFile = $this->getVariable('formFile', 'form'); $form = One_Form_Factory::createForm($this->scheme, $formFile, $this->getVariable('lang'), 'oneForm', ''); // Create a DOM and render the form in it $dom = One_Repository::createDom(); $form->render($model, $dom); // print_r($dom); $this->view->setModel($model); $this->view->set('scheme', $this->scheme); $this->view->set('form', $form); $this->view->set('dom', $dom); $this->view->set('errors', $session->get('errors', 'OneFormErrors')); $vForm = $this->view->show(); $session->remove('errors', 'OneFormErrors'); $session->remove('posted', 'OneFormErrors'); return $vForm; }
/** * Get the search value from the context * * @return mixed */ public function getValue() { $session = One_Repository::getSession(); if ($session->varExists('usedSearchOptions', 'one_search')) { $cx = new One_Context($session->get('usedSearchOptions', 'one_search')); } else { $cx = new One_Context(); } $name = $this->getName(); $value = $cx->get($name); if (!is_null($value)) { return trim($value); } else { return NULL; } }
/** * Determine what type of widget the DOMElement is and load it's conditions * * @param One_Form_Container_Abstract $container * @param DOMElement $element * @param string $widgetClass * @return One_Form_Widget_Abstract */ protected static function determineWidget($container, $element, $widgetClass) { $id = $element->hasAttribute('id') ? $element->getAttribute('id') : $element->getAttribute('attribute'); $name = $element->hasAttribute('name') ? $element->getAttribute('name') : $element->getAttribute('attribute'); $label = $element->hasAttribute('label') ? $element->getAttribute('label') : $element->getAttribute('attribute'); $attributes = array(); $rawAttributes = $element->attributes; for ($i = 0; $i < $rawAttributes->length; $i++) { $attribute = $rawAttributes->item($i); $attributes[$attribute->localName] = $attribute->value; } if (false === isset($attributes['language'])) { $attributes['language'] = strtolower(One_Config::get('app.language')); } $widget = new $widgetClass($id, $name, $label, $attributes); if ($element->hasAttribute('optionsFrom') && method_exists($widget, 'setOptions')) { $parts = explode(':', $element->getAttribute('optionsFrom')); if (2 == count($parts)) { $session = One_Repository::getSession(); $sessionCacheName = md5($id . '#' . $name . '#' . $element->getAttribute('optionsFrom')); if (false === $element->hasAttribute('cacheOptions') || $element->hasAttribute('cacheOptions') && false === $session->varExists($sessionCacheName, 'One_Form_Cache')) { $optView = new One_View($parts[0], $parts[1]); try { $rawOptions = trim($optView->show()); $options = json_decode($rawOptions, 1); if (false === is_array($options)) { $options = array(); } $widget->setOptions($options); if ($element->hasAttribute('cacheOptions')) { $session->set($sessionCacheName, $options, 'One_Form_Cache'); } } catch (Exception $e) { // echo $e->getMessage(); // exit; } } else { $options = $session->get($sessionCacheName, 'One_Form_Cache'); $widget->setOptions($options); } } } self::loadConstraints($element, $widget); $container->addWidget($widget); return $widget; }
/** * Get the value * * @param One_Model $model * @return Mixed */ public function getValue($model) { if (!$model) { return null; } // pick up vars entered entered from an invalidly filled in form $session = One_Repository::getSession(); $posted = $session->get('posted', 'OneFormErrors'); if (!in_array($this->getCfg('one'), array('one', 'yes', 'true', '1')) && isset($posted['oneForm'][$this->getName()])) { return $posted['oneForm'][$this->getName()]; } else { if (isset($posted[$this->getName()])) { return $posted[$this->getName()]; } else { $role = $this->getCfg('role'); if (!is_null($role) && $model instanceof One_Model) { $parts = explode(':', $role); $related = $model->getRelated($parts[1]); $relation = One_Repository::getRelation($parts[0]); $role = $relation->getRole($parts[1]); $relScheme = One_Repository::getScheme($role->schemeName); if ($related instanceof One_Model) { return $related[$relScheme->getIdentityAttribute()->getName()]; } elseif (is_array($related)) { // $related is an array of One_Models $returnValues = array(); foreach ($related as $r) { $returnValues[] = $r[$relScheme->getIdentityAttribute()->getName()]; } return $returnValues; } } else { //PD16FEB10 : add option to pick this up from request if ($this->getCfg('from') != "") { list($source, $key) = preg_split("/:/", $this->getCfg('from')); switch ($source) { case 'request': $from = $_REQUEST; if (!in_array($this->getCfg('one'), array('one', 'yes', 'true', '1'))) { $from = $_REQUEST['oneForm']; } if (isset($from[$key])) { return $from[$key]; } break; } } if ($this->getCfg('novalue') != "novalue") { return $model[$this->getOriginalName()]; } } } } }
public function validate() { $session = One_Repository::getSession(); if ($session->get('OnePhrase', 'OneCaptcha') != $this->requestValue()) { return false; } else { return true; } }
/** * This method validates a submitted form and returns to the proper page according to whether the submission * contained errors or whether the form was saved or applied */ public function execute() { $session = One_Repository::getSession(); $isNew = false; if ($this->id) { // update existing $factory = One_Repository::getFactory($this->scheme->getName()); $model = $factory->selectOne($this->id); if (is_null($model) && !$factory->getScheme()->getIdentityAttribute()->isAutoInc()) { $model = One::make($this->scheme->getName()); $isNew = true; } } else { $model = One::make($this->scheme->getName()); } $idAttrName = $model->getScheme()->getIdentityAttribute()->getName(); $this->authorize($this->scheme->getName(), $model->{$idAttrName}); $formFile = $this->getVariable('formFile', 'form'); $form = One_Form_Factory::createForm($this->scheme, $formFile, $this->getVariable('lang'), 'oneForm', ''); $flow = One_Controller_Flow::getInstance($this->scheme)->getRedirects(); $noErrors = $form->validate(); if ($noErrors || is_array($noErrors) && count($noErrors) == 0) { $form->bindModel($model); if ($this->id && !$isNew) { $model->update(); $id = $this->id; } else { $model->insert(); $idAttr = $this->scheme->getIdentityAttribute()->getName(); $id = $model->{$idAttr}; } $this->model = $model; // handle redirects // @TODO this code can use some cleanup $redirects = array_merge($flow, $form->getRedirects()); $todo = is_null($this->getVariable('action')) ? $this->getVariable('task') : $this->getVariable('action'); if (isset($this->options['flow'])) { $todo = $this->options['flow']; } $redirect = $redirects['default']; if (isset($redirects[$todo])) { $redirect = $redirects[$todo]; } if (isset($redirect['id']) && strtoupper(trim($redirect['id'])) == '::ID::') { $redirect['id'] = $model->{$idAttrName}; } $redirect = $this->replaceOtherVariables($redirect); $this->controller->setRedirect($redirect); } else { $errors = base64_encode(serialize($form->getErrors())); $session->set('executedReturn', $model, 'executedForm'); $session->set('errors', $form->getErrors(), 'OneFormErrors'); $session->set('posted', $_REQUEST, 'OneFormErrors'); $id = $this->id; $toView = 'edit'; if (!is_null($this->getVariable('returnToOne'))) { parse_str(base64_decode($this->getVariable('returnToOne')), $returnVals); $this->controller->setRedirect($returnVals); } else { $redirects = array_merge($flow, $form->getRedirects()); $todo = 'default'; if (isset($this->options['flowerror'])) { $todo = $this->options['flowerror']; } elseif (isset($redirects['formerror'])) { $todo = 'formerror'; } $redirect = $redirects[$todo]; if (isset($redirect['id']) && strtoupper(trim($redirect['id'])) == '::ID::') { $redirect['id'] = $model->{$idAttrName}; } $redirect = $this->replaceOtherVariables($redirect); $this->controller->setRedirect($redirect); } return false; } }
/** * Perform weighted search that returns the results according to matching compatibility * @return array */ protected function performWeightSearch() { $session = One_Repository::getSession(); $allSearchValues = array(); $results = array('count' => 0, 'all' => array(), 'most' => array(), 'some' => array()); // Get all used widgets $form = One_Form_Factory::createForm($this->scheme, $this->searchform); $widgets = $this->getWidgets($form, $this->scheme); $idAttr = $this->scheme->getIdentityAttribute()->getName(); $searchOptions = $this->scheme->get('behaviorOptions.searchable'); $conditions = 0; $weights = array(); $conditionByModel = array(); $specificWeights = array(); foreach ($widgets as $widget) { $widgetData = $widget->getWidgetData(); // Get the widget's name, value and operator // Check whether a widget is a specifier or not $specifier = false; if ($widget->getCfg('specifier')) { $specifier = true; } if (null !== $widgetData['value']) { $allSearchValues[$widgetData['name']] = $widgetData['value']; if (false !== strpos($widgetData['name'], ':')) { $parts = explode(':', $widgetData['name'], 2); $relation = One_Repository::getRelation($parts[0]); // get the used relation $link = $this->scheme->getLink($parts[1]); $targetScheme = One_Repository::getScheme($link->getTarget()); // get the scheme of the related field $tidAttr = $targetScheme->getIdentityAttribute()->getName(); $tSearchOptions = $this->scheme->get('behaviorOptions.searchable'); $wOptions = null; if (method_exists($widget, 'getOptions')) { $wOptions = $widget->getOptions(); } // Get the role of the current scheme as seen from the relation from the target's side $otherRole = null; foreach ($relation->getRoles() as $role) { if ($parts[1] != $role->name) { $otherRole = $role; } } if (null !== $otherRole) { // Dirty trick to enforce the value(s) as an array so they are easier manageable $values = $widgetData['value']; if (!is_array($widgetData['value'])) { $values = array($widgetData['value']); } if (null !== $widgetData['operator']) { $tmpValues = array(); $op = $widgetData['operator']; $allowed = $op->getAllowed(); $opVal = $op->getValue(); if (!is_null($opVal)) { if (in_array($opVal, $allowed)) { $op = $opVal; } else { $op = $allowed['default']; } } else { $op = $allowed['default']; } // Perform special operators foreach ($values as $value) { $tQ = One_Repository::selectQuery($targetScheme->getName()); $tQ->setSelect(array($tidAttr)); $tQ->where($tidAttr, $op, $value); $tmpResults = $tQ->execute(false); foreach ($tmpResults as $tmpResult) { $tmpValues[$tmpResult->{$tidAttr}] = $tmpResult->{$tidAttr}; } } $values = $tmpValues; } // Get all related data // @TODO in the future it could be possible that you don't always get ID's of the related fields, but works for now foreach ($values as $value) { $current = One_Repository::selectOne($targetScheme->getName(), $value); if (!is_null($current)) { if (isset($tSearchOptions['publishField'])) { $tPubField = $searchOptions['publishField']; if (0 == $current->{$tPubField}) { continue; } } $conditions++; $relateds = $current->getRelated($otherRole->name); if (null === $relateds) { continue; } if (!is_array($relateds)) { $relateds = array($relateds); } foreach ($relateds as $related) { if (!isset($weights[$related->{$idAttr}])) { if (false === $specifier) { $weights[$related->{$idAttr}] = 0; } $conditionByModel[$related->{$idAttr}] = array(); } if (!isset($conditionByModel[$related->{$idAttr}][$widgetData['name']])) { $conditionByModel[$related->{$idAttr}][$widgetData['name']] = array(); } if (isset($wOptions[$value])) { $conditionByModel[$related->{$idAttr}][$widgetData['name']][] = $wOptions[$value]; } else { $conditionByModel[$related->{$idAttr}][$widgetData['name']][] = $value; } // if the current widget is a specifier, maintain the data in a separate array to perform an array_intersect to if (true == $specifier) { if (!isset($specificWeights[$related->{$idAttr}])) { $specificWeights[$related->{$idAttr}] = 0; } $specificWeights[$related->{$idAttr}]++; continue; } $weights[$related->{$idAttr}]++; } } } } } else { $values = $widgetData['value']; if (!is_array($widgetData['value'])) { $values = array($widgetData['value']); } if (null !== $widgetData['operator']) { $op = $widgetData['operator']; $allowed = $op->getAllowed(); $opVal = $op->getValue(); if (!is_null($opVal)) { if (in_array($opVal, $allowed)) { $op = $opVal; } else { $op = $allowed['default']; } } else { $op = $allowed['default']; } } else { $op = 'eq'; } foreach ($values as $value) { if ('' != trim($value)) { $conditions++; $cQ = One_Repository::selectQuery($this->scheme->getName()); $cQ->setSelect(array($idAttr)); $cQ->where($widgetData['name'], $op, $value); $tmpResults = $cQ->execute(false); foreach ($tmpResults as $tmpResult) { if (!isset($weights[$tmpResult->{$idAttr}])) { if (false === $specifier) { $weights[$tmpResult->{$idAttr}] = 0; } $conditionByModel[$tmpResult->{$idAttr}] = array(); } if (!isset($conditionByModel[$tmpResult->{$idAttr}][$widgetData['name']])) { $conditionByModel[$tmpResult->{$idAttr}][$widgetData['name']] = array(); } $conditionByModel[$tmpResult->{$idAttr}][$widgetData['name']][] = $value; // if the current widget is a specifier, maintain the data in a separate array to perform an array_intersect to if (true == $specifier) { if (!isset($specificWeights[$tmpResult->{$idAttr}])) { $specificWeights[$tmpResult->{$idAttr}] = 0; } $specificWeights[$tmpResult->{$idAttr}]++; continue; } $weights[$tmpResult->{$idAttr}]++; } } } } } } $tilt = $this->getSearchTilt($conditions); foreach ($weights as $id => $weight) { if (0 < count($specificWeights)) { if (false === array_key_exists($id, $specificWeights)) { unset($weights[$id]); unset($conditionByModel[$id]); continue; } else { $weight += $specificWeights[$id]; } } $model = One_Repository::selectOne($this->scheme->getName(), $id); // using selectOne as the models needed here are already in cache, a global search by there id would take more resources $model->One_Search_Weight = round($weight / $conditions * 100, 2); $model->One_Search_Weight_Conditions = $conditions; $model->One_Search_Weight_ConditionsMet = $weight; $model->One_Search_Weight_ConditionObject = $conditionByModel[$id]; $type = 'some'; if ($conditions == $weight) { $type = 'all'; } elseif ($conditions >= $tilt) { $type = 'most'; } $results[$type][] = $model; $results['count']++; } foreach (array('all', 'most', 'some') as $type) { usort($results[$type], array($this, 'sortResultsByWeight')); } $session->set('results', $results, 'one_search'); return $results; }
/** * Gets all the columns that should be shown in the list * * @return array Array of all columns in the form of object with extra data */ private function getColumns() { $session = One_Repository::getSession(); $exists = true; $filename = One_Locator::locateUsing('list.xml', ONE_LOCATOR_ROOTPATTERN . 'views/' . One_Config::get('app.name') . '/' . $this->scheme->getName() . '/'); if ($filename === null) { $exists = false; } if ($exists) { $xml = @simplexml_load_file($filename); } if ($exists && $xml) { // JL06JAN2009 - if no sorting was clicked, check if the default sort column was set // in the xml file $xmlarray_defs = xmlpm($xml, "/view/columns"); $sort = (string) $xmlarray_defs[0]->attributes()->sort; $limit = (string) $xmlarray_defs[0]->attributes()->limit; if ('' != trim($sort)) { preg_match('/^([^\\+\\-]+)(\\+|\\-)?$/i', $sort, $sortMatch); $this->_sortOrder = 'asc'; $this->_sort = $sortMatch[1]; if (isset($sortMatch[2]) && $sortMatch[2] == '-') { $this->_sortOrder = 'desc'; } } if (0 < abs(intval($limit))) { $this->_limit = abs(intval($limit)); } $xmlarray = $xmlarray_defs[0]->column; $this->_columns = array(); foreach ($xmlarray as $xmlpart) { $tmp = new stdClass(); $name = ''; $setFilter = false; $filterType = 'text'; $filterOptions = array(); $operator = 'contains'; foreach ($xmlpart->attributes() as $key => $val) { switch (strtolower($key)) { case 'name': $tmp->name = (string) $val; break; case 'label': $tmp->label = (string) $val; break; case 'filter': if ($val == 1) { $setFilter = true; } break; case 'filtertype': if (in_array(strtolower($val), array('dropdown', 'text', 'daterange'))) { $filterType = strtolower($val); } break; case 'filteroptions': $options = explode(';', $val); foreach ($options as $option) { $parts = explode('=', $option, 2); $filterOptions[$parts[0]] = $parts[1]; } case 'operator': if (in_array((string) $val, $this->_operators)) { $operator = (string) $val; } default: $tmp->{$key} = (string) $val; } } if ($filterType == 'dropdown' && count($filterOptions) == 0 && trim($tmp->name) != '') { preg_match('/([^:]+)((:)(.+))?/', $tmp->name, $matches); if (!is_null($matches[4])) { $link = $this->scheme->getLink($matches[1]); $target = One_Repository::getScheme($link->getTarget()); $tAtt = $matches[4]; $tFac = One_Repository::getFactory($target->getName()); $tQ = $tFac->selectQuery(); $tQ->setSelect(array($tAtt)); $tQ->setOrder(array($matches[4] . '+')); $options = $tQ->execute(false); foreach ($options as $option) { $filterOptions[$option->{$tAtt}] = $option->{$tAtt}; } } } //PD16SEP09: if no name is given, interpret the body of the tag as CDATA containing nanoScript // TR20100408: change this to only set the name as the label if no name is given. if (!isset($tmp->name)) { $tmp->name = $tmp->label; } //filter operator defaults to contains if (!isset($tmp->name)) { $tmp->operator = 'contains'; } // TR20100408: change this to interpret as nanoscript if a value is passed to the tag if (trim((string) $xmlpart) != '') { $tmp->nScript = (string) $xmlpart; } $this->_columns[$tmp->name] = $tmp; if ($setFilter) { if ($filterType != 'daterange') { $value = JRequest::getVar('s' . $tmp->name, NULL); } else { $value = array(JRequest::getVar('s' . $tmp->name . 'Start', NULL), JRequest::getVar('s' . $tmp->name . 'End', NULL)); } if (is_null($value)) { $value = $session->get($tmp->name, $this->scheme->getName() . '--list'); } $session->set($tmp->name, $value, $this->scheme->getName() . '--list'); $this->_filters[$tmp->name] = array('label' => $tmp->label, 'value' => $value, 'type' => $filterType, 'options' => $filterOptions, 'operator' => $operator); } } } if (is_null($this->_columns)) { $columns = $this->scheme->get('attributes'); $this->_columns = array(); foreach ($columns as $name => $column) { $tmp = new stdClass(); $tmp->label = $column->getName(); $tmp->name = $column->getName(); // TR20100317 used to be $column->column() but should not be used anymore $this->_columns[$tmp->name] = $tmp; } } return $this->_columns; }