PageRenderTime 69ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/ontowiki/src/extensions/components/containermanager/ContainermanagerController.php

https://bitbucket.org/nihilus/ontowiki
PHP | 394 lines | 267 code | 49 blank | 78 comment | 75 complexity | e96077c74f2db311f65163d184b0be11 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. <?php
  2. /**
  3. * @category OntoWiki
  4. * @package OntoWiki_extensions_components_containermanager
  5. */
  6. require_once 'OntoWiki/Controller/Component.php';
  7. require_once 'OntoWiki/Module/Registry.php';
  8. /**
  9. * @category OntoWiki
  10. * @package OntoWiki_extensions_components_containermanager
  11. */
  12. class ContainermanagerController extends OntoWiki_Controller_Component
  13. {
  14. private $container;
  15. public function init()
  16. {
  17. parent::init();
  18. if ( (!isset($_SESSION['container'])) || ($_SESSION['container'] == null) ) {
  19. $_SESSION['container'] = array();
  20. }
  21. //if (!isset($_SESSION['container'])) {
  22. if (!isset($this->container)) {
  23. if($_SESSION['container']) {
  24. $this->container = $_SESSION['container'];
  25. $_SESSION['message'] = null;
  26. }
  27. else {
  28. $this->container = array();
  29. $_SESSION['container'] = $this->container;
  30. $_SESSION['message'] = null;
  31. }
  32. }
  33. //$this->_response->setBody($this->view->render('containermanager/box.phtml'));
  34. }
  35. public function __call($method, $args)
  36. {
  37. $this->_forward('view');
  38. }
  39. /**
  40. * Adds a new container
  41. */
  42. public function addcontainerAction() {
  43. $this->_helper->viewRenderer->setNoRender();
  44. $this->_helper->layout()->disableLayout();
  45. $url = $_SERVER['REQUEST_URI'];
  46. $name = strrchr ( $url, '=' );
  47. $name = substr($name, 1);
  48. $this->container = $_SESSION['container'];
  49. if(array_key_exists($name, $this->container)) {
  50. $this->_abort('The container you entered already exists.', OntoWiki_Message::ERROR);
  51. exit;
  52. }
  53. $this->container[$name] = array();
  54. ksort($this->container);
  55. $_SESSION['container'] = $this->container;
  56. $this->_response->setBody($this->view->render('containermanager/box.phtml'));
  57. }
  58. /**
  59. *Deletes the selected container.
  60. */
  61. public function deletecontainerAction() {
  62. $this->_helper->viewRenderer->setNoRender();
  63. $this->_helper->layout()->disableLayout();
  64. $this->container = $_SESSION['container'];
  65. $url = $_SERVER['REQUEST_URI'];
  66. $name = strrchr ( $url, '=' );
  67. $name = substr($name, 1);
  68. if(array_key_exists($name, $this->container)) {
  69. unset($this->container[$name]);
  70. $_SESSION['container'] = $this->container;
  71. if ($_SESSION['list'][$name]) unset($_SESSION['list'][$name]);
  72. $this->_response->setBody($this->view->render('containermanager/box.phtml'));
  73. } else {
  74. $this->_abort('The container you entered does not exists.', OntoWiki_Message::ERROR);
  75. exit;
  76. }
  77. }
  78. /**
  79. * Add a Ressource to the selected Container
  80. */
  81. public function addressourceAction() {
  82. $this->_helper->viewRenderer->setNoRender();
  83. $this->_helper->layout()->disableLayout();
  84. $this->container = $_SESSION['container'];
  85. $url = $_SERVER['REQUEST_URI'];
  86. $name = strstr ( $url, '=' );
  87. $name = substr($name, 1);
  88. $modeluri = $name;
  89. $name = substr_replace($name, '', strpos ( $name, '?' ));
  90. $modeluri = strstr($modeluri, '=');
  91. $modeluri = substr($modeluri, 1);
  92. $modeluri = substr_replace($modeluri, '', strpos ( $modeluri, '?' ));
  93. $r = strrchr($url, '=');
  94. $r = substr($r, 1);
  95. $title = 'hallo';
  96. $page = 2;
  97. if(!array_key_exists($name, $this->container)) {
  98. $this->_abort('The container you entered does not exists.', OntoWiki_Message::ERROR);
  99. exit;
  100. }
  101. if(strlen($name) == 0) {
  102. $this->_abort('No Container name was declared.', OntoWiki_Message::ERROR);
  103. exit;
  104. }
  105. if(strlen($modeluri) == 0) {
  106. $this->_abort('No Modeluri was declared.', OntoWiki_Message::ERROR);
  107. exit;
  108. }
  109. if(strlen($r) == 0) {
  110. $this->_abort('No Ressourceuri was declared.', OntoWiki_Message::ERROR);
  111. exit;
  112. }
  113. if($_SESSION['container'][$name] != null || isset($_SESSION['container'][$name])) {
  114. $this->add($modeluri, $r, $name, $this->container, $title);
  115. $this->pager($name, $page);
  116. //$this->view->render('list.php');
  117. }
  118. $this->_response->setBody($this->view->render('containermanager/list.phtml'));
  119. }
  120. /**
  121. * Removes a selected Ressource from the container
  122. */
  123. public function deleteressourceAction() {
  124. $this->_helper->viewRenderer->setNoRender();
  125. $this->_helper->layout()->disableLayout();
  126. $this->container = $_SESSION['container'];
  127. $url = $_SERVER['REQUEST_URI'];
  128. $name = strstr ( $url, '=' );
  129. $name = substr($name, 1);
  130. $modeluri = $name;
  131. $name = substr_replace($name, '', strpos ( $name, '?' ));
  132. $modeluri = strstr($modeluri, '=');
  133. $modeluri = substr($modeluri, 1);
  134. $modeluri = substr_replace($modeluri, '', strpos ( $modeluri, '?' ));
  135. $r = strrchr($url, '=');
  136. $r = substr($r, 1);
  137. $title = 'hallo';
  138. $page = 2;
  139. if(!array_key_exists($name, $this->container)) {
  140. $this->_abort('The container you entered does not exists.', OntoWiki_Message::ERROR);
  141. exit;
  142. }
  143. if(strlen($name) == 0) {
  144. $this->_abort('No Container name was declared.', OntoWiki_Message::ERROR);
  145. exit;
  146. }
  147. if(strlen($modeluri) == 0) {
  148. $this->_abort('No Modeluri was declared.', OntoWiki_Message::ERROR);
  149. exit;
  150. }
  151. if(strlen($r) == 0) {
  152. $this->_abort('No Ressourceuri was declared.', OntoWiki_Message::ERROR);
  153. exit;
  154. }
  155. if($modeluri == null || $r == null ){
  156. //$_SESSION['message'] = '<p class="message error">' . $this->_strings->cm->error->mod_res . '</p>';
  157. //$this->view->message = '<p class="message error">' . $this->_strings->cm->error->mod_res . '</p>';
  158. //header('HTTP/1.0 500 Internal Server Error');
  159. //echo $this -> view -> render('message.php');
  160. //$this->pager($name, $page);
  161. //$this->view->render('list.php');
  162. //exit();
  163. }
  164. for($i=0;$i<sizeof($this->container[$name]);$i++){
  165. if($this->container[$name][$i][0]==$r && $this->container[$name][$i][1]==$modeluri){
  166. unset($this->container[$name][$i]);
  167. break;
  168. }
  169. }
  170. $this->pager($name, $page);
  171. $_SESSION['container'] = $this->container;
  172. $this->_response->setBody($this->view->render('containermanager/list.phtml'));
  173. }
  174. public function listcontainerAction() {
  175. $this->_helper->viewRenderer->setNoRender();
  176. $this->_helper->layout()->disableLayout();
  177. $this->container = $_SESSION['container'];
  178. $url = $_SERVER['REQUEST_URI'];
  179. $name = strrchr ( $url, '=' );
  180. $name = substr($name, 1);
  181. // prepares the specified container in a separate array
  182. $_SESSION['list'] = array();
  183. $_SESSION['list'][$name] = $this->container[$name];
  184. $title = 'hallo';
  185. $page = 2;
  186. if(!array_key_exists($name, $this->container)) {
  187. $this->_abort('The container you entered does not exists.', OntoWiki_Message::ERROR);
  188. exit;
  189. }
  190. if($_SESSION['list'][$name] == null && ! array_key_exists( $name , $this->container ) ){
  191. //$_SESSION['message'] = '<p class="messagebox">' . $this->_strings->cm->error->entries . '</p>';
  192. //$this->view->message = '<p class="message error">' . $this->_strings->cm->error->entries . '</p>';
  193. //header('HTTP/1.0 400 Bad Request');
  194. //echo $this -> view -> render('message.php');
  195. //$this->pager($name, $page);
  196. //echo $this -> view -> render('list.php');
  197. //exit();
  198. }
  199. $this->pager($name, $page);
  200. $this->_response->setBody($this->view->render('containermanager/list.phtml'));
  201. }
  202. /**
  203. * Removes all ressources from the selected container.
  204. */
  205. public function clearcontainerAction() {
  206. $this->_helper->viewRenderer->setNoRender();
  207. $this->_helper->layout()->disableLayout();
  208. $this->container = $_SESSION['container'];
  209. $url = $_SERVER['REQUEST_URI'];
  210. $name = strrchr ( $url, '=' );
  211. $name = substr($name, 1);
  212. if(!array_key_exists($name, $this->container)) {
  213. $this->_abort('The container you entered does not exists.', OntoWiki_Message::ERROR);
  214. exit;
  215. }
  216. $this->container[$name] = array();
  217. $_SESSION['container'] = $this->container;
  218. if ($_SESSION['list'][$name]) $_SESSION['list'][$name] = array();
  219. $this->_response->setBody($this->view->render('containermanager/box.phtml'));
  220. }
  221. // generalizes the add actions
  222. private function add($modelUri, $r, $name, $container, $title){
  223. if ($modelUri == null || $r == null || $modelUri == 'undefined'|| $r == 'undefined' || $title == 'undefined' || $title == null){
  224. $this->_abort('No model or resource existing.', OntoWiki_Message::ERROR);
  225. //$_SESSION['message'] = '<p class="massage error">' . $this->_strings->cm->error->mod_res . '</p>';
  226. //$this->view->message = '<p class="massage error">' . $this->_strings->cm->error->mod_res . '</p>';
  227. //header('HTTP/1.0 500 Internal Server Error');
  228. //echo $this -> view -> render('message.php');
  229. return;
  230. }
  231. if (!array_key_exists($name, $container)) {
  232. $this->_abort('The container you entered ist not existing yet.', OntoWiki_Message::ERROR);
  233. //$_SESSION['message'] = '<p class="message error">' . $this->_strings->cm->error->nContainer . '</p>';
  234. //$this->view->message = '<p class="message error">' . $this->_strings->cm->error->nContainer . '</p>';
  235. //header('HTTP/1.0 400 Bad Request');
  236. //echo $this -> view -> render('message.php');
  237. return;
  238. }
  239. for($i=0;$i<sizeof($container[$name]);$i++){
  240. if($container[$name][$i][0]==$r && $container[$name][$i][1]==$modelUri){
  241. $this->_abort('This entry already exists in the choosen container.', OntoWiki_Message::ERROR);
  242. //$_SESSION['message'] = '<p class="message error">' . $this->_strings->cm->error->entry . '</p>';
  243. //$this->view->message = '<p class="message error">' . $this->_strings->cm->error->entry . '</p>';
  244. //header('HTTP/1.0 400 Bad Request');
  245. //echo $this -> view -> render('message.php');
  246. return;
  247. }
  248. }
  249. $container[$name][] = array($r, $modelUri, $title);
  250. asort($container[$name]);
  251. $_SESSION['container'] = $container;
  252. $_SESSION['list'] = array();
  253. $_SESSION['list'][$name] = $container[$name];
  254. if($_SESSION['list'][$name] == null && !(array_search(null, $container) == $name )){
  255. $this->_abort('Please check your entries.', OntoWiki_Message::ERROR);
  256. //$_SESSION['message'] = '<p class="messagebox">' . $this->_strings->cm->error->entries . '</p>';
  257. //$this->view->message = '<p class="messagebox">' . $this->_strings->cm->error->entries . '</p>';
  258. //header('HTTP/1.0 400 Bad Request');
  259. //echo $this -> view -> render('message.php');
  260. return;
  261. }
  262. }
  263. // pager
  264. function pager($name, $page){
  265. // page navigation
  266. // number of entries per page
  267. $max = 10;
  268. // number of pages
  269. $count_pages = ((count($_SESSION['container'][$name]))/$max);
  270. if ( isset($this->view->pagerLinks ) ) unset( $this->view->pagerLinks);
  271. // prepare an array for pager variables
  272. $this->view->pagerLinks = array();
  273. $this->view->pagerLinks['max'] = $max;
  274. $maxButtons = 10;
  275. $counter = 0;
  276. if (!isset($page) OR $page == 'undefined') {
  277. $page = 1;
  278. }
  279. // save the page number from the request objekt to pagerLinks array
  280. $this->view->pagerLinks['page'] = $page;
  281. // back links for side navi
  282. if ($page != 1) {
  283. $back = $page-1;
  284. //$this->view->pagerLinks['first'] = "<a class='pager' name='".$name."' id='1'>&lsaquo;&nbsp;" . $this->_strings->cm->page->first . "</a>";
  285. //$this->view->pagerLinks['back'] = "<a class='pager' name='".$name."' id='". $back ."'>&laquo;&nbsp;" . $this->_strings->cm->page->prev . "</a>";
  286. $counter += 2;
  287. }
  288. // forward links for side navi
  289. if ($page < $count_pages) {
  290. $forward = $page+1;
  291. //$this->view->pagerLinks['forward'] = "<a class='pager' name='".$name."' id='". $forward . "'>". $this->_strings->cm->page->next . "&nbsp;&raquo;</a>";
  292. //$this->view->pagerLinks['last'] = "<a class='pager' name='".$name."' id='". $count_pages ."'>". $this->_strings->cm->page->last . "&nbsp;&rsaquo;</a>";
  293. $counter += 2;
  294. }
  295. // all other displayed pages
  296. if($count_pages > 1) {
  297. $buffer = $maxButtons - $counter;
  298. for ($i = 1; $i < $count_pages+1; $i++) {
  299. if($i > $page-($buffer/2) && $i <= $page+($buffer/2)){
  300. //if($i == $page) $this->view->pagerLinks['pages'][$i] = "<a class='pager selected' name='".$name."' id='". $i ."'>" . $i . "</a>";
  301. //else {
  302. //$this->view->pagerLinks['pages'][$i] = "<a class='pager' name='".$name."' id='". $i ."'>" . $i . "</a>";
  303. //}
  304. }
  305. }
  306. }
  307. }
  308. function modelSort( $array ){
  309. $modelList = array();
  310. foreach($array as $key => $value){
  311. $modelURI = $value[1];
  312. $resourceURI = $value[0];
  313. if( array_key_exists($modelURI, $modelList)){
  314. array_push($modelList[$modelURI], $resourceURI);
  315. }
  316. else {
  317. $modelList[$modelURI] = array($resourceURI);
  318. }
  319. }
  320. return $modelList;
  321. }
  322. /**
  323. * Shortcut for adding messages
  324. */
  325. private function _abort($msg, $type = null, $redirect = null)
  326. {
  327. if (empty($type)) {
  328. $type = OntoWiki_Message::INFO;
  329. }
  330. $this->_owApp->appendMessage(
  331. new OntoWiki_Message(
  332. $msg ,
  333. $type
  334. )
  335. );
  336. if (empty($redirect)) {
  337. if ($redirect !== false) {
  338. $this->_redirect($this->_config->urlBase);
  339. }
  340. } else {
  341. $this->_redirect((string)$redirect);
  342. }
  343. return true;
  344. }
  345. }