コード例 #1
0
/**
 * Render and add a form element to both the PHP session and HTML output.
 *
 * @experimental
 * @param array  $params  Associative (and/or indexed) array of smarty parameters passed in from the template
 * @param Smarty $smarty  Parent Smarty template object
 *
 * @return string
 * @throws SmartyException
 */
function smarty_function_add_form_element($params, $smarty){

	if(!isset($params['form'])){
		$form = null;
	}
	elseif($params['form'] instanceof Form){
		$form = $params['form'];
	}
	elseif($params['form'] instanceof \Core\ListingTable\Table){
		$form = $params['form']->getEditForm();
	}
	else{
		throw new SmartyException('Unsupported value provided for "form", please ensure it is either a valid Form object or omitted completely!');
	}

	$type = isset($params['type']) ? $params['type'] : 'text';

	$element = FormElement::Factory($type, $params);

	if($form){
		$form->addElement($element);
	}

	// Assign or render?
	if(isset($params['assign'])){
		$smarty->assign($params['assign'], $element->render());
	}
	else{
		echo $element->render();
	}
}
コード例 #2
0
	/**
	 * Page to test the UI of form elements.
	 *
	 * This will generate a form with every registered form element.
	 */
	public function testui(){
		$view = $this->getView();
		$request = $this->getPageRequest();

		if(!\Core\user()->checkAccess('g:admin')){
			// This test page is an admin-only utility.
			return View::ERROR_ACCESSDENIED;
		}

		$form = new Form();

		// What type of orientation do you want to see?
		$orientation = $request->getParameter('orientation');
		if(!$orientation){
			$orientation = 'horizontal';
		}
		$required = ($request->getParameter('required'));
		$error    = ($request->getParameter('error'));


		$form->set('orientation', $orientation);

		$mappings = Form::$Mappings;
		// Make them alphabetical.
		ksort($mappings);

		foreach($mappings as $k => $v){
			try{
				$atts = [
					'name' => $k,
					'title' => $v,
					'description' => 'This form element is a ' . $v . ', registered to the key ' . $k . '.',
				];

				if($required) $atts['required'] = true;

				// Some form elements have particular requirements.
				switch($v){
					case 'FormFileInput':
					case 'MultiFileInput':
						$atts['basedir'] = 'tmp/form/testui';
						break;
					case 'FormPagePageSelectInput':
						$atts['templatename'] = 'foo';
						break;
					case 'FormPageInsertables':
						$atts['baseurl'] = '/';
						break;
					case 'FormPageMeta':
						$atts['name'] = 'test';
						break;
					case 'FormCheckboxesInput':
					case 'FormRadioInput':
						$atts['options'] = ['key1' => 'Key 1', 'key2' => 'Key 2'];
						break;
				}
				$el = FormElement::Factory($k, $atts);

				if($error && $el instanceof FormElement){
					$el->setError('Something bad happened', false);
				}
				$form->addElement( $el );
			}
			catch(Exception $e){
				\Core\set_message('Form element ' . $v . ' failed to load due to ' . $e->getMessage(), 'error');
			}
		}

		$view->title = 'Test Form Element UI/UX';
		$view->assign('form', $form);
		$view->assign('orientation', $orientation);
		$view->assign('required', $required);
		$view->assign('error', $error);
	}
コード例 #3
0
ファイル: FormPages.php プロジェクト: nicholasryan/CorePlus
	public function  __construct($atts = null) {
		error_log(__CLASS__ . ' is candidate for removal, please change this code!', E_USER_DEPRECATED);

		// The inbound options may vary slightly.
		if(isset($atts['model']) && $atts['model'] instanceof PageModel){
			$page = $atts['model'];
			$atts['baseurl'] = $page->get('baseurl');
		}
		elseif(isset($atts['baseurl'])){
			$page = new PageModel($atts['baseurl']);
		}
		else{
			throw new Exception('pageinsertables form needs at least the parameter "model" or "baseurl"!');
		}


		// Some defaults
		if(!isset($atts['title'])) $atts['title'] = 'Page Content';
		if(!isset($atts['name'])) $atts['name'] = 'insertables';

		parent::__construct($atts);

		// The prefix for all elements on this group.
		$prefix = $this->get('name') ? $this->get('name') : 'insertables';

		// I need to add the selector here as well.  This select box should be with the insertables because
		// it directly affects the content options.
		$this->_selector = FormElement::Factory(
			'pagepageselect',
			array(
				'name' => $prefix . '_page_template',
				'title' => 'Alternative Page Template',
				'templatename' => $page->getBaseTemplateName(),
			)
		);

		// Remember, objects are passed by reference :)
		$this->addElement($this->_selector);

		$this->setTemplateName($page->get('page_template'));
	}
コード例 #4
0
	/**
	 * Get this column value as a valid form element.
	 * 
	 * @return \FormElement|null
	 */
	public function getAsFormElement(){
		
		$attributes = $this->getFormElementAttributes();
		$type = isset($this->formAttributes['type']) ? $this->formAttributes['type'] : 'text';
		
		if($type == 'disabled'){
			// Disabled form elements do not render to anything.
			return null;
		}
		
		$el = \FormElement::Factory($type, $attributes);
		
		return $el;
	}
コード例 #5
0
	/**
	 * Transpose a populated form element from the underlying ConfigModel object.
	 * Will populate the name, options, validation, etc.
	 *
	 * @return \FormElement
	 *
	 * @throws \Exception
	 */
	public function getAsFormElement(){
		// key is in the format of:
		// /user/displayname/displayoptions

		$key        = $this->get('key');
		$attributes = $this->getFormAttributes();
		$val        = \ConfigHandler::Get($key);
		$type       = $attributes['type'];
		$el         = \FormElement::Factory($type, $attributes);

		if($type == 'radio'){
			// Ensure that this matches what the radios will have.
			if ($val == '1' || $val == 'true' || $val == 'yes') $val = 'true';
			else $val = 'false';
		}

		if($this->get('type') == 'int' && $type == 'text'){
			$el->validation        = '/^[0-9]*$/';
			$el->validationmessage = $attributes['group'] . ' - ' . $attributes['title'] . ' expects only whole numbers with no punctuation.';
		}

		if($type == 'checkboxes' && !is_array($val)){
			// Convert the found value to an array so it matches what checkboxes are expecting.
			$val  = array_map('trim', explode('|', $val));
		}

		$el->set('value', $val);
		
		// If multisite is enabled and this config is NOT set to overrideable, then set the field as read only!
		if(Core::IsComponentAvailable('multisite') && MultiSiteHelper::IsEnabled() && MultiSiteHelper::GetCurrentSiteID()){
			if(!$this->get('overrideable')){
				$el->set('readonly', true);
				$el->set('disabled', true);
			}
		}

		return $el;
	}