Magento 2 : Admin Module Part 2

Updated . Posted . Visible to the public.

5.Create Model and Resource model

Address model class extends the AddressInterface and it contains the data object getters setter methods.

<?php
/**
 * Address
 *
 * @copyright Copyright © 2021 Vasan. All rights reserved.
 * @author    survasp@gmail.com
 */

namespace Vasan\KpsAddress\Model;


use Vasan\KpsAddress\Api\Data\AddressInterface;
use Magento\Framework\Model\AbstractModel;
use Vasan\KpsAddress\Model\ResourceModel\Address as ResourceModel;

class Address extends AbstractModel implements AddressInterface
{
    /**
     * @var string
     */
    protected $_eventPrefix = 'kps_address';

    /**
     * @var string
     */
    protected $_eventObject = 'kps_address';

    /**
     * @return void
     */
    protected function _construct()
    {
        parent::_construct();
        $this->_init(ResourceModel::class);
    }


    /**
     * @inheridoc
     */
    public function getProvince()
    {
        return $this->getData(self::KEY_PROVINCE);
    }

    /**
     * @inheridoc
     */
    public function getProvinceCode()
    {
        return $this->getData(self::KEY_PROVINCE_CODE);
    }

    /**
     * @inheridoc
     */
    public function getCity()
    {
        return $this->getData(self::KEY_CITY);
    }

    /**
     * @inheridoc
     */
    public function getCityCode()
    {
        return $this->getData(self::KEY_CITY_CODE);
    }

    /**
     * @inheridoc
     */
    public function getUrban()
    {
        return $this->getData(self::KEY_URBAN);
    }

    /**
     * @inheridoc
     */
    public function getUrbanCode()
    {
        return $this->getData(self::KEY_URBAN_CODE);
    }

    /**
     * @inheridoc
     */
    public function getSubDistrict()
    {
        return $this->getData(self::KEY_SUB_DISTRICT);
    }

    /**
     * @inheridoc
     */
    public function getZipcode()
    {
        return $this->getData(self::KEY_ZIPCODE);
    }

    /**
     * @return string
     */
    public function getUpdatedAt()
    {
        return $this->getData(self::KEY_UPDATED_AT);
    }

    /**
     * @return string
     */
    public function getCreatedAt()
    {
        return $this->getData(self::KEY_CREATED_AT);
    }

    /**
     * @inheridoc
     */
    public function getSubDistrictCode()
    {
        $this->getData(self::KEY_SUB_DISTRICT_CODE);
    }

    /**
     * @inheridoc
     */
    public function setProvince($province)
    {
        $this->setData(self::KEY_PROVINCE, $province);
    }

    /**
     * @inheridoc
     */
    public function setProvinceCode($provinceCode)
    {
        $this->setData(self::KEY_PROVINCE_CODE, $provinceCode);
    }

    /**
     * @inheridoc
     */
    public function setCity($city)
    {
        $this->setData(self::KEY_CITY, $city);
    }

    /**
     * @inheridoc
     */
    public function setCityCode($cityCode)
    {
        $this->setData(self::KEY_CITY_CODE, $cityCode);
    }

    /**
     * @inheridoc
     */
    public function setUrban($urban)
    {
        $this->setData(self::KEY_URBAN, $urban);
    }

    /**
     * @inheridoc
     */
    public function setUrbanCode($urbanCode)
    {
        $this->setData(self::KEY_URBAN_CODE, $urbanCode);
    }

    /**
     * @inheridoc
     */
    public function setSubDistrict($subDistrict)
    {
        $this->setData(self::KEY_SUB_DISTRICT, $subDistrict);
    }

    /**
     * @inheridoc
     */
    public function setSubDistrictCode($subDistrictCode)
    {
        $this->setData(self::KEY_SUB_DISTRICT_CODE, $subDistrictCode);
    }

    /**
     * @inheridoc
     */
    public function setZipcode($zipcode)
    {
        $this->setData(self::KEY_ZIPCODE, $zipcode);
    }
}

Address is class of resource model

<?php
/**
 * Address
 *
 * @copyright Copyright © 2021 Vasan. All rights reserved.
 * @author    survasp@gmail.com
 */

namespace Vasan\KpsAddress\Model\ResourceModel;


use Vasan\KpsAddress\Api\Data\AddressInterface;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class Address extends AbstractDb
{

    protected function _construct()
    {
        $this->_init('kps_address', AddressInterface::KEY_ID);
    }
}

