Completed
Push — master ( 81699d...a5d7cd )
by Julito
31:09
created

LegacyController::setContainerValuesToLegacy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Controller;
5
6
use Chamilo\CourseBundle\Controller\ToolBaseController;
7
use Doctrine\DBAL\Connection;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
12
use Chamilo\CoreBundle\Framework\Container;
13
use Display;
14
15
/**
16
 * Class LegacyController
17
 * Manages the chamilo pages starting with Display::display_header and $tpl = new Template();
18
 * @package Chamilo\CoreBundle\Controller
19
 * @author Julio Montoya <[email protected]>
20
 */
21
class LegacyController extends ToolBaseController
22
{
23
    public $section;
24
25
    private function setContainerValuesToLegacy()
26
    {
27
          /** @var Connection $dbConnection */
28
        $dbConnection = $this->container->get('database_connection');
29
        $em = $this->get('kernel')->getContainer()->get('doctrine.orm.entity_manager');
30
31
        $database = new \Database($dbConnection, array());
32
33
        $database->setConnection($dbConnection);
34
        $database->setManager($em);
35
        Container::$container = $this->container;
36
        Container::$dataDir = $this->container->get('kernel')->getDataDir();
37
        Container::$courseDir = $this->container->get('kernel')->getDataDir();
38
        $this->container->get('twig')->addGlobal('api_get_cidreq', api_get_cidreq());
39
    }
40
41
    /**
42
     * Handles all request in old legacy files inside 'main/' folder
43
     * @param string $name
44
     * @param Request $request
45
     * @return Response
46
     */
47
    public function classicAction($name, Request $request, $folder = 'main')
48
    {
49
        // get.
50
        $_GET = $request->query->all();
51
        // post.
52
        $_POST = $request->request->all();
53
        $rootDir = $this->get('kernel')->getRealRootDir();
54
        $mainPath = $rootDir.$folder.'/';
55
        $fileToLoad = $mainPath.$name;
56
57
        // Setting legacy values inside the container
58
        $this->setContainerValuesToLegacy();
59
60
        if (is_file($fileToLoad) &&
61
            \Security::check_abs_path($fileToLoad, $mainPath)
62
        ) {
63
            /**
64
             * Some legacy Chamilo files still use this variables directly,
65
             * instead of using a function.
66
            **/
67
            $is_allowed_in_course = api_is_allowed_in_course();
68
            $is_courseAdmin = api_is_course_admin();
69
            $is_platformAdmin = api_is_platform_admin();
70
            $toolNameFromFile = basename(dirname($fileToLoad));
71
            $charset = 'UTF-8';
72
            // Default values
73
            $_course = api_get_course_info();
74
            $_user = api_get_user_info();
75
            $_cid = api_get_course_id();
76
            $debug = $this->container->get('kernel')->getEnvironment() == 'dev' ? true : false;
77
78
            // Loading legacy file
79
            ob_start();
80
            require_once $fileToLoad;
81
            $out = ob_get_contents();
82
            ob_end_clean();
83
84
            // No browser cache when executing an exercise.
85
            if ($name == 'exercise/exercise_submit.php') {
86
                $responseHeaders = array(
87
                    'cache-control' => 'no-store, no-cache, must-revalidate'
88
                );
89
            }
90
91
            // Loading code to be added
92
            $js = isset($htmlHeadXtra) ? $htmlHeadXtra : array();
0 ignored issues
show
Bug introduced by
The variable $htmlHeadXtra seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
93
94
            // Loading legacy breadcrumb $interbreadcrumb
95
            $interbreadcrumb = isset($interbreadcrumb) ? $interbreadcrumb : null;
0 ignored issues
show
Bug introduced by
The variable $interbreadcrumb seems only to be defined at a later point. As such the call to isset() seems to always evaluate to false.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
96
97
            // We change the layout based in this variable
98
            // This could be changed on the fly by a legacy script.
99
            $template = Container::$legacyTemplate;
100
            $params = [
101
                'legacy_breadcrumb' => $interbreadcrumb,
102
                'js' => $js
103
            ];
104
105
            // This means the page comes from legacy use Display::display_header
106
            if (!empty($out)) {
107
                $params['content'] = $out;
108
            } else {
109
                // This means the page comes from legacy use of $tpl = new Template();
110
                $legacyParams = \Template::$params;
111
                if (!empty($legacyParams)) {
112
                    $params = array_merge($legacyParams, $params);
113
                }
114
            }
115
116
            // Render using Symfony2 layouts see folder:
117
            // src/Chamilo/ThemeBundle/Resources/views/Layout
118
            return $this->render(
119
                $template,
120
                $params
121
            );
122
        } else {
123
            // Found does not exist
124
            throw new NotFoundHttpException();
125
        }
126
    }
127
128
    public function pluginAction($name, Request $request)
129
    {
130
        return $this->classicAction($name, $request, 'plugin');
131
    }
132
}
133