PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions.ext/containermanager/ContainermanagerController.php

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