Collection class of resource model

<?php
/**
 * Collection
 *
 * @copyright Copyright © 2021 Vasan. All rights reserved.
 * @author    survasp@gmail.com
 */

namespace Vasan\KpsAddress\Model\ResourceModel\Address;


use Vasan\KpsAddress\Model\Address;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Vasan\KpsAddress\Model\ResourceModel\Address as ResourceModel;
use Vasan\KpsAddress\Model\Address as Model;

class Collection extends AbstractCollection
{
    /**
     * @var string
     */
    protected $_idFieldName = Address::KEY_ID;

    /**
     * @return void
     */
    protected function _construct()
    {
        $this->_init(Model::class, ResourceModel::class);
    }
}
<?php
/**
 * AddressRepository
 *
 * @copyright Copyright © 2021 Vasan. All rights reserved.
 * @author    survasp@gmail.com
 */

namespace Vasan\DmsAddress\Model\Repository;

use Exception;
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Exception\CouldNotSaveException;


use Magento\Framework\Exception\StateException;
use Magento\Framework\Model\AbstractModel;

use Vasan\KpsAddress\Api\AddressRepositoryInterface;
use Vasan\KpsAddress\Api\Data\AddressInterface;
use Vasan\KpsAddress\Api\Data\AddressSearchResultsInterface;
use Vasan\KpsAddress\Api\Data\AddressSearchResultsInterfaceFactory as SearchResultFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Vasan\KpsAddress\Model\ResourceModel\Address\Collection;
use Vasan\KpsAddress\Model\ResourceModel\Address\CollectionFactory;
use Vasan\KpsAddress\Model\AddressFactory;
use Vasan\KpsAddress\Model\ResourceModel\Address as AddressResource;


class AddressRepository implements AddressRepositoryInterface
{

    /**
     * @var array
     */
    protected $instances = [];

    /**
     * @var SearchResultFactory
     */
    private $searchResultFactory;

    /**
     * @var CollectionFactory
     */
    private $collectionFactory;

    /**
     * @var JoinProcessorInterface
     */
    private $joinProcessor;

    /**
     * @var CollectionProcessorInterface
     */
    private $collectionProcessor;

    /**
     * @var AddressFactory
     */
    private $addressFactory;

    /**
     * @var AddressResource
     */
    private $addressResource;

    /**
     * AddressRepository constructor.
     * @param SearchResultFactory $searchResultFactory
     * @param CollectionFactory $collectionFactory
     * @param JoinProcessorInterface $joinProcessor
     * @param CollectionProcessorInterface $collectionProcessor
     * @param AddressFactory $addressFactory
     * @param AddressResource $addressResource
     */
    public function __construct(
        SearchResultFactory $searchResultFactory,
        CollectionFactory $collectionFactory,
        JoinProcessorInterface $joinProcessor,
        CollectionProcessorInterface $collectionProcessor,
        AddressFactory $addressFactory,
        AddressResource $addressResource
    ) {
        $this->searchResultFactory = $searchResultFactory;
        $this->collectionFactory = $collectionFactory;
        $this->joinProcessor = $joinProcessor;
        $this->collectionProcessor = $collectionProcessor;
        $this->addressFactory = $addressFactory;
        $this->addressResource = $addressResource;
    }

    /**
     * @inheridoc
     */
    public function getList(SearchCriteriaInterface $searchCriteria)
    {
        /** @var AddressSearchResultsInterface $searchResult */
        $searchResult = $this->searchResultFactory->create();
        /** @var Collection $collection */
        $collection = $this->collectionFactory->create();

        $this->joinProcessor->process($collection, AddressInterface::class);
        $this->collectionProcessor->process($searchCriteria, $collection);
        $searchResult->setSearchCriteria($searchCriteria);
        $searchResult->setTotalCount($collection->getSize());
        $searchResult->setItems($collection->getItems());

        return $searchResult;
    }

    /**
     * @inheridoc
     */
    public function save(AddressInterface $address)
    {
        try {
            /** @var AddressInterface | AbstractModel  $address */
            $this->addressResource->save($address);
        } catch (Exception $exception) {
            throw new CouldNotSaveException(__('Could not save the address: %1', $exception->getMessage()));
        }

        return $address;
    }

