/**
	 * Set the site permissions to a given Form object.
	 *
	 * Used by the create and update pages.
	 *
	 * @param Form $form
	 * @param UserGroupModel $model
	 */
	private function _setPermissionsToForm(Form $form, UserGroupModel $model){
		// I want to split up the permission set into a set of groups, based on the first key.
		$groups = [];
		foreach(Core::GetPermissions() as $key => $data){
			if($key{0} == '/'){
				$group = substr($key, 1, strpos($key, '/', 1)-1);
			}
			else{
				$group = 'general';
			}

			if(!isset($groups[$group])){
				$groups[$group] = [];
			}

			// NEW i18n support for config options!
			$i18nKey = \Core\i18n\I18NLoader::KeyifyString($key);
			//$opts['description'] = t('MESSAGE_PERM__' . $i18nKey);
			$groups[$group][$key] = t('STRING_PERMISSION_' . $i18nKey);
		}

		// Now, I can add these groups to the form.
		foreach($groups as $gkey => $options){
			// Make the title a little more friendly.
			$gtitle = ucwords($gkey) . ' Permissions to Assign';

			$form->addElement(
				'checkboxes',
				array(
					'group' => 'Permissions',
					'id' => 'permissions-' . $gkey,
					'name' => 'permissions',
					'title' => $gtitle,
					'options' => $options,
					'value' => $model->getPermissions(),
				)
			);
		}
	}
示例#2
0
}

// Pull all the configuration options for this component
// These get transposed to STRING_CONFIG_config_name_blah
$configs = ConfigModel::Find(['component = ' . $configKey]);
foreach($configs as $c){
	/** @var ConfigModel $c */
	$key = \Core\i18n\I18NLoader::KeyifyString($c->get('key'));
	$matches[] = 'STRING_CONFIG_' . $key;
	$matches[] = 'MESSAGE_CONFIG_' . $key;
}

// Give me permissions!
if($comp){
	foreach($comp->getPermissions() as $key => $p){
		$key = \Core\i18n\I18NLoader::KeyifyString($key);
		$matches[] = 'STRING_PERMISSION_' . $key;
		//$matches[] = 'MESSAGE_CONFIG_' . $key;
	}
}

// Retrieve the models for this component
if($comp){
	$models = array_merge($comp->getModelList(), $comp->getSupplementalModelList());
	foreach($models as $key => $file){
		$class = new ReflectionClass($key);
		if($class->isSubclassOf('Model')){
			$modelName = strtoupper($key);
			$schema = $class->getMethod('GetSchema')->invoke(null);
			$isSupplemental = false;
		}
	public function getFormAttributes(){
		$opts = [];

		$formOptions = $this->get('form_attributes');
		if($formOptions != ''){
			// Explode them by a semicolon
			$formOptions = array_map('trim', explode(';', $formOptions));
			foreach($formOptions as $o){
				if(($cpos = strpos($o, ':')) !== false){
					$k = substr($o, 0, $cpos);
					$v = substr($o, $cpos+1);

					$opts[$k] = $v;
				}
			}
		}

		if(!isset($opts['type'])){
			// Determine this automatically by the config data type.
			switch ($this->get('type')) {
				case 'string':
				case 'int':
					$opts['type'] = 'text';
					break;
				case 'text':
					$opts['type'] = 'textarea';
					break;
				case 'enum':
					$opts['type'] = 'select';
					break;
				case 'boolean':
					$opts['type'] = 'radio';
					break;
				case 'set':
					$opts['type'] ='checkboxes';
					break;
				default:
					$opts['type'] = 'text';
					break;
			}
		}

		// SELECT
		if(($opts['type'] == 'select' || $opts['type'] == 'checkboxes') && trim($this->get('options'))){
			// This is set from the main option set.
			$opts['options'] =  array_map('trim', explode('|', $this->get('options')));
		}

		// RADIO
		if($opts['type'] == 'radio'){
			$opts['options'] = ['false' => t('STRING_NO'), 'true'  => t('STRING_YES')];
		}

		$key = $this->get('key');

		$gname = substr($key, 1);
		$gname = ucwords(substr($gname, 0, strpos($gname, '/')));

		// NEW i18n support for config options!
		$i18nKey = \Core\i18n\I18NLoader::KeyifyString($key);
		$opts['title'] = t('STRING_CONFIG_' . $i18nKey);
		$opts['description'] = t('MESSAGE_CONFIG_' . $i18nKey);

		// Generate the title dynamically from either the title attribute or the key attribute.
		if(!isset($opts['title'])){
			// If the title is set, use that.  Otherwise pull it from the key name less the group.
			if($this->get('title')){
				$opts['title'] = $this->get('title');
			}
			else{
				$title = substr($key, strlen($gname) + 2);
				// Split the title on '/' and capitalize it to make it more user-friendly.
				$title = str_replace('/', ' ', $title);
				// Same thing for underscores '_', remove them and capitalize the words.
				$title = str_replace('_', ' ', $title);
				$title = ucwords($title);

				$opts['title'] = $title;
			}
		}

		// Description can be set dynamically or pulled from the attributes.
		if(!isset($opts['description'])){
			$desc = $this->get('description');
			if ($this->get('default_value') && $desc){
				$desc .= ' (' . t('MESSAGE_DEFAULT_VALUE_IS_S_', $this->get('default_value')) . ')';
			}
			elseif($this->get('default_value')) {
				$desc = t('MESSAGE_DEFAULT_VALUE_IS_S_', $this->get('default_value'));
			}
			$opts['description'] = $desc;
		}

		if(!isset($opts['group'])){
			$opts['group'] = $gname;
		}

		// The name can't be set by the XML metadata, but it is based on the type of form element, slightly.
		if($opts['type'] == 'checkboxes'){
			// Append "[]" to the name as there are many checkboxes!
			$opts['name'] = 'config[' . $key . '][]';
		}
		else{
			$opts['name'] = 'config[' . $key . ']';
		}

		return $opts;
	}