AjaxController.php 4.29 KB
Newer Older
Marcello Pivanti's avatar
Marcello Pivanti committed
1
<?php
Aster's avatar
Aster committed
2

Marcello Pivanti's avatar
Marcello Pivanti committed
3
/**
Aster's avatar
Aster committed
4
5
 * Lombardia Informatica S.p.A.
 * OPEN 2.0
Marcello Pivanti's avatar
Marcello Pivanti committed
6
 *
Aster's avatar
Aster committed
7
 * @see http://example.com Developers'community
8
9
 * @license GPLv3
 * @license https://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3
Marcello Pivanti's avatar
Marcello Pivanti committed
10
 *
Aster's avatar
Aster committed
11
 * @package    elitedivision\amos\sondaggi\controllers
Marcello Pivanti's avatar
Marcello Pivanti committed
12
 * @category   CategoryName
Aster's avatar
Aster committed
13
 * @author     Lombardia Informatica S.p.A.
Marcello Pivanti's avatar
Marcello Pivanti committed
14
15
 */

Aster's avatar
Aster committed
16
namespace elitedivision\amos\sondaggi\controllers;
Marcello Pivanti's avatar
Marcello Pivanti committed
17

Aster's avatar
Aster committed
18
19
20
use elitedivision\amos\sondaggi\AmosSondaggi;
use elitedivision\amos\sondaggi\models\SondaggiDomande;
use elitedivision\amos\sondaggi\models\SondaggiDomandePagine;
Marcello Pivanti's avatar
Marcello Pivanti committed
21
22
23
24
25
26
27
28
use yii\db\Query;
use yii\filters\AccessControl;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\web\Controller;

/**
 * Class AjaxController
Aster's avatar
Aster committed
29
 * @package elitedivision\amos\sondaggi\controllers
Marcello Pivanti's avatar
Marcello Pivanti committed
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
 */
class AjaxController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        $behaviors = ArrayHelper::merge(parent::behaviors(), [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'actions' => [
                            'domande-by-pagine',
                            'pagine',
                            'pagine-by-sondaggio',
                        ],
                        'roles' => ['@']
                    ]
                ]
            ]
        ]);
        return $behaviors;
    }

    public function actionPagineBySondaggio()
    {
        $out = [];
        if (isset($_POST['depdrop_parents'])) {
            $id = end($_POST['depdrop_parents']);
            $id_selected = end($_POST['depdrop_params']);
            $pagine = SondaggiDomandePagine::find()->andWhere(['sondaggi_id' => $id])->asArray()->all();
            $selected = null;
            if ($id != null && count($pagine) > 0) {
                $selected = '';
                foreach ($pagine as $i => $pagina) {

                    $out[] = ['id' => $pagina['id'], 'name' => $pagina['titolo']];

                    if ($id_selected) {
                        $selected = $id_selected;
                    } elseif ($i == 0) {
                        $selected = $pagina['id'];
                    }
                }
                // Shows how you can preselect a value
                return Json::encode(['output' => $out, 'selected' => $selected]);
            }
        }
        return Json::encode(['output' => '', 'selected' => '']);
    }

    public function actionDomandeByPagine()
    {
        $out = [];
        if (isset($_POST['depdrop_parents'])) {
            $id = end($_POST['depdrop_parents']);
            $id_selected = end($_POST['depdrop_params']);
            $pagine = SondaggiDomande::find()->andWhere(['sondaggi_domande_pagine_id' => $id])->orderBy('ordinamento ASC')->asArray()->all();
            $selected = null;
            if ($id != null && count($pagine) > 0) {
                $selected = '';
                foreach ($pagine as $i => $pagina) {

                    $out[] = ['id' => $pagina['id'], 'name' => $pagina['domanda']];

                    if ($id_selected) {
                        $selected = $id_selected;
                    } elseif ($i == 0) {
                        $selected = $pagina['id'];
                    }
                }
                // Shows how you can preselect a value
                return Json::encode(['output' => $out, 'selected' => $selected]);
            }
        }
        return Json::encode(['output' => '', 'selected' => '']);
    }

    public function actionPagine($search = null, $id = null)
    {
        $out = ['more' => false];
        if (!is_null($search)) {
            $query = new Query();
            $query->select('id, titolo AS text')
                ->from('sondaggi_domande_pagine')
                ->where('titolo LIKE "%' . $search . '%"');
            //->limit(20);
            $command = $query->createCommand();
            $data = $command->queryAll();
            $out['results'] = array_values($data);
        } elseif ($id > 0) {
            $out['results'] = ['id' => $id, 'text' => SondaggiDomandePagine::findOne($id)->titolo];
        } else {
            $out['results'] = ['id' => 0, 'text' => AmosSondaggi::t('amossondaggi', 'Nessun risultato trovato')];
        }
        return Json::encode($out);
    }
}