    /**
     * @inheridoc
     */
    public function getById($id)
    {
        if (!isset($this->instances[$id])) {
            /** @var AddressInterface | AbstractModel  $address */
            $address = $this->addressFactory->create();
            $this->addressResource->load($address, $id);
            if (!$address->getId()) {
                throw new NoSuchEntityException(__('Address does not exist'));
            }
            $this->instances[$id] = $address;
        }

        return $this->instances[$id];
    }

    /**
     * @inheridoc
     */
    public function delete(AddressInterface $address)
    {
        /** @var AddressInterface | AbstractModel  $address */
        $id = $address->getId();

        try {
            $this->addressResource->delete($address);
            unset($this->instances[$id]);
        } catch (Exception $e) {
            throw new StateException(__('Unable to remove address %1', $id));
        }

        return true;
    }

    /**
     * @inheridoc
     */
    public function deleteById($id)
    {
        $this->delete($this->getById($id));
    }
}

6.Dependency Injections

<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * di
 *
 * @copyright Copyright © 2021 Vasan. All rights reserved.
 * @author    survasp@gmail.com
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Vasan\KpsAddress\Api\Data\AddressInterface" type="Vasan\KpsAddress\Model\Address" />
    <preference for="Vasan\KpsAddress\Api\AddressRepositoryInterface" type="Vasan\KpsAddress\Model\Repository\AddressRepository" />
    <preference for="Vasan\KpsAddress\Api\Data\AddressSearchResultsInterface" type="Magento\Framework\Api\SearchResults" />
    <type name="Magento\Framework\Console\CommandListInterface">
        <arguments>
            <argument name="commands" xsi:type="array">
                <item name="vasan_dms_install" xsi:type="object">Vasan\KpsAddress\Console\InstallAddress</item>
            </argument>
        </arguments>
    </type>


    <virtualType name="KpsAddressGirdFilterPool" type="Magento\Framework\View\Element\UiComponent\DataProvider\FilterPool">
        <arguments>
            <argument name="appliers" xsi:type="array">
                <item name="regular" xsi:type="object">Magento\Framework\View\Element\UiComponent\DataProvider\RegularFilter</item>
                <item name="fulltext" xsi:type="object">Magento\Framework\View\Element\UiComponent\DataProvider\FulltextFilter</item>
            </argument>
        </arguments>
    </virtualType>

    <virtualType name="KpsAddressGridDataProvider" type="Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider">
        <arguments>
            <argument name="collection" xsi:type="object" shared="false">Vasan\KpsAddress\Model\ResourceModel\Address\Collection</argument>
            <argument name="filterPool" xsi:type="object" shared="false">KpsAddressGirdFilterPool</argument>
        </arguments>
    </virtualType>

    <virtualType name="Vasan\KpsAddress\Model\ResourceModel\Address\Grid\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
        <arguments>
            <argument name="mainTable" xsi:type="string">kps_address</argument>
            <argument name="resourceModel" xsi:type="string">Vasan\KpsAddress\Model\ResourceModel\Address</argument>
        </arguments>
    </virtualType>

    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="kpsaddress_index_listing_data_source" xsi:type="string">Vasan\KpsAddress\Model\ResourceModel\Address\Grid\Collection</item>
            </argument>
        </arguments>
    </type>

</config>


DataProvider class

<?php
/**
 * DataProvider
 *
 * @copyright Copyright © 2021 Vasan. All rights reserved.
 * @author    survasp@gmail.com
 */

namespace Vasan\KpsAddress\Model\Address;

use Magento\Ui\DataProvider\AbstractDataProvider;
use Vasan\KpsAddress\Api\Data\AddressInterface;
use Vasan\KpsAddress\Model\ResourceModel\Address\Collection;
use Vasan\KpsAddress\Model\ResourceModel\Address\CollectionFactory;
use Vasan\KpsAddress\Model\Address;

class DataProvider extends AbstractDataProvider
{

    /**
     * @var Collection
     */
    protected $collection;

    /**
     * @var array
     */
    protected $loadedData;

    /**
     * @param string $name
     * @param string $primaryFieldName
     * @param string $requestFieldName
     * @param CollectionFactory $collectionFactory
     * @param array $meta
     * @param array $data
     */
    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        CollectionFactory $collectionFactory,
        array $meta = [],
        array $data = []
    ) {
        $this->collection = $collectionFactory->create();
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
    }

    /**
     * @return array
     */
    public function getData()
    {
        if (isset($this->loadedData)) {
            return $this->loadedData;
        }

        $items = $this->collection->getItems();

        /** @var AddressInterface|Address $address */
        foreach ($items as $address) {
            $this->loadedData[$address->getId()] = $address->getData();
        }

        return $this->loadedData;
    }

}

