PHP code example of arkonsoft / ps-module-core

1. Go to this page and download the library: Download arkonsoft/ps-module-core library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

arkonsoft / ps-module-core example snippets



if (!defined('_PS_VERSION_')) {
    exit;
}

use Arkonsoft\PsModule\Core\Module\AbstractModule;

class MyModule extends AbstractModule
{
    public function __construct()
    {
        $this->name = 'mymodule';

        // ps-module-core
        $this->tab = ModuleCategory::FRONT_OFFICE_FEATURES;
        $this->version = '1.0.0';
        $this->author = 'Firstname Lastname';
        $this->need_instance = 0;
        $this->ps_versions_compliancy = [
            'min' => '1.7.0.0',
            'max' => '8.99.99',
        ];
        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->trans('My module', [], 'Modules.Mymodule.Admin');
        $this->description = $this->trans('Description of my module.', [], 'Modules.Mymodule.Admin');

        $this->confirmUninstall = $this->trans('Are you sure you want to uninstall?', [], 'Modules.Mymodule.Admin');

        // ps-module-core
        if ($this->canBeUpgraded()) {
            $this->warning = $this->trans('The module %s needs to be updated. Use the "update" option in the list of modules. The module without an update may not work properly.', [ $this->name ], 'Modules.Mymodule.Admin');
        }
    }
}



use Arkonsoft\PsModule\Core\Controller\AbstractAdminSettingsController;

if (!defined('_PS_VERSION_')) {
    exit;
}

class AdminMyModuleSettingsController extends AbstractAdminSettingsController
{
    public function prepareOptions(): void
    {
        $form = [
            'form' => [
                'tabs' => [
                    'general' => $this->module->getTranslator()->trans('General', [], 'Modules.MyModule.Admin'),
                    'other1' => $this->module->getTranslator()->trans('Other 1', [], 'Modules.MyModule.Admin'),
                    'other2' => $this->module->getTranslator()->trans('Other 2', [], 'Modules.MyModule.Admin'),
                ],
                'legend' => [
                    'title' => $this->module->getTranslator()->trans('Settings', [], 'Modules.MyModule.Admin'),
                    'icon' => 'icon-cogs'
                ],
                'submit' => [
                    'title' => $this->module->getTranslator()->trans('Save', [], 'Modules.MyModule.Admin'),
                    'class' => 'btn btn-default pull-right'
                ],
            ],
        ];

        $form['form']['input'][] = [
            'label' => $this->module->getTranslator()->trans('Example field 1', [], 'Modules.MyModule.Admin'),
            'type' => 'text',
            'name' => $this->module->name . 'example_field_1',
            'lang' => true,
            'tab' => 'general'
        ];

        $form['form']['input'][] = [
            'label' => $this->module->getTranslator()->trans('Example field 2', [], 'Modules.MyModule.Admin'),
            'type' => 'text',
            'name' => $this->module->name . 'example_field_2',
            'lang' => true,
            'tab' => 'general'
        ];

        $this->forms[] = $form;
    }
}


/**
 * Returns the fully qualified class name of the ObjectModel
 * 
 * @return string
 */
abstract public function getObjectModelClassName(): string;

/**
 * Determines if a shop context is istColumns(): array;

/**
 * Defines the form fields for creating/editing objects
 * 
 * @return array
 */
abstract public function getFormFields(): array;



use Arkonsoft\PsModule\Core\Controller\AbstractAdminObjectModelController;

if (!defined('_PS_VERSION_')) {
    exit;
}

class AdminMyModuleItemsController extends AbstractAdminObjectModelController
{
    public function getObjectModelClassName(): string
    {
        return MyModuleItem::class;
    }

    public function isShopContextRequired(): bool
    {
        return true;
    }

    public function getListColumns(): array
    {
        return [
            'id_my_module_item' => [
                'title' => $this->module->getTranslator()->trans('ID', [], 'Modules.MyModule.Admin'),
                'width' => 'auto',
                'type' => 'text',
            ],
            'name' => [
                'title' => $this->module->getTranslator()->trans('Name', [], 'Modules.MyModule.Admin'),
                'width' => 'auto',
                'type' => 'text',
            ],
            'position' => [
                'title' => $this->module->getTranslator()->trans('Position', [], 'Modules.MyModule.Admin'),
                'width' => 'auto',
                'position' => 'position',
                'align' => 'center',
            ],
        ];
    }

    public function getFormFields(): array
    {
        return [
            'legend' => [
                'title' => $this->module->getTranslator()->trans('Item', [], 'Modules.MyModule.Admin'),
            ],
            'input' => [
                [
                    'type' => 'text',
                    'label' => $this->module->getTranslator()->trans('Name', [], 'Modules.MyModule.Admin'),
                    'name' => 'name',
                    '

protected function getImagePreviewHtml(string $type, string $extension, string $widthInPx = '200px'): string

[
    'type' => 'file',
    'label' => $this->module->getTranslator()->trans('Image', [], 'Modules.MyModule.Admin'),
    'name' => 'image',
    'display_image' => true,
    'image' => $this->getImagePreviewHtml('default', 'jpg'),
    'delete_url' => $this->getDeleteImageUrl('default'),
]

private function processImagesSave(): void
{
    if (!\Tools::isSubmit('submitAdd' . $this->table)) {
        return;
    }

    $object = $this->loadObject();

    if (!$object) {
        return;
    }

    $id = (int) $object->id;

    // Save desktop image with jpg and webp formats
    $this->objectModelImageManager->saveImage('image', $id, 'default', ['jpg', 'webp']);
}

public function postProcess()
{
    parent::postProcess();

    $this->processImagesSave();
}

'position' => [
    'title' => $this->module->getTranslator()->trans('Position', [], 'Modules.MyModule.Admin'),
    'width' => 'auto',
    'position' => 'position',
    'align' => 'center',
]

/**
 * Returns the database version of the module
 * 
 * @return ?string
 */
protected function getDatabaseVersion()

/**
 * Checks if the database version is lower than the specified version for potential upgrade.
 * 
 * @return bool
 */
protected function canBeUpgraded(): bool

public function __construct()
    {
        // other code

        if ($this->canBeUpgraded()) {
            $this->warning = $this->trans('The module %s needs to be updated. Use the "update" option in the list of modules. The module without an update may not work properly.', [ $this->name ], 'Modules.Mymodule.Admin');
        }
    }

public function __construct()
    {
        // other code

        $this->tab = ModuleCategory::FRONT_OFFICE_FEATURES;
    }

public function installTab(): bool
    {
        if (Tab::getIdFromClassName($this->settingsAdminController)) {
            return true;
        }

        $tab = new Tab();

        $parentTabClassName = TabDictionary::CATALOG;

        // other code
    }

public function __construct()

abstract public function prepareOptions();

public function initContent()

public function postProcess()

public function dispatchAction()

public function renderForm()

public function loadFormValues(HelperForm $helper)

public function saveForm()