Classes

File phpboost/member/extended-fields/ExtendedFieldsService.class.php

File phpboost/member/extended-fields/ExtendedFieldsService.class.php

  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: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 
<?php
/**
 * @package     PHPBoost
 * @subpackage  Member\extended-fields
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Kevin MASSY <reidlos@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2014 12 22
 * @since       PHPBoost 3.0 - 2010 08 14
*/

class ExtendedFieldsService
{
    private static $error;
    const SORT_BY_ID = 1;
    const SORT_BY_FIELD_NAME = 2;

    /**
     * Add a extended field
     * @param object $extended_field ExtendedField containing the name, field name, position, content, field type, possible values, default values, required and regex.
     */
    public static function add(ExtendedField $extended_field)
    {
        $name = $extended_field->get_name();
        $type_field = $extended_field->get_field_type();

        $exit_by_type = ExtendedFieldsDatabaseService::check_field_exist_by_type($extended_field);
        $class = $extended_field->get_instance();
        if ($exit_by_type && $class->get_field_used_once() || $class->get_field_used_phpboost_configuration() && $extended_field->get_is_not_installer())
        {
            self::set_error(LangLoader::get_message('extended-fields-error-phpboost-config', 'admin-user-common'));
        }
        else
        {
            if (!empty($name) && !empty($type_field))
            {
                if (!ExtendedFieldsDatabaseService::check_field_exist_by_field_name($extended_field))
                {
                    ExtendedFieldsDatabaseService::add_extended_field($extended_field);

                    ExtendedFieldsCache::invalidate();
                }
                else
                {
                    self::set_error(LangLoader::get_message('extended-fields-error-already-exist', 'admin-user-common'));
                }
            }
        }
    }

    /**
     * Update a extended field
     * @param object $extended_field ExtendedField containing the id, name, field name, content, field type, possible values, default values, required and regex.
     */
    public static function update(ExtendedField $extended_field)
    {
        $name = $extended_field->get_name();
        $type_field = $extended_field->get_field_type();
        if (!empty($name) && !empty($type_field))
        {
            if (ExtendedFieldsDatabaseService::check_field_exist_by_id($extended_field))
            {
                ExtendedFieldsDatabaseService::update_extended_field($extended_field);

                ExtendedFieldsCache::invalidate();
            }
        }
    }

    /**
     * Delete a extended field for id
     * @param integer $id the id of the extended field
     */
    public static function delete_by_id($id)
    {
        if (!empty($id))
        {
            $extended_field = new ExtendedField();
            $extended_field->set_id($id);

            if (ExtendedFieldsDatabaseService::check_field_exist_by_id($extended_field))
            {
                $data = self::data_field($extended_field, self::SORT_BY_ID);
                $class = $data->get_instance();

                if (!$class->get_field_used_phpboost_configuration() || !$data->get_is_freeze())
                {
                    ExtendedFieldsDatabaseService::delete_extended_field($data);
                    ExtendedFieldsCache::invalidate();
                }
            }
        }
    }

    /**
     * Delete a extended field for field name
     * @param string $field_name the field name of the extended field
     */
    public static function delete_by_field_name($field_name)
    {
        if (!empty($field_name))
        {
            $extended_field = new ExtendedField();
            $extended_field->set_field_name($field_name);

            if (ExtendedFieldsDatabaseService::check_field_exist_by_field_name($extended_field))
            {
                $data = self::data_field($extended_field, self::SORT_BY_FIELD_NAME);
                $class = $data->get_instance();

                if (!$class->get_field_used_phpboost_configuration() || !$data->get_is_freeze())
                {
                    ExtendedFieldsDatabaseService::delete_extended_field($data);
                    ExtendedFieldsCache::invalidate();
                }
            }
        }
    }

    /**
     * Return informations for extended field by id or field name
     * @param object ExtendedField containing the id or the field name
     * @param const $sort SORT_BY_ID or SORT_BY_FIELD_NAME
     * $return Object ExtendedField containing the informations field
     */
    public static function data_field(ExtendedField $extended_field, $sort = self::SORT_BY_ID)
    {
        $field_name = $extended_field->get_field_name();
        $id = $extended_field->get_id();
        if ($sort == self::SORT_BY_ID && $id > 0)
        {
            $data = ExtendedFieldsDatabaseService::select_data_field_by_id($extended_field);
        }
        else if ($sort == self::SORT_BY_FIELD_NAME && !empty($field_name))
        {
            $data = ExtendedFieldsDatabaseService::select_data_field_by_field_name($extended_field);
        }

        if (isset($data))
        {
            $extended_field->set_name($data['name']);
            $extended_field->set_field_name($data['field_name']);
            $extended_field->set_position($data['position']);
            $extended_field->set_description($data['description']);
            $extended_field->set_field_type($data['field_type']);
            $extended_field->set_possible_values($data['possible_values']);
            $extended_field->set_default_value($data['default_value']);
            $extended_field->set_is_required($data['required']);
            $extended_field->set_display($data['display']);
            $extended_field->set_regex($data['regex']);
            $extended_field->set_is_freeze($data['freeze']);
            $extended_field->set_authorization($data['auth']);
        }
        return $extended_field;

    }

    private static function set_error($error)
    {
        self::$error = $error;
    }

    /**
     * $return String Possible errors encountered
     */
    public static function get_error()
    {
        $error = self::$error;
        if (!empty($error))
        {
            return $error;
        }
    }
}
?>
Annonces