7.Csv Upload

Install class for reading the data from csv and add into database

<?php
/**
 * Install
 *
 * @copyright Copyright © 2021 Vasan. All rights reserved.
 * @author    survasp@gmail.com
 */

namespace Vasan\KpsAddress\Model\Address;

use Vasan\KpsAddress\Api\Data\AddressInterface;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\File\Csv;
use Vasan\KpsAddress\Api\Data\AddressInterfaceFactory;
use Vasan\KpsAddress\Api\AddressRepositoryInterface;
use Magento\Framework\Model\AbstractModel;
use Psr\Log\LoggerInterface;

class Install
{
    /**
     * @var Csv $csv
     */
    protected $csv;

    /**
     * @var AddressRepositoryInterface $addressRepository
     */
    protected $addressRepository;

    /**
     * @var AddressInterfaceFactory
     */
    protected $addressFactory;

    /**
     * @var LoggerInterface
     */
    protected $logger;

    /**
     * Install constructor.
     * @param Csv $csv
     * @param AddressRepositoryInterface $addressRepository
     * @param AddressInterfaceFactory $addressFactory
     * @param LoggerInterface $logger
     */
    public function __construct(
        Csv $csv,
        AddressRepositoryInterface $addressRepository,
        AddressInterfaceFactory $addressFactory,
        LoggerInterface $logger
    ) {
        $this->addressRepository = $addressRepository;
        $this->csv = $csv;
        $this->addressFactory = $addressFactory;
        $this->logger = $logger;
    }


    public function saveAddress($addressArray)
    {
        try {
            /** @var AddressInterface | AbstractModel  $address */
            $address = $this->addressFactory->create();
            $address->setProvince($addressArray['province']);
            $address->setProvinceCode($addressArray['province_code']);
            $address->setCity($addressArray['city']);
            $address->setCityCode($addressArray['city_code']);
            $address->setUrban($addressArray['urban']);
            $address->setUrbanCode($addressArray['urban_code']);
            $address->setSubDistrict($addressArray['sub_district']);
            $address->setSubDistrictCode($addressArray['sub_district_code']);
            $address->setZipcode($addressArray['zipcode']);
            $this->addressRepository->save($address);
        } catch (\Exception $exception) {
            $this->logger->error('The record is not saved for ' . $exception->getMessage());
        }

    }

    public function getCsv($filename, $usecolumn = null)
    {
        $source = dirname(__FILE__) . '/../../Source/' . $filename;

        try {
            if (!$usecolumn) {
                return $this->csv->getData($source);
            }
            $rows = [];
            $csvData = $this->csv->getData($source);
            $columns = array_values($csvData[0]);
            $idx = 0;
            foreach ($csvData as $data) {
                if ($idx == 0) {
                    $idx++;
                    continue;
                }
                $temp = [];
                $notEmpty = false;
                foreach ($columns as $key => $colname) {
                    $colname = trim($colname);
                    $value = trim($data[$key]);
                    if (!empty($value)) {
                        $notEmpty = true;
                    }
                    $temp[$colname] = $value;
                }
                if ($notEmpty) {
                    $rows[] = $temp;
                }
                $idx++;
            }

            return $rows;
        } catch (\Exception $e) {
            return [];
        }
    }
}

InstallAddress is command line class to install address from command line

<?php
/**
 * InstallAddress
 *
 * @copyright Copyright © 2021 Vasan. All rights reserved.
 * @author    survasp@gmail.com
 */

namespace Vasan\DmsAddress\Console;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Vasan\KpsAddress\Model\Address\Install;


class InstallAddress extends Command
{

    /**
     * @var Install
     */
    protected $install;

    public function __construct(Install $install) {
        $this->install = $install;
        parent::__construct('vasan:kps:install');
    }

    /**
     * {@inheritedoc}
     */
    protected function configure()
    {
        $this->setName('vasan:kps:install')
            ->setDescription('Install address from csv');

        parent::configure();
    }



    protected function execute(InputInterface $input, OutputInterface $output)
    {

        $output->writeln('Address install started ');

        $addresses = $this->install->getCsv('kps_address.csv', true);

        foreach ($addresses as $address) {
            $this->install->saveAddress($address);
        }

        $output->writeln('Address install completed ');
    }
}

vasan
Last edit
vasan
Posted by vasan to vasan's deck (2021-01-10 16:55)