/** * @param One_Scheme $scheme * @param $idOrAlias * * DELETE schemename/ID * Delete an item */ public static function restDelete(One_Scheme $scheme, $idOrAlias) { try { $model = One_Repository::selectOne($scheme->getName(), $idOrAlias); if ($model === null) { throw new One_Exception_Rest_404('Cannot locate instance of scheme ' . $scheme->getName() . ' identified by ' . $idOrAlias); } $model->delete(); $this->slim->response()->status(200); echo 'OK'; } catch (One_Exception_Rest_404 $e) { // return 404 server error $this->slim->response()->status(404); echo '{}'; } catch (Exception $e) { $this->slim->response()->status(400); $this->slim->response()->header('X-Status-Reason', $e->getMessage()); } }
/** * Does the id passed have a the relationship of role "rolename" with the current Query * @param string $roleName * @param mixed $idValue * @return boolean */ public function isRelated($roleName, $idValue) { throw new One_Exception_NotImplemented('One_Query::isRelated() is not properly implemented yet'); $scheme = $this->getScheme(); $link = $scheme->getLink($roleName); $relatedSchemeName = $link->getTarget(); $relatedModel = One_Repository::selectOne($relatedSchemeName, $idValue); $relatedScheme = $relatedModel->getSchemeName(); $relId = $relatedScheme->identityColumn(); }
/** * 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; }
/** * Render the output of the widget and add it to the DOM * * @param One_Model $model * @param One_Dom $d */ protected function _render($model, One_Dom $d) { One_Vendor::requireVendor('jquery/one_loader'); One_Vendor::getInstance()->loadScript('jquery/js/jquery.tagify.js', 'head', 10); if (is_null($this->getCfg('role')) || is_null($this->getCfg('targetAttribute'))) { throw new One_Exception('The Multi-tag widget is only allowed for Many-to-many relations'); } $parts = explode(':', $this->getCfg('role'), 2); $targetAttr = $this->getCfg('targetAttribute'); $link = $model->getScheme()->getLink($parts[1]); if (is_null($link) || !$link->getAdapterType() instanceof One_Relation_Adapter_Manytomany) { throw new One_Exception('The Multi-tag widget is only allowed for Many-to-many relations'); } $tScheme = One_Repository::getScheme($model->getScheme()->getLink($parts[1])->getTarget()); $tIdAttr = $tScheme->getIdentityAttribute()->getName(); // set initial values $value = array(); $values = is_null($this->getValue($model)) ? $this->getDefault() : $this->getValue($model); foreach ($values as $tId) { $tModel = One_Repository::selectOne($tScheme->getName(), $tId); if (null !== $tModel) { $value[$tModel->{$tIdAttr}] = $tModel->{$targetAttr}; } } $data = array('id' => $this->getID(), 'name' => $this->getFormName(), 'events' => $this->getEventsAsString(), 'params' => $this->getParametersAsString(), 'required' => $this->isRequired() ? ' *' : '', 'options' => $this->getOptions(), 'info' => $this->getCfg('info'), 'error' => $this->getCfg('error'), 'class' => $this->getCfg('class'), 'label' => $this->getLabel(), 'lblLast' => $this->getCfg('lblLast')); $dom = $this->parse($model, $data); // $dom->add('<script type="text/javascript" href="'.One_Vendor::getInstance()->getSitePath().'/jquery/js/jquery.tagify.js" />'."\n", '_head'); // $dom->add('<link rel="stylesheet" type="text/css" href="'.One_Vendor::getInstance()->getSitePath().'/jquery/css/tagify-style.css" />'."\n", '_head'); //One_Vendor::getInstance()->loadScript('jquery/js/jquery.tagify.js', 'head', 20); One_Vendor::getInstance()->loadStyle('jquery/css/tagify-style.css', 'head', 20); // Prepare autocomplete tagfield $script = ' var oneTags' . $this->getID() . ' = jQuery("#' . $this->getID() . '").tagify(); oneTags' . $this->getID() . '.tagify("inputField").autocomplete({ source: function(req, add) { req.field = "' . $targetAttr . '"; req.query = "formtags"; //pass request to server jQuery.getJSON("' . One_Config::getInstance()->getSiterootUrl() . One_Config::getInstance()->getAddressOne() . '&scheme=' . $tScheme->getName() . '&task=json", req, function(data) { //create array for response objects var suggestions = []; //process response jQuery.each(data, function(i, val) { var keyval = { "label": val.' . $targetAttr . ', "value": val.' . $tIdAttr . ' }; suggestions.push(keyval); }); //pass array to callback add(suggestions); }); }, position: { of: oneTags' . $this->getID() . '.tagify("containerDiv") }, select: function(e, ui) { oneTags' . $this->getID() . '.tagify("inputField").val(ui.item.label); oneTags' . $this->getID() . '.tagify("add", ui.item.value, ui.item.label); oneTags' . $this->getID() . '.tagify("inputField").val(""); return false; }, focus: function(e, ui) { oneTags' . $this->getID() . '.tagify("inputField").val(ui.item.label); return false; }, change: function(e, ui) { oneTags' . $this->getID() . '.tagify("inputField").val(""); return false; } }); '; foreach ($value as $tId => $tVal) { $script .= ' oneTags' . $this->getID() . '.tagify("add", "' . $tId . '", "' . $tVal . '");'; } // $dom->add($script, '_onload'); One_Vendor::getInstance()->loadScriptDeclaration($script, 'onload', 20); $d->addDom($dom); }
public function countRelated(One_Relation_Adapter $link, One_Model $model, array $options = array()) { $linkName = $link->getName(); // identify the target scheme $source = One_Repository::getScheme($model->getSchemeName()); // PD 22OCT08 if ($link->getTarget() == '*') { $col = $link->getName() . '_scheme'; $target = One_Repository::getScheme($model->{$col}); } else { $target = One_Repository::getScheme($link->getTarget()); } // determine the target's identity column $column = $this->localFK($link, $target); // bind the data using the data $localValue = $model[$column]; // create query and execute return One_Repository::selectOne($target->getName(), $localValue) ? 1 : 0; }