Completed
Push — master ( 0afdc4...0e57e6 )
by Alexey
04:27
created

adminController::viewAction()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 19
nc 8
nop 2
dl 0
loc 23
rs 6.1403
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * App 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 adminController extends Controller {
12
13
  public function indexAction() {
14
    $args = func_get_args();
15
    call_user_func_array([$this, 'dataManagerAction'], $args);
16
  }
17
18
  public function dataManagerAction($model = '', $dataManager = 'manager') {
19
    if (!$model) {
20
      $modulePath = Module::getModulePath($this->module->moduleName);
21
      $path = $modulePath . '/models';
22
      if (file_exists($path)) {
23
        $files = array_slice(scandir($path), 2);
24
        foreach ($files as $file) {
25
          if (is_dir($path . '/' . $file)) {
26
            continue;
27
          }
28
          $model = pathinfo($file, PATHINFO_FILENAME);
29
          break;
30
        }
31
      }
32
    }
33
    $fullModelName = $this->module->moduleName . '\\' . ucfirst($model);
34
    $dataManager = new Ui\DataManager($fullModelName, $dataManager);
35
    $title = !empty($dataManager->managerOptions['name']) ? $dataManager->managerOptions['name'] : $fullModelName::objectName();
36
    $this->view->setTitle($title);
37
    $this->view->page(['module' => 'Ui', 'content' => 'dataManager/manager', 'data' => compact('dataManager')]);
38
  }
39
40
  public function viewAction($model, $pk) {
3 ignored issues
show
Coding Style introduced by
viewAction uses the super-global variable $_POST 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...
Coding Style introduced by
viewAction uses the super-global variable $_SERVER 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...
Coding Style introduced by
viewAction 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...
41
    $fullModelName = $this->module->moduleName . '\\' . ucfirst($model);
42
    if (Users\User::$cur->group_id != 3 && (empty($fullModelName::$views['manager']['access']) || !in_array(Users\User::$cur->group_id, $fullModelName::$views['manager']['access']['groups']))) {
43
      Tools::redirect('/admin', 'У вас нет прав доступа для просмотра этого объекта', 'danger');
44
    }
45
    $item = $fullModelName::get($pk);
46
    $this->view->setTitle(($fullModelName::$objectName ? $fullModelName::$objectName : $fullModelName) . ($item ? ( ' - ' . $item->name()) : ''));
47
    if (!empty($_POST['comment'])) {
48
      $comment = new Dashboard\Comment();
49
      $comment->text = $_POST['comment'];
50
      $comment->user_id = \Users\User::$cur->id;
51
      $comment->model = $fullModelName;
52
      $comment->item_id = $item->pk();
53
      $comment->save();
54
      Tools::redirect($_SERVER['REQUEST_URI']);
55
    }
56
    $moduleName = $this->module->moduleName;
57
    $pageParam = ['module' => 'Ui', 'content' => 'dataManager/view', 'data' => compact('item', 'moduleName')];
58
    if (isset($_GET['print'])) {
59
      $pageParam['page'] = 'print';
60
    }
61
    $this->view->page($pageParam);
62
  }
63
64
}
65