public function get($formFile, One_Model $model, array $options = array()) { $cx = new One_Context(); $formName = 'oneForm'; $action = ''; $task = 'edit'; $scheme = $model->getScheme(); if (isset($options['action'])) { $action = $options['action']; } if (isset($options['formName'])) { $formName = $options['formName']; } if (is_null($model->task) || trim($model->task) == '') { if (!is_null($cx->get('task'))) { $task = $cx->get('task'); } if (isset($options['task'])) { $task = $options['task']; } } else { $task = $model->task; } $form = One_Form_Factory::createForm($scheme, $formFile, NULL, $formName, $action); return $form; }
/** * 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 $dom) { $src = $this->getCfg('src'); if (trim($src) == '') { throw new One_Exception("A field of type 'nscript' should have a 'src'-attribute defining the nanoScript file to parse."); } One_Script_Factory::saveSearchPath(); One_Script_Factory::clearSearchPath(); $useLang = $this->getCfg('language'); if ('' == trim($useLang)) { $useLang = strtolower(One_Config::get('app.language')); } die('deprecated stuff found in ' . __FILE__ . ':' . __LINE); $cps = One_Config::getInstance()->getCustomPaths(); foreach ($cps as $cp) { One_Script_Factory::addSearchPath($cp . '/views/' . One_Config::get('app.name') . '/' . $model->getScheme()->getName() . '/language/' . $useLang . '/'); } foreach ($cps as $cp) { One_Script_Factory::addSearchPath($cp . '/views/' . One_Config::get('app.name') . '/' . $model->getScheme()->getName() . '/'); } $ns = new One_Script(); $ns->load($src); if (!$ns->isError()) { if ($this->getID()) { $ns->set('id', $this->getID()); } if ($this->getName()) { $ns->set('name', $this->getName()); } if ($this->getLabel()) { $ns->set('label', $this->getLabel()); } if ($this->getValue($model)) { $ns->set('value', $this->getValue($model)); } $ns->set('model', $model); $dom->add($ns->execute()); } else { throw new One_Exception($ns->error); } $dom->add($this->value); One_Script_Factory::restoreSearchPath(); }
/** * When the model is loaded, add a slug-field to the model that is composed of the specified fields * * @param One_Scheme $scheme * @param One_Model $model */ public function afterLoadModel(One_Scheme $scheme, One_Model $model) { if (null !== $scheme->getAttribute('slug')) { // don't create the slug if the attribute "slug" actually exists return; } $options = $scheme->get('behaviorOptions.slug'); $createFrom = $options['createFrom']; $parts = preg_split('/\\+/', $createFrom); $mangled = array(); foreach ($parts as $part) { if (preg_match('/^([a-z0-9\\_\\-]+):([a-z0-9\\_\\-]+)$/', $part, $matches) > 0) { $scheme = $model->getScheme(); $link = $scheme->getLink($matches[1]); if (!is_null($link)) { if ($link->getAdapterType() == 'manytoone') { $related = $model->getRelated($matches[1]); if (!is_null($related)) { $targetPart = $matches[2]; $mangle = !is_null($related->{$targetPart}) ? trim($this->mangle($related->{$targetPart})) : NULL; if (!is_null($mangle)) { $mangled[] = $mangle; } } } } } else { $mangle = !is_null($model->{$part}) ? trim($this->mangle($model->{$part})) : NULL; if (!is_null($mangle)) { $mangled[] = $mangle; } } } if (count($mangled) > 0) { $model->slug = implode('_', $mangled); } }
public function getAlias(One_Model $model) { $scheme = $model->getScheme(); $aliasOpts = $scheme->get('behaviorOptions.linkalias'); if (null !== $behaviorOpts && isset($behaviorOpts['attribute'])) { $aliasField = $behaviorOpts['attribute']; $alias = $model->{$aliasField}; } elseif (isset($model->slug)) { $alias = $model->slug; } else { $idAttr = $model->getIdentityName(); $alias = $model->{$idAttr}; } return $alias; }
/** * 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); }
/** * 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) { $this->setCfg('class', 'OneFieldDropdown ' . $this->getCfg('class')); // fetch all data to do with the relationship $parts = explode(':', $this->getCfg('role')); $related = $model->getRelated($parts[1]); $targetAttr = $this->getCfg('targetAttribute'); $triggerOn = intval($this->getCfg('triggerOn')) > 0 ? intval($this->getCfg('triggerOn')) : 2; $scheme = $model->getScheme(); $idAttr = $scheme->getIdentityAttribute()->getName(); $link = $scheme->getLink($parts[1]); $relatedIDs = ''; $options = array(); if (is_array($related) && count($related) > 0) { $relatedIDArray = array(); $idAttr = One_Repository::getScheme($link->getTarget())->getIdentityAttribute()->getName(); $tparts = explode(':', $targetAttr); foreach ($related as $relate) { if (is_null($idAttr)) { $scheme = $model->getScheme(); $idAttr = $scheme->getIdentityAttribute()->getName(); } $value = $relate->{$idAttr}; $shown = ''; foreach ($tparts as $tpart) { $shown .= $relate->{$tpart} . ' '; } $options[$value] = $shown; $relatedIDArray[] = $relate->{$idAttr}; } $relatedIDs = implode('^,^', $relatedIDArray); } $data = array('id' => $this->getID(), 'name' => $this->getFormName(), 'events' => $this->getEventsAsString(), 'params' => $this->getParametersAsString(), 'required' => $this->isRequired() ? ' *' : '', 'value' => is_null($this->getValue($model)) ? $this->getDefault() : $this->getValue($model), 'options' => $options, 'onEmpty' => strtolower($this->getCfg('onEmpty')), 'triggerOn' => $triggerOn, 'scheme' => $scheme, 'link' => $link, 'targetAttr' => $targetAttr, 'modelID' => $model->{$idAttr}, 'relatedIDs' => $relatedIDs, 'info' => $this->getCfg('info'), 'error' => $this->getCfg('error'), 'class' => $this->getCfg('class'), 'label' => $this->getLabel(), 'lblLast' => $this->getCfg('lblLast')); // $dom = new One_Dom(); // dom for head section // // $head = '<script type="text/javascript" src="' . One::getInstance()->getUrl() . 'lib/libraries/js/featherajax.js"></script>'; // $dom->add( $head, '_head' ); $head = ' function setRelatedOptions( selfscheme, scheme, selfId, id, targetAttribute, phrase ) { var self = ""; if( selfscheme == scheme ) self = "&selfId=" + selfId; var aj = new AjaxObject101(); aj.sndReq( "post", "' . One_Config::getInstance()->getUrl() . '/lib/form/ajax/relational.php", "searchscheme=" + scheme + "&dd=f" + id + "&target=" + targetAttribute + "&phrase=" + phrase + self ); } function addChosenOptions( id ) { var dropdown = document.getElementById( "f" + id ); var to = document.getElementById( "t" + id ); for( var i = 0; i < dropdown.length; i++ ) { if( dropdown.options[i].selected == true ) { var option = document.createElement("option"); option.value = dropdown.options[i].value; option.innerHTML = dropdown.options[i].text; var found = false for( var j = 0; j < to.length; j++ ) { if( option.value == to.options[j].value ) { found = true; break; } } if( !found ) { var hidden = document.getElementById( id ); to.appendChild( option ); if( hidden.value != "" ) hidden.value = hidden.value + "^,^" + option.value; else hidden.value = option.value; } } } } function removeChosenOptions( id ) { var to = document.getElementById( "t" + id ); var hidden = document.getElementById( id ); for( var i = ( to.length - 1 ); i >= 0; i-- ) { if( to.options[i].selected == true ) { var pattern = \'((\\^,\\^)?\' + to.options[i].value + \'(\\^,\\^)?)\'; var test = new RegExp( pattern, "gi" ); /* @TODO There is probably an easier way to do this */ hidden.value = hidden.value.replace( test, "" ); hidden.value = hidden.value.replace( /\\^,\\^\\^,\\^/gi, "^,^" ); hidden.value = hidden.value.replace( /^\\^,\\^/gi, "" ); hidden.value = hidden.value.replace( /\\^,\\^$/gi, "" ); to.remove( i ); } } }'; // $dom->add( $head, '_head' ); One_Vendor::getInstance()->loadScript('js/featherajax.js', 200); One_Vendor::getInstance()->loadScriptDeclaration($head, 'head', 200); $content = $this->parse($model, $data); // $d->addDom($dom); $d->addDom($content); }