Completed
Push — master ( 55a9f3...13acd2 )
by Alexey
05:06
created

WidgetsController::widgetChooserAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 11
rs 9.4285
cc 3
eloc 7
nc 3
nop 0
1
<?php
2
3
/**
4
 * Widgets admin controller
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
class WidgetsController extends Controller
12
{
13
    public function widgetChooserAction()
14
    {
15
        $widgets = [];
16
        foreach (App::$primary->config['modules'] as $module) {
17
            $info = Module::getInfo($module);
18
            if (!empty($info['widgets'])) {
19
                $widgets += $info['widgets'];
20
            }
21
        }
22
        $this->view->page(['page' => 'blank', 'data' => compact('widgets')]);
23
    }
24
25
    public function widgetImageAction()
1 ignored issue
show
Coding Style introduced by
widgetImageAction uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
26
    {
27
        if (!empty($_GET['text'])) {
28
            $widgetCode = explode(':', preg_replace('!^{WIDGET:!isU', '', preg_replace('!}$!isU', '', urldecode($_GET['text']))));
29
            $text = 'Виджет: ';
30
            $widget = false; //Widget::get($widgetCode[0], 'widget_filename');
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
31
32
            if ($widget) {
33
                $text .= $widget->widget_name . "\n";
34
                $i = 1;
35
                if (isset($widgetCode[$i]) && $widget->widget_params) {
36
                    $params = json_decode($widget->widget_params, true);
37
                    if ($params) {
38
                        foreach ($params as $param) {
39
                            if (!isset($widgetCode[$i]))
40
                                break;
41
                            if ($param['type'] == 'select') {
42
                                $item = $param['model']::get($widgetCode[$i++]);
43
                                if ($item) {
44
                                    $text .= $param['name'] . ': ' . $item->$param['showCol'] . "\n";
45
                                } else {
46
                                    $text .= $widgetCode[$i - 1];
47
                                }
48
                            } else {
49
                                $value = $widgetCode[$i++];
50
                                if (mb_strlen($value, 'utf-8') > 50) {
51
                                    $value = mb_substr($value, 0, 50) . '...';
52
                                }
53
                                $text .= $param['name'] . ': ' . $value . "\n";
54
                            }
55
                        }
56
                    }
57
                } else {
58
                    unset($widgetCode[0]);
59
                    foreach ($widgetCode as $item) {
60
                        $text .= $item . "\n";
61
                    }
62
                }
63
            } else {
64
                foreach ($widgetCode as $item) {
65
                    $text .= $item . "\n";
66
                }
67
            }
68
        } else {
69
            $text = 'text not defined';
70
        }
71
72
        header('Content-type: image/png');
73
        // шрифт
74
        $font = dirname(__FILE__) . '/../fonts/Cousine/Cousine-Regular.ttf';
75
        // вычисляем сколько места займёт текст
76
        $bbox = imageftbbox(10, 0, $font, $text);
77
78
79
80
81
        $width = abs($bbox[0]) + abs($bbox[2]); // distance from left to right
82
        $height = abs($bbox[1]) + abs($bbox[5]); // distance from top to bottom
83
        // размер изображения
84
        $img = imagecreatetruecolor($width + 10, $height + 10);
85
        // цвет текста
86
        $black = imagecolorallocate($img, 255, 255, 255);
87
88
        // цвет фона
89
        $bg = imagecolorallocate($img, 85, 101, 115);
90
        imagefilledrectangle($img, 0, 0, $width + 10, $height + 10, $bg);
91
92
        // вычисляем координаты для центрирования
93
        $x = (imagesx($img) - $bbox[4]) / 2;
94
        $y = (imagesy($img) - $bbox[5] - $bbox[3]) / 2;
95
96
        // добавляем текст на изображение
97
        imagefttext($img, 10, 0, $x, $y, $black, $font, $text);
98
99
        // выводим изображение
100
        imagepng($img);
101
        // освобождаем память
102
        imagedestroy($img);
103
    }
104
105
}
106