PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/lib/Views/AdminViews/Resource/Listing_1.php

https://gitlab.com/gothcon/cthulhu
PHP | 310 lines | 256 code | 47 blank | 7 comment | 13 complexity | 6333e9744ce03338b6b7332aed9693e8 MD5 | raw file
  1. <?php
  2. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR."../../../Views/HtmlListView.php");;
  3. class ListingView extends HtmlListView{
  4. protected $sleepingStatistics;
  5. public function init(){
  6. $this->resourceList = $this->model->getResourceList();
  7. // $this->headerLink_available_for_sleeping = $this->internalLink("resource",$this->getHeaderLink("available_for_sleeping"),false);
  8. $this->sleepingStatistics = $this->getModel()->getSleepingStatistics();
  9. }
  10. public function render(){?>
  11. <?php $r=0; ?>
  12. <table class="resourceList">
  13. <tr class="even">
  14. <th class="idCol"><a href="?<?php $this->printHeaderLink("id");?>"><?php echo $this->translate("resources/id");?></a></th>
  15. <th class="nameCol"><a href="?<?php $this->printHeaderLink("name")?>"><?php echo $this->translate("resources/name");?></a></th>
  16. <th class="typeCol"><a href="?<?php $this->printHeaderLink("type")?>"><?php echo $this->translate("resources/type");?></a></th>
  17. <th class="typeCol"><a href="?<?php $this->printHeaderLink("available")?>"><?php echo $this->translate("resources/isAvailable");?></a></th>
  18. <th class="typeCol"><?php echo $this->translate("resources/availableForSleeping");?></th>
  19. <th class="typeCol"><?php echo $this->translate("resources/noOfSleepingSlots");?></th>
  20. <th class="typeCol"><?php echo $this->translate("resources/noOfFreeSleepingSlots");?></th>
  21. <?php foreach($this->resourceList as $resource){
  22. $resourceType = $resource->resourceType;
  23. $resourceLink = $this->internalLink("resource",$resource->id,false);
  24. $resourceTypeLink = $this->internalLink("resourceType",$resourceType->id,false);
  25. $availableForSleeping = isset($this->sleepingStatistics[$resource->id]) && $this->sleepingStatistics[$resource->id]->is_available;
  26. $noOfSleepingSlots = isset($this->sleepingStatistics[$resource->id]) ? $this->sleepingStatistics[$resource->id]->no_of_slots : "-";
  27. $noOfFreeSleepingSlots = isset($this->sleepingStatistics[$resource->id]) ? $this->sleepingStatistics[$resource->id]->free_slots : "-";
  28. ?>
  29. <tr class="<?php echo $r++ % 2 ? "even" : "odd";?>">
  30. <td><a href="<?php echo $resourceLink; ?>"><?php psafe($resource->id)?></a></td>
  31. <td><a href="<?php echo $resourceLink; ?>"><?php psafe($resource->name)?></a></td>
  32. <td><a href="<?php echo $resourceTypeLink; ?>"><?php psafe($resourceType->name);?></a></td>
  33. <td><a href="<?php echo $resourceLink; ?>"><?php echo $resource->available ? $this->translate("general/yes") : $this->translate("general/no");?></a></td>
  34. <td><a href="<?php echo $resourceLink; ?>"><?php echo $availableForSleeping ? $this->translate("general/yes") : $this->translate("general/no");?></a></td>
  35. <td><?php psafe($noOfSleepingSlots)?></td>
  36. <td><?php psafe($noOfFreeSleepingSlots)?></td>
  37. </tr>
  38. <?php } ?>
  39. </table>
  40. <script type="text/javascript">
  41. (function($){
  42. var defaultOptions = {
  43. saveUrl : "<?php $this->internalLink("resource","{0}/save?format=json");?>",
  44. newUrl : "<?php $this->internalLink("resource","new?format=json");?>",
  45. deleteUrl : "<?php $this->internalLink("resource","{0}/delete?format=json");?>",
  46. typeListUrl : "<?php $this->internalLink("resource","?format=json");?>"
  47. }
  48. $.fn.resourceList = function(optionsParam){
  49. var $this = $(this);
  50. if($this.length < 1){
  51. return $this;
  52. }
  53. var options = $.extend({},defaultOptions,optionsParam);
  54. var resourceTypes = [];
  55. var typeIdToNameMapping = [];
  56. function doAjaxSave(requestUrl,data,callback){
  57. $.ajax({
  58. url: requestUrl,
  59. dataType: 'json',
  60. type : 'POST',
  61. data: data,
  62. success: callback
  63. });
  64. }
  65. function doAjaxDelete(requestUrl,resourceId,callback){
  66. $.ajax({
  67. url: requestUrl,
  68. dataType: 'json',
  69. type : 'POST',
  70. data: { "id" : resourceId},
  71. success: callback
  72. });
  73. }
  74. function transformRows($table){
  75. $table.find("tr").each(function(key,row){
  76. if(key == 0){
  77. return true;
  78. }
  79. $tr = $(row);
  80. $idCell = $($tr.children()[0]);
  81. $nameCell = $($tr.children()[1]);
  82. $typeCell = $($tr.children()[2]);
  83. $buttonCell = $($tr.children()[3]);
  84. var $idLink = $($idCell.find("a")[0]);
  85. var id = $idLink.text();
  86. var $idSpan = $("<span class='idSpan'>"+id+"</span>");
  87. $idLink.replaceWith($idSpan);
  88. var $nameLink = $($nameCell.find("a")[0]);
  89. var name = $nameLink.text();
  90. var $nameSpan = $("<span class='nameSpan'>"+name+"</span>");
  91. $nameLink.replaceWith($nameSpan);
  92. var $typeLink = $($typeCell.find("a")[0]);
  93. var type = $typeLink.text();
  94. var $typeSpan = $("<span class='typeSpan'>"+type+"</span>");
  95. $typeLink.replaceWith($typeSpan);
  96. var $editButton = $($buttonCell.find(".editButton")[0]).removeProp("click");
  97. var $deleteButton = $($buttonCell.find(".deleteButton")[0]).hide().removeProp("click");
  98. var $saveButton = createSaveButton().hide().insertAfter($editButton);
  99. var $abortButton = createAbortButton().insertAfter($saveButton).hide();
  100. var $nameInput = createNameInput(name).appendTo($nameCell).hide();
  101. var $typeSelect = createTypeSelect(resourceTypes,type).appendTo($typeCell).hide();
  102. var leaveEditState = function(){
  103. $deleteButton.hide();
  104. $abortButton.hide();
  105. $saveButton.hide();
  106. $editButton.show();
  107. $nameInput.hide();
  108. $nameSpan.show();
  109. $typeSelect.hide();
  110. $typeSpan.show();
  111. }
  112. var enterEditState = function(){
  113. $editButton.hide();
  114. $abortButton.show();
  115. $saveButton.show();
  116. $deleteButton.show();
  117. $nameSpan.hide();
  118. $nameInput.show();
  119. $typeSelect.show();
  120. $typeSpan.hide();
  121. }
  122. $abortButton.click(function(event){
  123. event.stopPropagation();
  124. event.preventDefault();
  125. leaveEditState();
  126. return false;
  127. });
  128. $editButton.click(function(event){
  129. event.stopPropagation();
  130. event.preventDefault();
  131. enterEditState();
  132. return false;
  133. });
  134. $deleteButton.click(createDeleteButtonClick($tr,id));
  135. $saveButton.click(createSaveButtonClick($tr,$.translate("general/save"),function(data){
  136. $nameSpan.text(data.data.name);
  137. $idSpan.text(data.data.id);
  138. $typeSpan.text(getTypeFromId(data.data.resource_type_id).name);
  139. leaveEditState();
  140. log(data.message);
  141. }));
  142. var onRowMouseOver = function(){
  143. $nameSpan.hide();
  144. }
  145. });
  146. }
  147. function createSaveButton(label){
  148. label = label || $.translate("general/save");
  149. var $saveButton = $("<a href='#spara' class='submitButtonLink saveButton'>" + label + "<span class='end'></span></a>");
  150. return $saveButton;
  151. }
  152. function createAbortButton(){
  153. label = $.translate("general/abort");
  154. var $button = $("<a href='#avbryt' class='submitButtonLink abortButton'>" + label + "<span class='end'></span></a>");
  155. return $button;
  156. }
  157. function createDeleteButton(){
  158. var $deleteButton = $("<a href='#' class='submitButtonLink deleteButton'>"+ $.translate("general/delete")+"<span class='end'></span></a>");
  159. return $deleteButton;
  160. }
  161. function createNameInput(name){
  162. name = name || "";
  163. return $("<input type='text' name='name' class='nameInput' value='"+name+"'/>")
  164. }
  165. function createTypeSelect(typeList,selectedValue){
  166. var $select = $("<select class='typeSelect'></select>");
  167. for(key in typeList){
  168. $option = $("<option value='"+ typeList[key].id +"'>"+ typeList[key].name+"</option>");
  169. if(selectedValue == typeList[key].name){
  170. $option.attr("selected","selected");
  171. }
  172. $select.append($option);
  173. }
  174. return $select;
  175. }
  176. function getData($tr){
  177. var id = $($tr.find(".idSpan")[0]).text();
  178. var name = $($tr.find(".nameInput")[0]).val();
  179. var type = $($tr.find(".typeSelect")[0]).val();
  180. var data = {"id" : id, "name" : name, "resource_type_id" : type};
  181. return data;
  182. }
  183. function createSaveButtonClick($tr,operation,ajaxCallback){
  184. var id = $($tr.find(".idSpan")).text();
  185. operation = operation || "save";
  186. return function(event){
  187. if(operation == "new"){
  188. saveUrl = options.newUrl;
  189. }else{
  190. saveUrl = options.saveUrl.replace("{0}",id);
  191. }
  192. doAjaxSave(saveUrl,getData($tr),ajaxCallback);
  193. event.stopPropagation();
  194. event.preventDefault();
  195. return false;
  196. };
  197. }
  198. function createNewButtonClick($table){
  199. return function(event){
  200. var $ajaxCallback = function(data){
  201. // create row
  202. $tr = $("<tr>");
  203. $td1 = $("<td>");
  204. $td2 = $("<td>");
  205. $td3 = $("<td>");
  206. $table.append($tr.append($td1).append($td2).append($td3));
  207. // create the input field and name textspan
  208. $nameInput = $("<input type='text' value='"+ data.data.name +"'>");
  209. $nameSpan = $("<span>"+data.data.name+"</span>");
  210. // create the save button
  211. $saveButton = createSaveButton($.translate("general/save"));
  212. $saveButton.click(createSaveButtonClick($nameInput));
  213. // create the delete button
  214. $deleteButton = createDeleteButton()
  215. // create the click callback
  216. $deleteButtonClick = createDeleteButtonClick($tr,id);
  217. $table.append($tr.append($td1).append($td2.append($td2input)).append($td3.append($td2submit)));
  218. $table.append("<tr></tr>");
  219. }
  220. };
  221. }
  222. function createDeleteButtonClick($tr,typeId){
  223. var ajaxCallback = function(data){
  224. $tr.remove();
  225. log(data.data.message);
  226. }
  227. var deleteUrl = options.deleteUrl.replace("{0}",typeId);
  228. return function(event){
  229. doAjaxDelete(deleteUrl,typeId,ajaxCallback);
  230. event.stopPropagation();
  231. event.preventDefault();
  232. return false;
  233. };
  234. }
  235. function log(string){
  236. console.log(string);
  237. }
  238. function getTypeFromId(id){
  239. return resourceTypes[typeIdToNameMapping[id]];
  240. }
  241. function init(data){
  242. resourceTypes = data.data;
  243. for(key in resourceTypes){
  244. var id = resourceTypes[key].id;
  245. typeIdToNameMapping[id] = key;
  246. }
  247. $this.each(function(key,obj){
  248. var $table = $(obj);
  249. transformRows($table);
  250. });
  251. }
  252. $.ajax({
  253. url: options.typeListUrl,
  254. dataType: 'json',
  255. type : 'POST',
  256. success: init
  257. });
  258. };
  259. })(jQuery);
  260. // $(".resourceList").resourceList();
  261. </script>
  262. <?php }
  263. }