Einstieg in die Modul-Entwicklung

Aus Wiki | modified eCommerce Shopsoftware
Zur Navigation springenZur Suche springen


Es gibt zwei grundsächliche Arten wie Module aufgebaut werden. Diese beiden können sich auch je nach Modul vermischen. Du kannst unser "auto include system" verwenden oder einige PHP-Klassen erweitern. In beiden Fällen, verteilst du in der Verzeichnis-Struktur von modifed deine Dateien. Ein Verzeichnis indem dein Modul und alle dazugehörigen Dateien gebündelt liegen gibt es nicht. Das Modified System schaut in diese verteilten Verzeichnisse und lädt deinen Code aus deinen Dateien zwischen den Core-Programm Code. Das kannst du dir wie Hooks vorstellen.

<?php

/**
 * My Module
 */

class jay_my_module
{
    public string $code;
    public string $name;
    public string $title;
    public string $description;
    public int $sort_order;
    public bool $enabled;

    public function __construct()
    {
        $this->name        = strtoupper(self::class);
        $this->code        = self::class;
        $this->title       = $this->name . '_TITLE';
        $this->description = $this->name . '_DESC';
        $this->sort_order  = $this->name . '_SORT_ORDER';
        $this->enabled     = defined($this->name . '_STATUS') && 'True' === constant($this->name . '_STATUS');
    }

    /**
     * Returns whether the module is installed.
     *
     * @return int
     */
    public function check(): int
    {
        $query   = 'SELECT `configuration_value`
                      FROM `' . TABLE_CONFIGURATION . '`
                     WHERE `configuration_key` = "' . $this->name . '_STATUS"';
        $perform = xtc_db_query($query);
        $result  = xtc_db_fetch_array($perform);

        $is_installed = null !== $result;

        return $is_installed;
    }

    /**
     * Configuration keys used by the module. Used when installing and removing
     * the module.
     *
     * @return array
     */
    public function keys(): array
    {
        $keys = array(
            $this->name . '_VERSION',
            $this->name . '_STATUS',
        );

        return $keys;
    }

    /**
     * Actions performed when the user clicks the install button.
     *
     * @return void
     */
    public function install(): void
    {
        /** General */
        $query   = 'INSERT INTO `' . TABLE_CONFIGURATION . '` (
            `configuration_key`,
            `configuration_value`,
            `configuration_group_id`,
            `date_added`,
            `set_function`
        )
        VALUES
        (
            "' . $this->name . '_VERSION",
            "' . self::VERSION . '",
            6,
            NOW(),
            "' . self::class . '->configurationFieldVersion"
        ),
        (
            "' . $this->name . '_STATUS",
            "True",
            6,
            NOW(),
            "xtc_cfg_select_option(array(\'True\', \'False\'),"
        )';
        $perform = xtc_db_query($query);
    }

    public function update()
    {
    }

    /**
     * Actions performed when the user clicks the uninstall button.
     *
     * @return void
     */
    public function remove(): void
    {
        /** General */
        $keys = '"' . implode('", "', $this->keys()) . '"';

        $query   = 'DELETE FROM `' . TABLE_CONFIGURATION . '`
                          WHERE `configuration_key` IN (' . $keys . ')';
        $perform = xtc_db_query($query);
    }

    public function process()
    {
    }

    /**
     * Additional HTML to show during module configuration.
     *
     * @return array
     */
    public function display(): array
    {
        $rows = array(
            'text' => implode(
                ' ',
                array(
                    xtc_button(BUTTON_SAVE),
                    xtc_button_link(
                        BUTTON_CANCEL,
                        xtc_href_link(
                            FILENAME_MODULE_EXPORT,
                            'set=' . filter_input(INPUT_GET, 'set') . '&module=' . $this->code
                        )
                    ),
                )
            )
        );

        return $rows;
    }

    /**
     * Action to perform when the configuration key "_VERSION" is being
     * displayed.
     *
     * @param string $value
     * @param string $constant
     *
     * @return string
     */
    public function configurationFieldVersion(string $value, string $constant): string
    {
        return xtc_draw_input_field(
            'configuration[' . $constant . ']',
            $value,
            'readonly="true" style="opacity: 0.4;"'
        );
    }
}