/**
	 * Internal function to parse and handle the user configs in the component.xml file.
	 * This is used for installations and upgrades.
	 *
	 * Returns false if nothing changed, else will return an int of the number of configuration options changed.
	 *
	 * @param boolean $install   Set to false to force uninstall/disable mode.
	 * @param int     $verbosity (default 0) 0: standard output, 1: real-time, 2: real-time verbose output.
	 *
	 * @return boolean | int
	 * @throws InstallerException
	 */
	public function _parseUserConfigs($install = true, $verbosity = 0) {
		// If the class isn't available, don't do anything here.
		// This is possible if I'm currently loading the user component!
		if(!class_exists('UserConfigModel')) return false;

		// Keep track of if this changed anything.
		$changes = array();

		$action = $install ? 'Installing' : 'Uninstalling';

		Core\Utilities\Logger\write_debug($action . ' User Configs for ' . $this->getName());

		// I need to get the schema definitions first.
		$node = $this->_xmlloader->getElement('userconfigs', false);

		if($node){
			trigger_error('Use of the <userconfigs/> metatag is deprecated in favour of the <users/> metatag.  (In the ' . $this->getName() . ' component)', E_USER_DEPRECATED);
		}
		else{
			// Try the 2.8 version, <users/>.
			$node = $this->_xmlloader->getElement('users');
		}

		// Now, get every table under this node.
		foreach ($node->getElementsByTagName('userconfig') as $confignode) {
			/** @var DOMElement $confignode */

			//<userconfig key="first_name" name="First Name"/>
			//<userconfig key="last_name" name="Last Name" default="" formtype="" onregistration="" options=""/>

			$key        = $confignode->getAttribute('key');
			$name       = $confignode->getAttribute('name');
			$default    = $confignode->getAttribute('default');
			$formtype   = $confignode->getAttribute('formtype');
			$onreg      = $confignode->getAttribute('onregistration');
			$onedit     = $confignode->getAttribute('onedit');
			$hidden     = $confignode->getAttribute('hidden');
			$options    = $confignode->getAttribute('options');
			$searchable = $confignode->getAttribute('searchable');
			$validation = $confignode->getAttribute('validation');
			$required   = $confignode->getAttribute('required');
			$weight     = $confignode->getAttribute('weight');

			// Defaults
			if($onreg === null)      $onreg = 1;
			if($onedit === null)     $onedit = 1;
			if($searchable === null) $searchable = 0;
			if($required === null)   $required = 0;
			if($weight === null)     $weight = 0;
			if($weight == '')        $weight = 0;
			if($hidden === null)     $hidden = 0;

			// OVERRIDES!
			// Any hidden config option must be set to not-onedit and not-onreg.
			if($hidden){
				$onedit = 0;
				$onreg  = 0;
			}

			if($verbosity == 2){
				CLI::PrintActionStart($action . ' userconfig ' . $key);
			}

			$model = UserConfigModel::Construct($key);
			$isnew = !$model->exists();

			if($install){
				// Installations create/save it!

				// First, all the default and non-editable fields.
				$model->set('default_name', $name);
				if($default)  $model->set('default_value', $default);
				if($formtype) $model->set('formtype', $formtype);
				$model->set('default_onregistration', $onreg);
				$model->set('default_onedit', $onedit);
				$model->set('searchable', $searchable);
				$model->set('hidden', $hidden);
				if($options)  $model->set('options', $options);
				$model->set('validation', $validation);
				$model->set('required', $required);
				$model->set('default_weight', $weight);

				// And now the admin-editable fields.
				// These only get set if the configuration option does not exist prior.
				if($isnew || $hidden){
					$model->set('name', $name);
					$model->set('onregistration', $onreg);
					$model->set('onedit', $onedit);
					$model->set('weight', $weight);
				}

				if($default)  $model->set('default_value', $default);
				if($formtype) $model->set('formtype', $formtype);

				if($model->save()){
					if($isnew){
						$changes[] = 'Created user config [' . $model->get('key') . '] as a [' . $model->get('formtype') . ' input]';
					}
					else{
						$changes[] = 'Updated user config [' . $model->get('key') . '] as a [' . $model->get('formtype') . ' input]';
					}
					if($verbosity == 2){
						CLI::PrintActionStatus(true);
					}
				}
				else{
					if($verbosity == 2){
						CLI::PrintActionStatus('skip');
					}
				}
			}
			else{
				// Uninstallations remove user configuration keys.
				$model->delete();
				$changes[] = 'Removed user config [' . $key . ']';
				if($verbosity == 2){
					CLI::PrintActionStatus(true);
				}
			}
		}

		return (sizeof($changes)) ? $changes : false;

	} // private function _parseUserConfigs