SlideshowController.php 3.63 KB
Newer Older
Marcello Pivanti's avatar
Marcello Pivanti committed
1
2
3
4
5
6
7
<?php

/**
 * Art-ER Attrattività, ricerca e territorio dell’Emilia-Romagna
 * OPEN 2.0
 *
 *
waltermazza's avatar
waltermazza committed
8
 * @package    arter\amos\slideshow\controllers
Marcello Pivanti's avatar
Marcello Pivanti committed
9
10
11
12
 * @category   CategoryName
 * @author     Elite Division S.r.l.
 */

waltermazza's avatar
waltermazza committed
13
namespace arter\amos\slideshow\controllers;
Marcello Pivanti's avatar
Marcello Pivanti committed
14

waltermazza's avatar
waltermazza committed
15
16
use arter\amos\slideshow\models\SlideshowRoute;
use arter\amos\slideshow\models\SlideshowUserflag;
Marcello Pivanti's avatar
Marcello Pivanti committed
17
18
19
20
21
22
23
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;

/**
 * Class SlideshowController
waltermazza's avatar
waltermazza committed
24
 * @package arter\amos\slideshow\controllers
Marcello Pivanti's avatar
Marcello Pivanti committed
25
26
27
 *
 * This is the class for controller "SlideshowController".
 */
waltermazza's avatar
waltermazza committed
28
class SlideshowController extends \arter\amos\slideshow\controllers\base\SlideshowController
Marcello Pivanti's avatar
Marcello Pivanti committed
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
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
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        $behaviors = ArrayHelper::merge(parent::behaviors(), [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'actions' => [
                            'cambia',
                            'route-by-role',
                        ],
                        'roles' => ['@']
                    ]
                ]
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'route-by-role' => ['post']
                ]
            ]
        ]);
        return $behaviors;
    }

    /**
     * @return bool
     */
    public function actionCambia()
    {
        if (\Yii::$app->request->isAjax) {
            if (!\Yii::$app->getUser()->isGuest) {
                $data = \Yii::$app->request->post();
                if (isset($data['set']) && $data['set'] == 'true') {
                    $routeId = $data['value'];
                    $userId = \Yii::$app->getUser()->getId();
                    $slideshowUserflag = SlideshowUserflag::findOne(['user_id' => $userId, 'slideshow_route_id' => $routeId]);
                    if (!$slideshowUserflag) {
                        $slideshowUserflag = new SlideshowUserflag();
                        $slideshowUserflag->slideshow_route_id = $routeId;
                        $slideshowUserflag->user_id = $userId;
                        $slideshowUserflag->save(FALSE);
                    }
                } else {
                    $routeId = $data['value'];
                    $userId = \Yii::$app->getUser()->getId();
                    $slideshowUserflag = SlideshowUserflag::deleteAll(['user_id' => $userId, 'slideshow_route_id' => $routeId]);
                }
            }
        }

        return TRUE;
    }

    public function actionRouteByRole()
    {
        $out = [];
        if (isset($_POST['depdrop_parents'])) {
            $id = end($_POST['depdrop_parents']);
            $id_selected = end($_POST['depdrop_params']);
            $slideshow = new SlideshowRoute();
            $routes = $slideshow->getRotte(NULL, $id);
            $selected = null;

            if ($id != null && count($routes) > 0) {
                $selected = '';
                foreach ($routes as $i => $route) {

                    $out[] = ['id' => $i, 'name' => $route];

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

        echo Json::encode(['output' => '', 'selected' => '']);
    }
}