Your IP : 216.73.216.240


Current Path : /home/juvelize/www/libraries/syw/
Upload File :
Current File : /home/juvelize/www/libraries/syw/fields.php

<?php
/**
 * @copyright	Copyright (C) 2011 Simplify Your Web, Inc. All rights reserved.
 * @license		GNU General Public License version 3 or later; see LICENSE.txt
 */

// no direct access
defined('_JEXEC') or die;

class SYWFields
{
    /**
     * Cache for field parameters
     * @var array
     */
	protected static $fields = array();
	
	/**
	 * Cache for field categories
	 * @var array
	 */
	protected static $fields_categories = array();
    
    /**
     * Get the values of a custom field for a specific item
     *
     * @param string $item_id
     * @param string $field_id
     * @param boolean $include_params - deprecated
     * @return array or array of value arrays
     */
    public static function getCustomFieldValues($field_id, $item_id, $include_params = false, $force_multiple_array = false)
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        
        if ($include_params) {
            $query->select($db->quoteName(array('fv.value', 'f.label', 'f.name', 'f.params', 'f.fieldparams'), array('value', 'title', 'alias', 'fieldoptions', 'fieldparams')));
        } else {
            $query->select($db->quoteName('fv.value', 'value'));
        }
        
        $query->from($db->quoteName('#__fields_values', 'fv'));
        $query->where($db->quoteName('fv.field_id').' = ' . $field_id);
        $query->where($db->quoteName('fv.item_id').' = ' . $item_id);
        
        if ($include_params) {
            $query->join('LEFT', $db->quoteName('#__fields', 'f').' ON '.$db->quoteName('f.id').' = '.$db->quoteName('fv.field_id'));
        }
        
        $db->setQuery($query);
        
        $results = array();
        
        try {
            $results = $db->loadAssocList();
        } catch (RuntimeException $e) {
            return null;
        }
        
        if (!$force_multiple_array && count($results) == 1) {
            return $results[0];
        }
        
        return $results;
    }
    
    /**
     * Get the parameters of a custom field
     *
     * @param integer $field_id
     * @return array of parameters
     */
    public static function getCustomFieldParams($field_id)
    {
        if (isset(static::$fields[$field_id])) {
            return static::$fields[$field_id];
        }
        
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        
        $query->select($db->quoteName(array('f.label', 'f.name', 'f.params', 'f.fieldparams', 'f.context', 'f.type', 'f.default_value'), array('title', 'alias', 'fieldoptions', 'fieldparams', 'context', 'type', 'default_value')));
        
        $query->from($db->quoteName('#__fields', 'f'));
        $query->where($db->quoteName('f.id').' = ' . $field_id);
        
        $db->setQuery($query);
        
        $results = array();
        
        try {
            $results = $db->loadAssoc();
            static::$fields[$field_id] = $results;
        } catch (RuntimeException $e) {
            return null;
        }
        
        return $results;
    }
    
    /**
     * Check if a custom field is restricted to one or several categories
     * 
     * @param int $field_id
     * @return array categories the field is restricted to - an empty array means the field is available in ALL categories
     */
    public static function getAssignedCategories($field_id)
    {
    	if (isset(static::$fields_categories[$field_id])) {
    		return static::$fields_categories[$field_id];
    	}
    	
    	$db = JFactory::getDbo();
    	$query = $db->getQuery(true);
    	
    	$query->select($db->quoteName('category_id'));
    	$query->from($db->quoteName('#__fields_categories'));
    	$query->where('field_id = ' . $field_id);
    	
    	$db->setQuery($query);
    	
    	$results = array();
    	
    	try {
    		$results = $db->loadColumn();
    		static::$fields_categories[$field_id] = $results;
    	} catch (RuntimeException $e) {
    		//
    	}
    	
    	return $results;
    }
    
    /**
     * For a custom field that has a list of items, translates the options and returns the coma separated list of values ready for display
     *
     * @param object $field
     */
    public static function prepareCustomFieldValueFromOptions(&$field)
    {
        $field_params = json_decode($field->fieldparams);
        
        if (isset($field_params->options) && is_object($field_params->options)) {
            
            $options = array();
            
            foreach ($field_params->options as $key => $value) {
                $options[$value->value] = $value->name;
            }
            
            $cfield_values = array();
            
            if (!is_array($field->value)) {
                $field->value = array($field->value);
            }
            
            foreach ($field->value as $result) {
                if (!empty($options)) {
                    if (isset($options[$result]) && trim($options[$result]) !== '') {
                        if (JFactory::getLanguage()->hasKey($options[$result])) {
                            $cfield_values[] = JText::_($options[$result]);
                        } else {
                            $cfield_values[] = trim($options[$result]);
                        }
                    } else {
                        //$cfield_val[] = ''; // could happen, for instance 3 values then get down to 2
                    }
                } else {
                    if (trim($result) !== '') {
                        $cfield_values[] = trim($result);
                    }
                }
            }
            
            $field->value = implode(', ', $cfield_values);
        }
    }
    
    /**
     * Prepares the value of a custom field for display
     *
     * @param string $context
     * @param object $item
     * @param object $field
     */
    public static function prepareCustomFieldValue($context, $item, &$field)
    {
        JPluginHelper::importPlugin('fields');
        
        $dispatcher = JEventDispatcher::getInstance();
        
        // Event allow plugins to modify the output of the field before it is prepared
        $dispatcher->trigger('onCustomFieldsBeforePrepareField', array($context, $item, &$field));
        
        // Gathering the value for the field
        $prepared_value = $dispatcher->trigger('onCustomFieldsPrepareField', array($context, $item, &$field));
        
        if (is_array($prepared_value)) {
            $prepared_value = implode(' ', $prepared_value);
        }
        
        // Event allow plugins to modify the output of the prepared field
        $dispatcher->trigger('onCustomFieldsAfterPrepareField', array($context, $item, $field, &$prepared_value));
        
        $field->value = $prepared_value;
    }
    
}
?>