FlexFormHooks.php 2.15 KB
Newer Older
Dainis Abols's avatar
Dainis Abols committed
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
<?php

namespace Lu\LuContacts\Hooks;

use Lu\LuApi\DataSources\LuisContacts;
use Lu\LuContacts\Helpers\DataHelper;
use Lu\LuContacts\Models\Navigation;

/**
 * Class FlexFormHooks
 *
 * @author  Dainis Abols <dainis.abols@lu.lv>
 * @owner   University of Latvia
 * @version 3.0.0
 * @since   28.06.2018
 *
 * @package Lu\LuContacts\Hooks
 */
class FlexFormHooks
{
    /**
     * Create navigation select menu
     *
     * @param array $config
     */
    public function defaultNavigation(array &$config)
    {
        // Preset menu
        $optionList = $this->newOptionsList();

        // Get navigation items
        $NavigationModel = new Navigation();
        $navigtion       = $NavigationModel->getNavigation();

        // Create optionsList
        foreach ($navigtion as $item) {
            $optionList[] = [
                $item['name'],
                $item['id'],
            ];
        }

        // Merge into items
        $config['items'] = array_merge($config['items'], $optionList);
    }

    /**
     * Create address list select menu
     *
     * @param array $config
     */
    public function addressList(array &$config)
    {
        // Preset menu and models
        $optionList = $this->newOptionsList(true);
        $DataHelper = new DataHelper();

        // Get address list from Solr
        $luisContacts = new LuisContacts();
        $luisContacts->setRequestType('address');
        $jsonData  = $luisContacts->fetch();
        $addresses = $DataHelper->APIDecode($jsonData);

        // Create optionsList
        foreach ($addresses as $item) {
            $optionList[] = [
                $item['name'],
                str_replace(@SOLR_PREFIX_ADDRESS, null, $item['id']),
            ];
        }

        // Merge into items
        $config['items'] = array_merge($config['items'], $optionList);
    }

    /**
     * Create new empty options list with or without a first element
     *
     * @param bool $addFirstEmptyElement
     *
     * @return array
     */
    private function newOptionsList(bool $addFirstEmptyElement = false)
    {
        if ($addFirstEmptyElement) {
            return [['' => '']];
        }

        return [];
    }
}