Completed
Push — Order ( a8dbff...a1a9f7 )
by Laurent
03:52
created

ControllerHelper::getSlug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
/**
3
 * ControllerHelper Helpers des Controller de l'application GLSR.
4
 *
5
 * PHP Version 5
6
 *
7
 * @author    Quétier Laurent <[email protected]>
8
 * @copyright 2014 Dev-Int GLSR
9
 * @license   http://opensource.org/licenses/gpl-license.php GNU Public License
10
 *
11
 * @version since 1.0.0
12
 *
13
 * @link      https://github.com/Dev-Int/glsr
14
 */
15
namespace AppBundle\Helper;
16
17
use AppBundle\Entity\Article;
18
use Doctrine\Common\Persistence\ObjectManager;
19
use Symfony\Component\HttpFoundation\Session\Session;
20
21
/**
22
 * Controller helper.
23
 *
24
 * @category Helper
25
 */
26
class ControllerHelper
27
{
28
    private $translator;
29
    private $session;
30
31
    public function __construct($translator, $session)
32
    {
33
        $this->translator = $translator;
34
        $this->session = $session;
35
    }
36
37
    /**
38
     * Tests of creation conditions.
39
     *
40
     * @param array Articles à tester
41
     * @return boolean
42
     */
43
    public function hasSupplierArticles(array $articles)
44
    {
45
        $return = true;
46
47
        // This supplier has no articles!
48
        if (count($articles) < 1) {
49
            $message = $this->translator->trans('settings.no_articles', array(), 'gs_suppliers');
50
            $this->session->getFlashBag()->add('danger', $message);
51
            $return = false;
52
        }
53
54
        return $return;
55
    }
56
57
    /**
58
     * Tests Order in progress for a supplier.
59
     *
60
     * @param \AppBundle\Entity\Article $articles Articles to test
61
     * @param \Doctrine\Common\Persistence\ObjectManager $etm Named object manager
62
     * @return boolean
63
     */
64
    public function isOrderInProgress(Article $articles, ObjectManager $etm)
65
    {
66
        $return = false;
67
        $orders = $etm->getRepository('AppBundle:Orders')->findAll();
68
        // This provider already has an order in progress!
69
        foreach ($orders as $order) {
70
            if ($order->getSupplier() === $articles->getSupplier()) {
71
                $return = true;
72
            }
73
        }
74
        return $return;
75
    }
76
77
    /**
78
     * Array of file (`pdf`) layout.
79
     *
80
     * @param string $date  File date
81
     * @param string $title File title
82
     * @return array
83
     */
84
    public function getArray($date, $title)
85
    {
86
        $array = array(
87
            'margin-top' => 15,
88
            'header-spacing' => 5,
89
            'header-font-size' => 8,
90
            'header-left' => 'G.L.S.R.',
91
            'header-center' => $title,
92
            'header-right' => $date,
93
            'header-line' => true,
94
            'margin-bottom' => 15,
95
            'footer-spacing' => 5,
96
            'footer-font-size' => 8,
97
            'footer-left' => 'GLSR &copy 2014 and beyond.',
98
            'footer-right' => 'Page [page]/[toPage]',
99
            'footer-line' => true,
100
        );
101
        return $array;
102
    }
103
104
    /**
105
     * Test paramters to return.
106
     *
107
     * @param Object $entity     Entity to return
108
     * @param string $entityName Entity name to test
109
     * @return array Parameters to return
110
     */
111
    public function testReturnParam($entity, $entityName)
112
    {
113
        $entityArray = ['article', 'supplier', 'familylog', 'zonestorage', 'unitstorage', ];
114
        if (in_array($entityName, $entityArray, true)) {
115
            $param = ['slug' => $entity->getSlug()];
116
        } else {
117
            $param = ['id' => $entity->getId()];
118
        }
119
120
        return $param;
121
    }
122
123
    /**
124
     * SetOrder for the SortAction in views.
125
     *
126
     * @param string $name   session name
127
     * @param string $entity entity name
128
     * @param string $field  field name
129
     * @param string $type   sort type ("ASC"/"DESC")
130
     */
131
    public function setOrder($name, $entity, $field, $type = 'ASC')
132
    {
133
        $session = new Session();
134
135
        $session->set('sort.'.$name, array('entity' => $entity, 'field' => $field, 'type' => $type));
136
    }
137
138
    /**
139
     * GetOrder for the SortAction in views.
140
     *
141
     * @param string $name session name
142
     *
143
     * @return array
144
     */
145
    public function getOrder($name)
146
    {
147
        $session = new Session();
148
149
        return $session->has('sort.' . $name) ? $session->get('sort.' . $name) : null;
150
    }
151
}
152