/**
  * Update all field values for a given $fieldId to match $fieldValues[]
  *
  * @param  int    $fieldId      Id of field
  * @param  array  $fieldValues  New or existing values: ordered array( array( 'fieldtitle' => 'Title of field', 'fieldlabel' => 'Label of field' ) )
  * @return boolean              Result
  */
 public function updateFieldValues($fieldId, array $fieldValues)
 {
     $existingFieldValues = $this->getFieldValuesOfField($fieldId);
     if ($fieldValues) {
         // Remove deleted field values:
         foreach ($existingFieldValues as $i => $existingFieldValue) {
             $i = (int) $i;
             $exists = false;
             foreach ($fieldValues as $fieldValue) {
                 $fieldValue = (array) $fieldValue;
                 $id = (int) cbGetParam($fieldValue, 'fieldvalueid');
                 //TODO: Use new Input class
                 $title = trim(stripslashes(cbGetParam($fieldValue, 'fieldtitle')));
                 if ($id && $i == $id && $title != '') {
                     $exists = true;
                     break;
                 }
             }
             if (!$exists) {
                 if (!$this->delete($i)) {
                     return false;
                 }
                 unset($existingFieldValues[$i]);
             }
         }
         // Insert new field values or update existing:
         foreach ($fieldValues as $i => $fieldValue) {
             $fieldValue = (array) $fieldValue;
             $id = (int) cbGetParam($fieldValue, 'fieldvalueid');
             //TODO: Use new Input class
             $title = trim(stripslashes(cbGetParam($fieldValue, 'fieldtitle')));
             $label = trim(stripslashes(cbGetParam($fieldValue, 'fieldlabel')));
             if ($title != '') {
                 if (isset($existingFieldValues[$id])) {
                     $newFieldValue = $existingFieldValues[$id];
                     if ((int) $newFieldValue->get('fieldid') == (int) $fieldId && $newFieldValue->get('fieldtitle') == $title && $newFieldValue->get('fieldlabel') == $label && (int) $newFieldValue->get('ordering') == (int) ($i + 1)) {
                         continue;
                     }
                 } else {
                     $newFieldValue = new FieldValueTable($this->_db);
                 }
                 $newFieldValue->set('fieldid', (int) $fieldId);
                 $newFieldValue->set('fieldtitle', $title);
                 $newFieldValue->set('fieldlabel', $label);
                 $newFieldValue->set('ordering', (int) ($i + 1));
                 if (!$newFieldValue->store()) {
                     return false;
                 }
             }
         }
         $this->updateOrder($this->_db->NameQuote('fieldid') . " = " . (int) $fieldId);
     } else {
         // Delete all current field values:
         $query = 'DELETE' . "\n FROM " . $this->_db->NameQuote($this->_tbl) . "\n WHERE " . $this->_db->NameQuote('fieldid') . " = " . (int) $fieldId;
         $this->_db->setQuery($query);
         if (!$this->_db->query()) {
             return false;
         }
     }
     return true;
 }
	/**
	 * adds field values array to xml data
	 * Used by Backend XML only
	 * @deprecated Do not use directly, only for XML tabs backend
	 *
	 * @param string             $value
	 * @param RegistryInterface  $pluginParams
	 * @param string             $name
	 * @param \SimpleXMLElement  $node
	 * @param string             $control_name
	 * @param string             $control_name_name
	 * @param boolean            $view
	 * @param RegistryInterface  $data
	 */
	public function fetchFieldValues( /** @noinspection PhpUnusedParameterInspection */ $value, $pluginParams, $name, $node, $control_name, $control_name_name, $view, $data ) {
		if ( $this->fieldid > 0 ) {
			$fieldValuesTable	=	new FieldValueTable( $this->_db );
			$fieldValues		=	$fieldValuesTable->getFieldValuesOfField( (int) $this->fieldid );
		} else {
			$fieldValues		=	array();
		}

		$data->set( '_fieldvalues', $fieldValues );
	}
 /**
  * Installs a field value
  *
  * @param  int               $fieldId
  * @param  SimpleXMLElement  $fieldValue
  * @return boolean                        True on success, False on failure
  */
 function installFieldValue($fieldId, $fieldValue)
 {
     global $_CB_database;
     $row = new FieldValueTable();
     $row->fieldid = (int) $fieldId;
     $row->fieldtitle = $fieldValue->attributes('title');
     $row->ordering = $fieldValue->attributes('ordering');
     $row->sys = $fieldValue->attributes('sys');
     $_CB_database->setQuery("SELECT fieldvalueid FROM #__comprofiler_field_values WHERE fieldid = " . (int) $fieldId . " AND fieldtitle = '" . $row->fieldtitle . "'");
     $fieldValueId = $_CB_database->loadResult();
     if ($fieldValueId) {
         $row->fieldvalueid = $fieldValueId;
     }
     try {
         $row->store();
     } catch (\RuntimeException $e) {
         $this->setError(1, 'SQL error on field store' . ': ' . htmlspecialchars($e->getMessage()));
         return false;
     }
     return true;
 }