Skip to content

Commit bc53b5d

Browse files
committed
codeIgniter class (library)
1 parent 4de0c79 commit bc53b5d

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

Ajax/php/ci/JsUtils.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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')){
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+
}

0 commit comments

Comments
 (0)