示例#1
0
    /**
     * 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);
    }
示例#2
0
    protected function renderJQueryDatepicker($model, One_Dom $d)
    {
        // include most common jquery files from vendor
        One_Vendor::requireVendor('jquery/one_loader');
        $id = $this->getID();
        $name = $this->getName();
        $value = $this->getValue($model);
        $dom = One_Repository::createDom();
        $extraParams = array('default' => $this->getDefault());
        if (in_array($this->getCfg('one'), array('one', 'yes', 'true', '1'))) {
            $extraParams['one'] = 'one';
        }
        if ('readonly' == $this->getCfg('readonly')) {
            $extraParams['readonly'] = 'readonly';
        }
        $tf = new One_Form_Widget_Scalar_Textfield($id, $name, NULL, $extraParams);
        $tf->render($model, $dom);
        $pickerType = 'date';
        $timeFormat = '';
        if (trim($this->getCfg('time')) != '') {
            One_Vendor::getInstance()->loadScript('jquery/js/jquery-ui-timepicker-addon.js', 'head', 10);
            One_Vendor::getInstance()->loadStyle('jquery/css/ui.timepicker.addon.css', 'head', 10);
            $pickerType = 'datetime';
            $timeFormat = '
										timeFormat: "hh:mm:ss",';
        }
        $script = '
		jQuery("#' . $id . '").' . $pickerType . 'picker({
										dateFormat: "yy-mm-dd",' . $timeFormat . '
										showButtonPanel: true' . ('' != trim($value) ? ',
										defaultDate: "' . $value . '"' : '') . '
									});';
        One_Vendor::getInstance()->loadScriptDeclaration($script, 'onload');
        $d->addDom($dom);
    }