Completed
Push — master ( 4de0c7...bc53b5 )
by Jean-Christophe
06:49
created

JsUtils::getUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Ajax\php\ci;
3
4
class JsUtils extends \Ajax\JsUtils{
5
	protected $ci;
6
	protected $_my_controller_paths= array();
7
	protected $_my_controllers= array();
8
9
	public function __construct($params=array(),$injected=NULL){
10
		parent::__construct($params,$injected);
11
		$this->_my_controller_paths = array(APPPATH);
12
	}
13
	public function getUrl($url){
14
		return site_url($url);
15
	}
16
17
	public function getCi(){
18
		if(isset($this->ci)===false){
19
			$this->ci =& get_instance();
20
			$this->ci->load->helper('url');
21
		}
22
		return $this->ci;
23
	}
24
25
	public function addViewElement($identifier,$content,&$view){
26
		if(\array_key_exists("q", $view)===false){
27
			$view["q"]=array();
28
		}
29
		$view["q"][$identifier]=$content;
30
	}
31
32
	public function createScriptVariable(&$view,$view_var, $output){
33
		$view[$view_var]=$output;
34
	}
35
36
	public function forward($initialControllerInstance,$controllerName,$actionName,$params=NULL){
37
		$ci=$this->getCi();
38
		$controllerName=strtolower($controllerName);
39
		$this->controller($controllerName);
40
		\ob_start();
41
		$ci->{$controllerName}->{$actionName}($params);
42
		$result=ob_get_contents();
43
		\ob_end_clean();
44
		return $result;
45
	}
46
47
	public function renderContent($initialControllerInstance,$viewName, $params=NULL) {
48
		return $initialControllerInstance->load->view($viewName, $params, true);
49
	}
50
51
	public function fromDispatcher($dispatcher){
52
		return array_values($dispatcher->uri->segment_array());
53
	}
54
55
	public function controller($controller, $name = '', $db_conn = FALSE){
56
		if (is_array($controller)){
57
			foreach ($controller as $babe){
58
				$this->controller($babe);
59
			}
60
			return;
61
		}
62
		if ($controller == ''){
63
			return;
64
		}
65
		$path = '';
66
		// Is the controller in a sub-folder? If so, parse out the filename and path.
67
		if (($last_slash = strrpos($controller, '/')) !== FALSE){
68
			// The path is in front of the last slash
69
			$path = substr($controller, 0, $last_slash + 1);
70
			// And the controller name behind it
71
			$controller = substr($controller, $last_slash + 1);
72
		}
73
74
		if ($name == ''){
75
			$name = $controller;
76
		}
77
78
		if (in_array($name, $this->_my_controllers, TRUE)){
79
			return;
80
		}
81
82
		$CI =$this->getCi();
83
		if (isset($CI->$name)){
84
			show_error('The controller name you are loading is the name of a resource that is already being used: '.$name);
85
		}
86
		$controller = strtolower($controller);
87
		foreach ($this->_my_controller_paths as $mod_path){
88
			if ( ! file_exists($mod_path.'controllers/'.$path.$controller.'.php')){
89
				continue;
90
			}
91
			if ($db_conn !== FALSE AND ! class_exists('CI_DB')){
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
92
				if ($db_conn === TRUE){
93
					$db_conn = '';
94
				}
95
				$CI->load->database($db_conn, FALSE, TRUE);
96
			}
97
			if ( ! class_exists('CI_Controller')){
98
				load_class('Controller', 'core');
99
			}
100
			require_once($mod_path.'controllers/'.$path.$controller.'.php');
101
			$controller = ucfirst($controller);
102
			$CI->$name = new $controller();
103
104
			$this->_my_controllers[] = $name;
105
			return;
106
		}
107
		show_error('Unable to locate the controller you have specified: '.$controller);
108
	}
109
}