Blame view

administrator/components/com_jce/models/fields/fonts.php 3.81 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
<?php

defined('JPATH_PLATFORM') or die;

class JFormFieldFonts extends JFormFieldCheckboxes
{
    /**
     * The form field type.
     *
     * @var string
     *
     * @since  11.1
     */
    protected $type = 'Fonts';

    /**
     * Name of the layout being used to render the field
     *
     * @var    string
     * @since  3.5
     */
    protected $layout = 'form.field.fonts';

    /**
     * Flag to tell the field to always be in multiple values mode.
     *
     * @var    boolean
     * @since  11.1
     */
    protected $forceMultiple = false;

    private static $fonts = array(
        'Andale Mono' => 'andale mono,times',
        'Arial' => 'arial,helvetica,sans-serif',
        'Arial Black' => 'arial black,avant garde',
        'Book Antiqua' => 'book antiqua,palatino',
        'Comic Sans MS' => 'comic sans ms,sans-serif',
        'Courier New' => 'courier new,courier',
        'Georgia' => 'georgia,palatino',
        'Helvetica' => 'helvetica',
        'Impact' => 'impact,chicago',
        'Symbol' => 'symbol',
        'Tahoma' => 'tahoma,arial,helvetica,sans-serif',
        'Terminal' => 'terminal,monaco',
        'Times New Roman' => 'times new roman,times',
        'Trebuchet MS' => 'trebuchet ms,geneva',
        'Verdana' => 'verdana,geneva',
        'Webdings' => 'webdings',
        'Wingdings' => 'wingdings,zapf dingbats',
    );

    /**
     * Allow to override renderer include paths in child fields
     *
     * @return  array
     *
     * @since   3.5
     */
    protected function getLayoutPaths()
    {
        return array(JPATH_ADMINISTRATOR . '/components/com_jce/layouts', JPATH_SITE . '/layouts');
    }

    protected function getOptions()
    {
        $fieldname = preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname);
        $options = array();

        if (is_string($this->value)) {
            $this->value = json_decode(htmlspecialchars_decode($this->value), true);
        }

        // cast to array
        $this->value = (array) $this->value;

        $fonts = array();

        // map associative array to array of key value pairs
        foreach ($this->value as $key => $value) {
            if (is_numeric($key) && is_array($value)) {
                $fonts = array_merge($fonts, $value);
            } else {                
                $fonts[] = array($key => $value);
            }
        }

        // assign emtpy (unchecked) options for unused fonts
        foreach(self::$fonts as $text => $value) {
            
            if (array_key_exists($text, $fonts)) {
                continue;
            }

            $tmp = array(
                'value' => $value,
                'text' => JText::alt($text, $fieldname),
                'checked' => false,
                'custom' => false,
            );

            $options[] = (object) $tmp;
        }

        foreach ($fonts as $text => $value) {
            $value = htmlspecialchars_decode($value, ENT_QUOTES);

            $tmp = array(
                'value' => $value,
                'text' => JText::alt($text, $fieldname),
                'checked' => true,
                'custom' => !in_array($value, array_values(self::$fonts)),
            );

            $options[] = (object) $tmp;
        }

        return $options;
    }

    /**
     * Method to determine if an array is an associative array.
     *
     * @param    array        An array to test
     *
     * @return bool True if the array is an associative array
     *
     * @link    https://www.php.net/manual/en/function.is-array.php#84488
     */
    private static function is_associative_array($array)
    {
        if (!is_array($array)) {
            return false;
        }

        $i = count($array);

        while ($i > 0) {
            if (!array_key_exists(--$i, $array)) {
                return true;
            }
        }

        return false;
    }
}