Ejemplo n.º 1
0
	/**
	 * Parse list options for options, optgroups, and evaluate IF conditions
	 *
	 * @param  string              $name          The name of the form element
	 * @param  SimpleXMLElement    $node          The xml element for the parameter
	 * @param  string              $control_name  The control name
	 * @param  array               $options       The base array of options to extend with parsed options
	 * @param  SimpleXMLElement[]  $children      The child xml elements for the parameter
	 * @param  bool                $otgroups      If optgroups should be added to the options array
	 * @param  string              $value         The value of the element
	 * @param  bool                $ignoreClass   If option classes should be ignored or not
	 * @param  int                 $index         The option index
	 */
	function _list_options( $name, &$node, $control_name, &$options, $children, $otgroups = true, $value = null, $ignoreClass = false, &$index = 0 ) {
		$translate												=	$node->attributes( 'translate' );

		if ( $children ) foreach ( $children as $option ) {
			$optTranslate										=	( $option->attributes( 'translate' ) !== null ? $option->attributes( 'translate' ) : $translate );

			if ( $option->getName() == 'option' ) {
				if ( ( $option->attributes( 'index' ) !== '' ) && ( $option->attributes( 'index' ) !== null ) ) {
					$val										=	$option->attributes( 'index' );
				} else {
					$val										=	$option->attributes( 'value' );
				}

				if ( ( $option->attributes( 'selectable' ) != 'false' ) || ( (string) $val === (string) $value ) ) {
					$label										=	$option->data();
					$opt										=	$this->_list_make_option( $optTranslate, $val, ( $label !== '' ? $label : $val ), 'value', 'text', null, ( $ignoreClass ? null : RegistryEditView::buildClasses( $option ) ) );
					$opt->id									=	$this->control_id( $control_name, $name ) . '__cbf' . $index;

					$options[]									=	$opt;

					$index++;
				}
			} elseif ( $otgroups && ( $option->getName() == 'optgroup' ) ) {
				if ( $optTranslate == 'no' ) {
					$label										=	$option->attributes( 'label' );
				} else {
					$label										=	CBTxt::T( $option->attributes( 'label' ) );
				}

				$opt											=	moscomprofilerHTML::makeOptGroup( $label, 'value', 'text', null, ( $ignoreClass ? null : RegistryEditView::buildClasses( $option ) ) );
				$opt->id										=	$this->control_id( $control_name, $name ) . '__cbf' . $index;

				$options[]										=	$opt;

				$index++;

				$this->_list_options( $name, $node, $control_name, $options, $option->children(), $otgroups, $value, $ignoreClass, $index );

				$options[]										=	moscomprofilerHTML::makeOptGroup( null );
			} elseif ( $option->getName() == 'data' ) {
				// TODO: Replace this usage with _getFieldValues (aslo needs upgrading to support private/custom below) once it is refactored into RegistryEditView:
				global $_CB_database;

				if ( $option->attributes( 'type' ) == 'private' ) {
					$privateOptions								=	$this->_form_private( $name, $value, $option, $control_name );

					if ( is_array( $privateOptions ) ) {
						$options								=	array_merge( $options, $privateOptions );
					}
				} elseif ( $option->attributes( 'type' ) == 'custom' ) {
					$customOptions								=	$this->_form_custom( $name, $value, $option, $control_name );

					if ( is_array( $customOptions ) ) {
						$options								=	array_merge( $options, $customOptions );
					}
				} elseif ( $option->attributes( 'dataprocessed' ) != 'true' ) {
					$dataTable									=	$option->attributes( 'table' );

					$xmlsql										=	new XmlQuery( $_CB_database, $dataTable, $this->_pluginParams );

					$xmlsql->setExternalDataTypeValues( 'modelofdata', $this->_modelOfData[0] );
					$xmlsql->process_orderby( $option->getElementByPath( 'orderby') );								// <data><orderby><field> fields
					$xmlsql->process_fields( $option->getElementByPath( 'rows') );									// <data><rows><field> fields
					$xmlsql->process_where( $option->getElementByPath( 'where') );									// <data><where><column> fields

					$groupby									=	$option->getElementByPath( 'groupby' );

					$xmlsql->process_groupby( ( $groupby !== false ? $groupby : 'value' ) );									// <data><groupby><field> fields

					$fieldValuesInDb							=	$xmlsql->queryLoadObjectsList( $option );	// get the records

					$rows										=	$option->getElementByPath( 'rows');			// check for type="firstwords"

					/** @var $rows SimpleXMLElement|null */
					if ( $rows ) {
						$textField								=	$rows->getChildByNameAttr( 'field', 'as', 'text' );

						/** @var $textField SimpleXMLElement|null */
						if ( $textField ) {
							if ( $textField->attributes( 'type' ) == 'firstwords' ) {
								$size							=	$textField->attributes( 'size' );

								if ( ! $size ) {
									$size						=	45;
								}

								foreach ( array_keys( $fieldValuesInDb ) as $k ) {
									$strippedContent			=	trim( $fieldValuesInDb[$k]->text );

									if ( cbIsoUtf_strlen( $strippedContent ) > $size ) {
										$strippedContent		=	cbIsoUtf_substr( $strippedContent, 0, $size ) . '...';
									}

									$fieldValuesInDb[$k]->text	=	$strippedContent;
								}
							}
						}
					}

					if ( $fieldValuesInDb ) {
						foreach ( array_keys( $fieldValuesInDb ) as $k ) {
							$dbOptTranslate						=	( isset( $fieldValuesInDb[$k]->translate ) ? $fieldValuesInDb[$k]->translate : $optTranslate );

							$options[]							=	$this->_list_make_option( $dbOptTranslate, $fieldValuesInDb[$k]->value, ( $fieldValuesInDb[$k]->text !== '' ? $fieldValuesInDb[$k]->text : $fieldValuesInDb[$k]->value ) );
						}
					}
				}
			} elseif ( $option->getName() == 'if' ) {
				if ( $option->attributes( 'type' ) == 'showhide' ) {
					$ifName										=	( $this->_htmlId( $control_name, $option ) . $option->attributes( 'operator' ) . $option->attributes( 'value' ) . $option->attributes( 'valuetype' ) );

					$this->_jsif[$ifName]['element']			=	$option;
					$this->_jsif[$ifName]['control_name']		=	$control_name;
					$this->_jsif[$ifName]['ifname']				=	$this->_htmlId( $control_name, $option );

					$ifOptions									=	array();

					$this->_list_options( $name, $node, $control_name, $ifOptions, $option->children(), $otgroups, $value, $ignoreClass, $index );

					if ( $ifOptions ) foreach ( $ifOptions as $ifOption ) {
						if ( isset( $ifOption->id ) ) {
							$this->_jsif[$ifName]['show'][]		=	$ifOption->id;
						}
					}

					$options									=	array_merge( $options, $ifOptions );
				} else {
					if ( $option->attributes( 'type' ) == 'permission' ) {
						$showInside								=	Access::authorised( $option );
					} else {
						$showInside								=	$this->_evalIf( $option );
					}

					if ( $showInside ) {
						$then									=	$option->getChildByNameAttributes( 'then' );

						if ( $then ) {
							$insideParamToRender				=	$then;
						} else {
							$insideParamToRender				=	$option;
						}
					} else {
						$insideParamToRender					=	$option->getChildByNameAttributes( 'else' );
					}

					if ( $insideParamToRender ) {
						$this->_list_options( $name, $node, $control_name, $options, $insideParamToRender->children(), $otgroups, $value, $ignoreClass, $index );
					}
				}
			}
		}
	}
 /**
  * View for <param  type="private" class="cbpaidParamsExt" method="datalist">...
  *
  * @param  string              $value                  Stored Data of Model Value associated with the element
  * @param  ParamsInterface     $pluginParams           Main settigns parameters of the plugin
  * @param  string              $name                   Name attribute
  * @param  CBSimpleXMLElement  $param                  This XML node
  * @param  string              $control_name           Name of the control
  * @param  string              $control_name_name      css id-encode of the names of the controls surrounding this node
  * @param  boolean             $view                   TRUE: view, FALSE: edit
  * @param  cbpaidTable         $modelOfData            Data of the Model corresponding to this View
  * @param  cbpaidTable[]       $modelOfDataRows        Displayed Rows if it is a table
  * @param  int                 $modelOfDataRowsNumber  Total Number of rows
  * @return null|string
  */
 public function datalist($value, &$pluginParams, $name, &$param, $control_name, $control_name_name, $view, &$modelOfData, &$modelOfDataRows, &$modelOfDataRowsNumber)
 {
     global $_CB_database;
     //TBD	$multi					=	( $param->attributes( 'multiple' ) == 'true' );
     $data = $param->getElementByPath('data');
     if ($data) {
         $dataTable = $data->attributes('table');
         if (!$dataTable) {
             if (isset($this->table)) {
                 $dataTable = $this->table;
             } elseif (is_object($modelOfData) && $modelOfData instanceof TableInterface) {
                 $dataTable = $modelOfData->getTableName();
             } elseif (is_object($modelOfData) && isset($modelOfData->_tbl)) {
                 $dataTable = $modelOfData->_tbl;
             } else {
                 $dataTable = null;
             }
         }
         $xmlsql = new XmlQuery($_CB_database, $dataTable, $pluginParams);
         $xmlsql->setExternalDataTypeValues('modelofdata', $modelOfData);
         $xmlsql->process_orderby($data->getElementByPath('orderby'));
         // <data><orderby><field> fields
         $xmlsql->process_fields($data->getElementByPath('rows'));
         // <data><rows><field> fields
         $xmlsql->process_where($data->getElementByPath('where'));
         // <data><where><column> fields
         $groupby = $data->getElementByPath('groupby');
         if (!$groupby) {
             $groupby = 'value';
         }
         if ($data->attributes('dogroupby') != 'false') {
             $xmlsql->process_groupby($groupby);
         }
         $fieldValuesInDb = $xmlsql->queryLoadObjectsList($data);
         // get the records
         if ($view) {
             if (is_array($fieldValuesInDb)) {
                 foreach ($fieldValuesInDb as $v) {
                     if ($v->value == $value) {
                         $value = $v->text;
                         break;
                     }
                 }
             }
             return htmlspecialchars($value);
         } else {
             // check if value is in possible values:
             if ($value != $param->attributes('default') && is_array($fieldValuesInDb)) {
                 $setToDefault = true;
                 foreach ($fieldValuesInDb as $v) {
                     if ($v->value == $value) {
                         $setToDefault = false;
                         break;
                     }
                 }
                 if ($setToDefault) {
                     $value = $param->attributes('default');
                 }
             }
             if ($param->attributes('blanktext') && ($param->attributes('hideblanktext') != 'true' || $value == $param->attributes('default'))) {
                 $default = (string) $param->attributes('default');
                 array_unshift($fieldValuesInDb, moscomprofilerHTML::makeOption($default, CBPTXT::T($param->attributes('blanktext'))));
             }
             //TBD	$selected			=	explode( '|*|', $value );
             $classes = 'class="' . RegistryEditView::buildClasses($param, array('form-control')) . '"';
             return moscomprofilerHTML::selectList($fieldValuesInDb, $control_name_name, $classes . $this->_title($param), 'value', 'text', $value, 2);
             // return $this->selectList( $fieldValuesInDb, $param, $control_name, $name, $selected, $multi );
         }
     }
     return null;
 }
