PageRenderTime 30ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/php/lib/Gloo/MySQL/Widget.php

http://webgloo.googlecode.com/
PHP | 286 lines | 165 code | 57 blank | 64 comment | 17 complexity | b2484b2b602ae5d24ba20905b9255b0a MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @author rajeevj
  5. * class to deal with widgets inside a block.
  6. *
  7. */
  8. class Gloo_MySQL_Widget {
  9. const MODULE_NAME = 'Gloo_MySQL_Widget';
  10. static function getPageWidgetsInBlock($mysqli,$orgId,$pageKey,$blockNo) {
  11. $orgId = $mysqli->real_escape_string($orgId);
  12. $pageKey = $mysqli->real_escape_string($pageKey);
  13. $blockNo = $mysqli->real_escape_string($blockNo);
  14. $sql = " select * from gloo_block_data where org_id = ".$orgId." and block_no = " .$blockNo ;
  15. $sql .= " and page_key = '" .$pageKey. "' order by ui_order " ;
  16. $rows = Gloo_MySQL_Helper::fetchRows($mysqli, $sql);
  17. return $rows ;
  18. }
  19. static function getOnId($mysqli,$orgId,$widgetId) {
  20. $orgId = $mysqli->real_escape_string($orgId);
  21. $widgetId = $mysqli->real_escape_string($widgetId);
  22. $sql = " select * from gloo_block_data where org_id = ".$orgId ." and id=".$widgetId ;
  23. $row = Gloo_MySQL_Helper::fetchRow($mysqli, $sql);
  24. return $row ;
  25. }
  26. static function update(
  27. $mysqli,
  28. $orgId,
  29. $blockNo,
  30. $widgetId,
  31. $title,
  32. $widgetXml,
  33. $widgetNote,
  34. $widgetCode,
  35. $widgetHtml) {
  36. $sql = " update gloo_block_data set updated_on=now(),title= ? ,widget_xml = ? ,widget_markdown = ? " ;
  37. $sql .= " , widget_code = ?, widget_html = ? where id = ? and org_id = ? and block_no = ? " ;
  38. $stmt = $mysqli->prepare($sql);
  39. //returns FALSE if prepare flopped
  40. if($stmt) {
  41. $stmt->bind_param("sssssiii",
  42. $title,
  43. $widgetXml,
  44. $widgetNote,
  45. $widgetCode,
  46. $widgetHtml,
  47. $widgetId,
  48. $orgId,
  49. $blockNo);
  50. $stmt->execute();
  51. if($mysqli->affected_rows != 1) {
  52. Gloo_MySQL_Error::handle(self::MODULE_NAME, $stmt);
  53. }
  54. $stmt->close();
  55. } else {
  56. Gloo_MySQL_Error::handle(self::MODULE_NAME, $mysqli);
  57. }
  58. }
  59. static function updateWidgetXml($mysqli,$orgId,$widgetId,$widgetXml) {
  60. $sql = " update gloo_block_data set updated_on=now(), widget_xml = ? " ;
  61. $sql .= " where id = ? and org_id = ? " ;
  62. $stmt = $mysqli->prepare($sql);
  63. //returns FALSE if prepare flopped
  64. if($stmt) {
  65. $stmt->bind_param("sii",
  66. $widgetXml,
  67. $widgetId,
  68. $orgId);
  69. $stmt->execute();
  70. if($mysqli->affected_rows != 1) {
  71. Gloo_MySQL_Error::handle(self::MODULE_NAME, $stmt);
  72. }
  73. $stmt->close();
  74. } else {
  75. Gloo_MySQL_Error::handle(self::MODULE_NAME, $mysqli);
  76. }
  77. }
  78. static function changeWidgetPositionInBlock(
  79. $mysqli,
  80. $orgId,
  81. $pageKey,
  82. $blockNo,
  83. $widgetId,
  84. $oldPosition,
  85. $newPosition) {
  86. /*
  87. * --------------------
  88. * Algorithm:
  89. * --------------------
  90. *
  91. * widgets are arranged within a block in increasing ui_order, the lowest
  92. * ui_order widget appears on top of block. Like,
  93. *
  94. * ui_order 1
  95. * ui_order 2
  96. * ui_order 3
  97. * ui_order 4
  98. * ...........
  99. * ui_order 7
  100. *
  101. *
  102. * old > new means Move Up
  103. * if old position = 7 and new position = 4 that means a move up operation
  104. * Move up: 7 => 4 operation
  105. *
  106. * Left = new position = 4
  107. * Right = old position = 7
  108. *
  109. * Everything between L and R , ui_order := ui_order + 1
  110. *
  111. * 4 =>5, 5=>6 ,6 =>7, 7 =>8 | 1,2,3 untouched , hole at 4
  112. * move widget ( old 7=> now at 8) to Hole at 4 (new position)
  113. *
  114. *
  115. * Old < New means Move Down
  116. * Move down 4 => 7 operation
  117. *
  118. * Left = old position = 4
  119. * Right = new position = 7
  120. * Everything between L and R , ui_order := ui_order-1
  121. *
  122. * 7=>6 , 6 =>5 , 5 =>4 , 4 => 3 | 1,2,3 untouched
  123. * (Hole at 7, 2 widgets at position 3 now )
  124. * move widget (old 4 => now at 3) to hole at 7 (new position)
  125. *
  126. *
  127. *
  128. */
  129. //old > new is Move up
  130. $left = $newPosition;
  131. $right = $oldPosition ;
  132. $sql = " update gloo_block_data set ui_order = ui_order + 1 where org_id = ? " ;
  133. $sql .= " and page_key = ? and block_no = ? and ( ui_order >= ? and ui_order <= ? ) " ;
  134. if($oldPosition < $newPosition ) {
  135. // old < new is down operation
  136. // left = old, right = new
  137. $left = $oldPosition ;
  138. $right = $newPosition ;
  139. //Move down
  140. $sql = " update gloo_block_data set ui_order = ui_order - 1 where org_id = ? " ;
  141. $sql .= " and page_key = ? and block_no = ? and ( ui_order >= ? and ui_order <= ? ) " ;
  142. }
  143. $stmt = $mysqli->prepare($sql);
  144. //returns FALSE if prepare flopped
  145. if($stmt) {
  146. $stmt->bind_param("isiii",
  147. $orgId,
  148. $pageKey,
  149. $blockNo,
  150. $left,
  151. $right);
  152. $stmt->execute();
  153. $stmt->close();
  154. } else {
  155. Gloo_MySQL_Error::handle(self::MODULE_NAME, $mysqli);
  156. }
  157. $newPosition = $mysqli->real_escape_string($newPosition);
  158. $widgetId = $mysqli->real_escape_string($widgetId);
  159. $sql = " update gloo_block_data set ui_order = ".$newPosition. " where id = ".$widgetId ;
  160. Gloo_MySQL_Helper::executeSQL($mysqli, $sql);
  161. }
  162. static function moveWidgetToNewBlock( $mysqli,
  163. $orgId,
  164. $oldPageKey,
  165. $newPageKey,
  166. $oldBlockNo,
  167. $newBlockNo,
  168. $widgetId,
  169. $oldPosition,
  170. $newPosition,
  171. $newBlockType) {
  172. //1. create new hole in target block
  173. //everything > insert position := position +1
  174. $sql = " update gloo_block_data set ui_order = ui_order+1 where org_id = ? " ;
  175. $sql .= " and page_key = ? and block_no = ? and ui_order >= ? " ;
  176. $stmt = $mysqli->prepare($sql);
  177. if($stmt) {
  178. $stmt->bind_param("isii",
  179. $orgId,
  180. $newPageKey,
  181. $newBlockNo,
  182. $newPosition);
  183. $stmt->execute();
  184. $stmt->close();
  185. } else {
  186. trigger_error('mysql error >> '. $mysqli->errno.'::'.$mysqli->error,E_USER_ERROR);
  187. }
  188. //2. Move widget to target block and target position
  189. $sql = " update gloo_block_data set ui_order = ? , block_no = ?, page_key = ?, block_type =? " ;
  190. $sql .= " where id= ? and org_id = ? and page_key = ? and block_no = ? " ;
  191. $stmt = $mysqli->prepare($sql);
  192. $insertPosition = $newPosition ;
  193. //returns FALSE if prepare flopped
  194. if($stmt) {
  195. $stmt->bind_param("iisiiisi",
  196. $insertPosition ,
  197. $newBlockNo,
  198. $newPageKey,
  199. $newBlockType,
  200. $widgetId,
  201. $orgId,
  202. $oldPageKey,
  203. $oldBlockNo);
  204. $stmt->execute();
  205. $stmt->close();
  206. } else {
  207. Gloo_MySQL_Error::handle(self::MODULE_NAME, $mysqli);
  208. }
  209. //3. create a hole in old block
  210. // position := position -1 for (all position > oldPosition )
  211. // 1,2,3,4 => 2 moved => 1,2(3),3(4)
  212. $sql = " update gloo_block_data set ui_order = ui_order-1 where org_id = ? " ;
  213. $sql .= " and page_key = ? and block_no = ? and ui_order > ? " ;
  214. $stmt = $mysqli->prepare($sql);
  215. //returns FALSE if prepare flopped
  216. if($stmt) {
  217. $stmt->bind_param("isii",
  218. $orgId,
  219. $oldPageKey,
  220. $oldBlockNo,
  221. $oldPosition);
  222. $stmt->execute();
  223. $stmt->close();
  224. } else {
  225. Gloo_MySQL_Error::handle(self::MODULE_NAME, $mysqli);
  226. }
  227. }
  228. }
  229. ?>