CONTRANCE6: BUILDING A TOOL (IMPORT ET4 TOOLS PLUGIN)

created: 02.11.2018 14:29

edited: 09.01.2019 11:37

back

Add: /contrance/Plugin/<ModulName>/Controller/Tools/<ToolName>Plugin.php

php
<?php

App::uses('ToolsPlugin', 'Tools.Controller/Tools');

class ImportEt4ToolsPlugin extends ToolsPlugin
{
    // Which icon should be used?
    public $icon = 'fa fa-upload';

    // Defines each specific step and its name
    public $steps = [
        'url',
    ];

    // Which components are used for this tool? In this case 'Session" to 
    // save during requests and responses
    public $components = [
        'Session',
    ];

    // Builds the button and the description
    public function __construct()
    {
        $this->button      = __d('app', 'Punkte importieren');
        $this->description = __d('app', 'Punkte aus et4 importieren');
    }

    // Calls the View and the edit fields from View/Tools/<ToolName>/<StepName>.php
    public function _processUrl(Controller $controller)
    {
    }

    // Here is the logic, what will you do with your edit fields data from <StepName>.php in the 'View' directory?
    public function _validateUrl(Controller $controller, $data)
    {
        if (Hash::get($data, 'url') == "") {
            return false;
        }
        $xml          = Xml::build(Hash::get($data, 'url'));
        $pointsModel  = ClassRegistry::init('Points.Point');
        $maxItems     = count($xml->items->item);
        $countSuccess = 0;
        foreach ($xml->items->item as $item) {
            $point                               = $pointsModel->find(
                'first',
                [
                    'conditions' => ['external_id' => (string)$item->id],
                    'contain'    => 'PointI18n'
                ]
            );
            $insertData['Point']['id']           = Hash::get($point, 'Point.id');
            $insertData['Point']['title']        = (string)$item->title;
            $insertData['Point']['external_id']  = (string)$item->id;
            $insertData['Point']['content_type'] = strtolower((string)$item->type);
            $insertData['Point']['category']     = (string)$item->categories->category;

            if ($item->highlight) {
                $insertData['Point']['top_tip'] = 1;
            } else {
                $insertData['Point']['top_tip'] = 0;
            }
            $insertData['Point']['lat']         = (string)$item->geo->main['latitude'];
            $insertData['Point']['long']        = (string)$item->geo->main['longitude'];
            $insertData['Point']['street']      = (string)$item->street;
            $insertData['Point']['zip_code']    = (string)$item->zip;
            $insertData['Point']['city']        = (string)$item->city;
            $insertData['Point']['phone']       = (string)$item->phone;
            $insertData['Point']['website_url'] = (string)$item->web;
            $insertData['Point']['email']       = (string)$item->email;

            $k = 1;
            if (count($item->areas->area) > 0) {
                foreach ($item->areas->area as $region) {
                    if ($insertData['Point']['region' . $k] = $region[0]) {
                        $insertData['Point']['region' . $k] = $region[0];
                    } else {
                        continue;
                    }
                    if ($k >= 3) {
                        break;
                    }
                    $k++;
                }
            }

            $insertData['Point']['teaser_title']                = (string)$item->title;
            $insertData['Point']['teaser_image']                = $pointsModel->fakeUpload(
                (string)$item->media_objects->media_object['url']
            );
            $insertData['Point']['PointI18n'][0]['teaser_text'] = (string)$item->texts->text[5];

            $i = 1;
            foreach ($item->media_objects->media_object as $media) {
                $insertData['Point']['image_' . $i] = $pointsModel->fakeUpload((string)$media['url'][0]);

                // Maximale Anzahl an Bildern welche hochgeladen werden k├Ânnen
                if ($i >= 7) {
                    break;
                }
                $i++;
            }

            $insertData['Point']['PointI18n'][0]['id']          = Hash::get($point, 'PointI18n.0.id');
            $insertData['Point']['PointI18n'][0]['language_id'] = 1;
            $insertData['Point']['PointI18n'][0]['country']     = (string)$item->country;
            $insertData['Point']['PointI18n'][0]['detail_text'] = (string)$item->texts->text[0];

            if ($pointsModel->saveAssociated($insertData, ['deep' => true])) {
                $countSuccess++;
            } else {
                CakeLog::write('debug', 'Error Importing External ID: ' . (string)$item->id);
                exit;
            }
        }

        $controller->Session->setFlash(
            __d(
                'app',
                '%s von %s Daten wurden importiert',
                [$countSuccess, $maxItems]
            ),
            'BaseData.flash',
            [
                'title' => __d('app', 'Import erfolgreich'),
                'type'  => $countSuccess == $maxItems ? 'success' : 'error'
            ]
        );
    }
}

Add: /contrance/Plugin/<ModulName>/View/Tools/<ToolName>/<StepName>.php

php
<?php
$this->extend('Tools.Tools/base');

$this->append('form');
echo $this->AceForm->create(false, ['class' => 'form-horizontal']);

echo $this->AceForm->input(
    'url',
    [
        'type'  => 'text',
        'label' => ['text' => __d('app', 'URL')]
    ]
);
echo $this->element('BaseData.formWizardButtons');
echo $this->AceForm->end();
$this->end();