Ejemplo n.º 3
0
    /**
     * Generic function to get an array of option values for lists, radios, checkboxes params and filter fields:
     *
     * @param  SimpleXMLElement  $o
     * @param  string              $basetype   RETURNED: base type
     * @param  string              $valueType  RETURNED: valuetype type
     * @return array|null
     */
    protected function _getFieldValues(&$o, &$basetype, &$valueType)
    {
        $valueType = $o->attributes('valuetype');
        $fieldValuesInDb = null;
        $this->registryEditVew->resolveXmlParamType($o);
        if ($o->attributes('base')) {
            $basetype = $o->attributes('base');
        } else {
            $basetype = $o->attributes('type');
        }
        switch ($o->attributes('type')) {
            case 'data':
                $data = $o->getElementByPath('data');
                if ($data) {
                    $dataTable = $data->attributes('table');
                    if (!$dataTable) {
                        $dataTable = $this->table;
                    }
                    $xmlsql = new XmlQuery($this->_db, $dataTable, $this->_pluginParams);
                    $xmlsql->process_orderby($data->getElementByPath('orderby'));
                    // <data><orderby><field> fields
                    $xmlsql->process_fields($data->getElementByPath('rows'));
                    // <data><rows><field> fields
                    $xmlsql->process_where($data->getElementByPath('where'));
                    // <data><where><column> fields
                    $groupby = $data->getElementByPath('groupby');
                    $xmlsql->process_groupby($groupby ? $groupby : 'value');
                    // <data><groupby><field> fields
                    $fieldValuesInDb = $xmlsql->queryLoadObjectsList($data);
                    // get the records
                    // check for type="firstwords":
                    $rows = $data->getElementByPath('rows');
                    /** @var $rows SimpleXMLElement|null */
                    if ($rows) {
                        $textField = $rows->getChildByNameAttr('field', 'as', 'text');
                        /** @var $textField SimpleXMLElement|null */
                        if ($textField) {
                            if ($textField->attributes('type') == 'firstwords') {
                                $size = $textField->attributes('size');
                                if (!$size) {
                                    $size = 45;
                                }
                                foreach (array_keys($fieldValuesInDb) as $k) {
                                    $strippedContent = trim($fieldValuesInDb[$k]->text);
                                    if (cbIsoUtf_strlen($strippedContent) > $size) {
                                        $strippedContent = cbIsoUtf_substr($strippedContent, 0, $size) . '...';
                                    }
                                    $fieldValuesInDb[$k]->text = $strippedContent;
                                }
                            }
                        }
                    }
                    $data->addAttribute('dataprocessed', 'true');
                } else {
                    // echo 'filter type is data but no child data present !';
                    $fieldName = $o->attributes('name');
                    if ($o->attributes('value')) {
                        $valueFieldName = $o->attributes('value');
                    } else {
                        $valueFieldName = $fieldName;
                    }
                    $dataTable = $o->attributes('table');
                    if (!$dataTable) {
                        $dataTable = $this->table;
                    }
                    $data = new SimpleXMLElement(<<<EOT
<?xml version="1.0" encoding="UTF-8"?>
<table table="{$dataTable}">
\t<rows>
\t\t<field name="{$valueFieldName}" as="value" type="sql:field" />
\t\t<field name="{$fieldName}" as="text" type="sql:field" />
\t</rows>
\t<orderby>
\t\t<field name="{$fieldName}" ordering="ASC" />
\t</orderby>
\t<groupby>
\t\t<field name="{$fieldName}" />
\t</groupby>
</table>
EOT
);
                    $xmlsql = new XmlQuery($this->_db, $dataTable, $this->_pluginParams);
                    $xmlsql->process_orderby($data->getElementByPath('orderby'));
                    // <data><orderby><field> fields
                    $xmlsql->process_fields($data->getElementByPath('rows'));
                    // <data><rows><field> fields
                    $xmlsql->process_where($data->getElementByPath('where'));
                    // <data><where><column> fields
                    $groupby = $data->getElementByPath('groupby');
                    $xmlsql->process_groupby($groupby ? $groupby : 'value');
                    // <data><groupby><field> fields
                    $fieldValuesInDb = $xmlsql->queryLoadObjectsList($data);
                    // get the records
                }
                break;
            case 'field_show_only_if_selected':
                break;
            case 'list':
            case 'radio':
            case 'checkbox':
            case 'checkmark':
            case 'published':
            case 'usergroup':
            case 'viewaccesslevel':
            case 'tag':
                foreach ($o->children() as $option) {
                    /** @var $option SimpleXMLElement */
                    if ($option->getName() == 'option') {
                        $hasIndex = $option->attributes('index') !== '' && $option->attributes('index') !== null;
                        $selObj = new \stdClass();
                        $selObj->value = $hasIndex ? $option->attributes('index') : $option->attributes('value');
                        if ($hasIndex) {
                            $selObj->internalvalue = $option->attributes('value');
                        }
                        $selObj->operator = $option->attributes('operator');
                        $selObj->text = $option->data();
                        $fieldValuesInDb[] = $selObj;
                    }
                }
                break;
            case 'field':
                global $_CB_database;
                $where = array();
                $where[] = "f." . $_CB_database->NameQuote('published') . " = 1";
                $where[] = "f." . $_CB_database->NameQuote('name') . " != " . $_CB_database->Quote('NA');
                $query = "SELECT f." . $_CB_database->NameQuote('fieldid') . " AS value" . ", f." . $_CB_database->NameQuote('name') . ' AS ' . $_CB_database->NameQuote('index') . ", f." . $_CB_database->NameQuote('title') . ' AS ' . $_CB_database->NameQuote('text') . ", f." . $_CB_database->NameQuote('table') . ' AS ' . $_CB_database->NameQuote('table') . ", " . $_CB_database->Quote('id') . ' AS ' . $_CB_database->NameQuote('table_key') . ", " . $_CB_database->Quote('=') . " AS operator" . "\n FROM " . $_CB_database->NameQuote('#__comprofiler_fields') . " AS f" . "\n LEFT JOIN " . $_CB_database->NameQuote('#__comprofiler_tabs') . " AS t" . " ON t." . $_CB_database->NameQuote('tabid') . " = f." . $_CB_database->NameQuote('tabid') . "\n WHERE " . implode("\n AND ", $where) . "\n ORDER BY t." . $_CB_database->NameQuote('position') . ", t." . $_CB_database->NameQuote('ordering') . ", f." . $_CB_database->NameQuote('ordering');
                $_CB_database->setQuery($query);
                $fieldValuesInDb = $_CB_database->loadObjectList();
                break;
            default:
                if (substr($o->attributes('type'), 0, 4) == 'sql:') {
                    // get list for dropdown filter
                    $fieldName = $o->attributes('name');
                    if ($o->attributes('value')) {
                        $valueFieldName = $o->attributes('value');
                    } else {
                        $valueFieldName = $fieldName;
                    }
                    $dataTable = $o->attributes('table');
                    if (!$dataTable) {
                        $dataTable = $this->table;
                    }
                    $data = new SimpleXMLElement(<<<EOT
<?xml version="1.0" encoding="UTF-8"?>
<table table="{$dataTable}">
\t<rows>
\t\t<field name="{$valueFieldName}" as="value" type="sql:field" />
\t\t<field name="{$fieldName}" as="text" type="sql:field" />
\t</rows>
\t<orderby>
\t\t<field name="{$fieldName}" ordering="ASC" />
\t</orderby>
\t<groupby>
\t\t<field name="{$fieldName}" />
\t</groupby>
</table>
EOT
);
                    $xmlsql = new XmlQuery($this->_db, $dataTable, $this->_pluginParams);
                    $xmlsql->process_orderby($data->getElementByPath('orderby'));
                    // <data><orderby><field> fields
                    $xmlsql->process_fields($data->getElementByPath('rows'));
                    // <data><rows><field> fields
                    $xmlsql->process_where($data->getElementByPath('where'));
                    // <data><where><column> fields
                    $groupby = $data->getElementByPath('groupby');
                    $xmlsql->process_groupby($groupby ? $groupby : 'value');
                    // <data><groupby><field> fields
                    $fieldValuesInDb = $xmlsql->queryLoadObjectsList($data);
                    // get the records
                    $o->addAttribute('type', 'list');
                    /*
                    					$fieldName	= $this->_db->getEscaped( $o->attributes( 'name' ) );
                    					if ( $o->attributes( 'value' ) ) {
                    						$valueFieldName		=	$this->_db->getEscaped( $o->attributes( 'value' ) );
                    					} else {
                    						$valueFieldName		=	$fieldName;
                    					}
                    					$tableName				=	$this->_db->getEscaped( $this->table );
                    					$query = "SELECT `" . $valueFieldName . "` AS value, `" . $fieldName . "` AS text"
                    					. "\n FROM `" . $tableName . "`"
                    					. "\n GROUP BY " . $fieldName
                    					. "\n ORDER BY " . $fieldName
                    					;
                    					$this->_db->setQuery( $query );
                    					$fieldValuesInDb = $this->_db->loadObjectList();
                    */
                }
                break;
        }
        return $fieldValuesInDb;
    }