PageRenderTime 31ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/paginator/adapter/model.c

http://github.com/phalcon/cphalcon
C | 326 lines | 231 code | 57 blank | 38 comment | 32 complexity | d28140994aaf4caba845889437287468 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. +------------------------------------------------------------------------+
  3. | Phalcon Framework |
  4. +------------------------------------------------------------------------+
  5. | Copyright (c) 2011-2012 Phalcon Team (http://www.phalconphp.com) |
  6. +------------------------------------------------------------------------+
  7. | This source file is subject to the New BSD License that is bundled |
  8. | with this package in the file docs/LICENSE.txt. |
  9. | |
  10. | If you did not receive a copy of the license and are unable to |
  11. | obtain it through the world-wide-web, please send an email |
  12. | to license@phalconphp.com so we can send you a copy immediately. |
  13. +------------------------------------------------------------------------+
  14. | Authors: Andres Gutierrez <andres@phalconphp.com> |
  15. | Eduar Carvajal <eduar@phalconphp.com> |
  16. +------------------------------------------------------------------------+
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include "php.h"
  22. #include "php_phalcon.h"
  23. #include "phalcon.h"
  24. #include "Zend/zend_operators.h"
  25. #include "Zend/zend_exceptions.h"
  26. #include "Zend/zend_interfaces.h"
  27. #include "kernel/main.h"
  28. #include "kernel/memory.h"
  29. #include "kernel/object.h"
  30. #include "kernel/array.h"
  31. #include "kernel/fcall.h"
  32. #include "kernel/operators.h"
  33. /**
  34. * Phalcon_Paginator_Adapter_Model
  35. *
  36. * This adapter allows to paginate data using Phalcon_Model resultsets.
  37. *
  38. */
  39. /**
  40. * Phalcon_Paginator_Adapter_Model constructor
  41. *
  42. * @param array $config
  43. */
  44. PHP_METHOD(Phalcon_Paginator_Adapter_Model, __construct){
  45. zval *config = NULL;
  46. zval *r0 = NULL, *r1 = NULL;
  47. int eval_int;
  48. PHALCON_MM_GROW();
  49. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &config) == FAILURE) {
  50. PHALCON_MM_RESTORE();
  51. RETURN_NULL();
  52. }
  53. phalcon_update_property_zval(this_ptr, SL("_config"), config TSRMLS_CC);
  54. eval_int = phalcon_array_isset_string(config, SL("limit")+1);
  55. if (eval_int) {
  56. PHALCON_ALLOC_ZVAL_MM(r0);
  57. phalcon_array_fetch_string(&r0, config, SL("limit"), PHALCON_NOISY TSRMLS_CC);
  58. phalcon_update_property_zval(this_ptr, SL("_limitRows"), r0 TSRMLS_CC);
  59. }
  60. eval_int = phalcon_array_isset_string(config, SL("page")+1);
  61. if (eval_int) {
  62. PHALCON_ALLOC_ZVAL_MM(r1);
  63. phalcon_array_fetch_string(&r1, config, SL("page"), PHALCON_NOISY TSRMLS_CC);
  64. phalcon_update_property_zval(this_ptr, SL("_page"), r1 TSRMLS_CC);
  65. }
  66. PHALCON_MM_RESTORE();
  67. }
  68. /**
  69. * Set the current page number
  70. *
  71. * @param int $page
  72. */
  73. PHP_METHOD(Phalcon_Paginator_Adapter_Model, setCurrentPage){
  74. zval *page = NULL;
  75. PHALCON_MM_GROW();
  76. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &page) == FAILURE) {
  77. PHALCON_MM_RESTORE();
  78. RETURN_NULL();
  79. }
  80. phalcon_update_property_zval(this_ptr, SL("_page"), page TSRMLS_CC);
  81. PHALCON_MM_RESTORE();
  82. }
  83. /**
  84. * Returns a slice of the resultset to show in the pagination
  85. *
  86. * @return stdClass
  87. */
  88. PHP_METHOD(Phalcon_Paginator_Adapter_Model, getPaginate){
  89. zval *show = NULL, *items = NULL, *page_number = NULL, *n = NULL, *page = NULL, *start = NULL, *total_pages = NULL;
  90. zval *i = NULL, *next = NULL, *before = NULL;
  91. zval *t0 = NULL, *t1 = NULL, *t2 = NULL, *t3 = NULL, *t4 = NULL, *t5 = NULL, *t6 = NULL;
  92. zval *t7 = NULL, *t8 = NULL, *t9 = NULL, *t10 = NULL, *t11 = NULL, *t12 = NULL;
  93. zval *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *r4 = NULL, *r5 = NULL, *r6 = NULL;
  94. zval *r7 = NULL, *r8 = NULL, *r9 = NULL, *r10 = NULL, *r11 = NULL, *r12 = NULL, *r13 = NULL;
  95. zval *r14 = NULL, *r15 = NULL, *r16 = NULL, *r17 = NULL, *r18 = NULL, *r19 = NULL, *r20 = NULL;
  96. zval *r21 = NULL, *r22 = NULL, *r23 = NULL, *r24 = NULL, *r25 = NULL;
  97. zval *i0 = NULL;
  98. zval *a0 = NULL;
  99. zval *c0 = NULL;
  100. PHALCON_MM_GROW();
  101. PHALCON_ALLOC_ZVAL_MM(t0);
  102. phalcon_read_property(&t0, this_ptr, SL("_limitRows"), PHALCON_NOISY TSRMLS_CC);
  103. PHALCON_CPY_WRT(show, t0);
  104. PHALCON_ALLOC_ZVAL_MM(t1);
  105. phalcon_read_property(&t1, this_ptr, SL("_config"), PHALCON_NOISY TSRMLS_CC);
  106. PHALCON_ALLOC_ZVAL_MM(r0);
  107. phalcon_array_fetch_string(&r0, t1, SL("data"), PHALCON_NOISY TSRMLS_CC);
  108. PHALCON_CPY_WRT(items, r0);
  109. PHALCON_ALLOC_ZVAL_MM(t2);
  110. phalcon_read_property(&t2, this_ptr, SL("_page"), PHALCON_NOISY TSRMLS_CC);
  111. PHALCON_CPY_WRT(page_number, t2);
  112. if (Z_TYPE_P(page_number) == IS_NULL) {
  113. PHALCON_INIT_VAR(page_number);
  114. ZVAL_LONG(page_number, 1);
  115. }
  116. PHALCON_ALLOC_ZVAL_MM(r1);
  117. phalcon_fast_count(r1, items TSRMLS_CC);
  118. PHALCON_CPY_WRT(n, r1);
  119. PHALCON_ALLOC_ZVAL_MM(i0);
  120. object_init(i0);
  121. PHALCON_CPY_WRT(page, i0);
  122. PHALCON_INIT_VAR(t3);
  123. ZVAL_LONG(t3, 1);
  124. PHALCON_ALLOC_ZVAL_MM(r2);
  125. sub_function(r2, page_number, t3 TSRMLS_CC);
  126. PHALCON_ALLOC_ZVAL_MM(r3);
  127. mul_function(r3, show, r2 TSRMLS_CC);
  128. PHALCON_CPY_WRT(start, r3);
  129. PHALCON_INIT_VAR(t4);
  130. ZVAL_LONG(t4, 1);
  131. PHALCON_ALLOC_ZVAL_MM(r4);
  132. sub_function(r4, n, t4 TSRMLS_CC);
  133. PHALCON_ALLOC_ZVAL_MM(r5);
  134. div_function(r5, r4, show TSRMLS_CC);
  135. PHALCON_INIT_VAR(total_pages);
  136. PHALCON_CALL_FUNC_PARAMS_1(total_pages, "ceil", r5);
  137. if (Z_TYPE_P(items) != IS_OBJECT) {
  138. PHALCON_THROW_EXCEPTION_STR(phalcon_paginator_exception_ce, "Invalid data for paginator");
  139. return;
  140. }
  141. if (Z_TYPE_P(page_number) == IS_NULL) {
  142. PHALCON_INIT_VAR(page_number);
  143. ZVAL_LONG(page_number, 0);
  144. }
  145. PHALCON_INIT_VAR(t5);
  146. ZVAL_LONG(t5, 0);
  147. PHALCON_INIT_VAR(r6);
  148. is_smaller_function(r6, start, t5 TSRMLS_CC);
  149. if (zend_is_true(r6)) {
  150. PHALCON_THROW_EXCEPTION_STR(phalcon_paginator_exception_ce, "The start page number is zero or less");
  151. return;
  152. }
  153. PHALCON_INIT_VAR(a0);
  154. array_init(a0);
  155. phalcon_update_property_zval(page, SL("items"), a0 TSRMLS_CC);
  156. PHALCON_INIT_VAR(t6);
  157. ZVAL_LONG(t6, 0);
  158. PHALCON_INIT_VAR(r7);
  159. is_smaller_function(r7, t6, n TSRMLS_CC);
  160. if (zend_is_true(r7)) {
  161. PHALCON_INIT_VAR(r8);
  162. is_smaller_or_equal_function(r8, start, n TSRMLS_CC);
  163. if (zend_is_true(r8)) {
  164. PHALCON_CALL_METHOD_PARAMS_1_NORETURN(items, "seek", start, PHALCON_NO_CHECK);
  165. } else {
  166. PHALCON_INIT_VAR(c0);
  167. ZVAL_LONG(c0, 1);
  168. PHALCON_CALL_METHOD_PARAMS_1_NORETURN(items, "seek", c0, PHALCON_NO_CHECK);
  169. PHALCON_INIT_VAR(page_number);
  170. ZVAL_LONG(page_number, 1);
  171. }
  172. PHALCON_INIT_VAR(i);
  173. ZVAL_LONG(i, 1);
  174. ws_e435_0:
  175. PHALCON_INIT_VAR(r9);
  176. PHALCON_CALL_METHOD(r9, items, "valid", PHALCON_NO_CHECK);
  177. if (Z_TYPE_P(r9) != IS_BOOL || (Z_TYPE_P(r9) == IS_BOOL && !Z_BVAL_P(r9))) {
  178. goto we_e435_0;
  179. }
  180. PHALCON_INIT_VAR(r10);
  181. PHALCON_CALL_METHOD(r10, items, "current", PHALCON_NO_CHECK);
  182. PHALCON_INIT_VAR(t7);
  183. phalcon_read_property(&t7, page, SL("items"), PHALCON_NOISY TSRMLS_CC);
  184. phalcon_array_append(&t7, r10, PHALCON_NO_SEPARATE_THX TSRMLS_CC);
  185. phalcon_update_property_zval(page, SL("items"), t7 TSRMLS_CC);
  186. PHALCON_INIT_VAR(r11);
  187. is_smaller_or_equal_function(r11, show, i TSRMLS_CC);
  188. if (zend_is_true(r11)) {
  189. goto we_e435_0;
  190. }
  191. PHALCON_SEPARATE(i);
  192. increment_function(i);
  193. goto ws_e435_0;
  194. we_e435_0:
  195. if(0){}
  196. }
  197. phalcon_update_property_long(page, SL("first"), 1 TSRMLS_CC);
  198. PHALCON_ALLOC_ZVAL_MM(r12);
  199. phalcon_add_function(r12, start, show TSRMLS_CC);
  200. PHALCON_INIT_VAR(r13);
  201. is_smaller_function(r13, r12, n TSRMLS_CC);
  202. if (zend_is_true(r13)) {
  203. PHALCON_INIT_VAR(t8);
  204. ZVAL_LONG(t8, 1);
  205. PHALCON_ALLOC_ZVAL_MM(r14);
  206. phalcon_add_function(r14, page_number, t8 TSRMLS_CC);
  207. PHALCON_CPY_WRT(next, r14);
  208. } else {
  209. PHALCON_ALLOC_ZVAL_MM(r15);
  210. phalcon_add_function(r15, start, show TSRMLS_CC);
  211. PHALCON_ALLOC_ZVAL_MM(r16);
  212. is_equal_function(r16, r15, n TSRMLS_CC);
  213. if (zend_is_true(r16)) {
  214. PHALCON_CPY_WRT(next, n);
  215. } else {
  216. PHALCON_ALLOC_ZVAL_MM(r17);
  217. div_function(r17, n, show TSRMLS_CC);
  218. PHALCON_INIT_VAR(next);
  219. PHALCON_CALL_FUNC_PARAMS_1(next, "intval", r17);
  220. PHALCON_INIT_VAR(t9);
  221. ZVAL_LONG(t9, 1);
  222. PHALCON_ALLOC_ZVAL_MM(r18);
  223. phalcon_add_function(r18, next, t9 TSRMLS_CC);
  224. PHALCON_CPY_WRT(next, r18);
  225. }
  226. }
  227. PHALCON_INIT_VAR(r19);
  228. is_smaller_function(r19, total_pages, next TSRMLS_CC);
  229. if (zend_is_true(r19)) {
  230. PHALCON_CPY_WRT(next, total_pages);
  231. }
  232. phalcon_update_property_zval(page, SL("next"), next TSRMLS_CC);
  233. PHALCON_INIT_VAR(t10);
  234. ZVAL_LONG(t10, 1);
  235. PHALCON_INIT_VAR(r20);
  236. is_smaller_function(r20, t10, page_number TSRMLS_CC);
  237. if (zend_is_true(r20)) {
  238. PHALCON_INIT_VAR(t11);
  239. ZVAL_LONG(t11, 1);
  240. PHALCON_ALLOC_ZVAL_MM(r21);
  241. sub_function(r21, page_number, t11 TSRMLS_CC);
  242. PHALCON_CPY_WRT(before, r21);
  243. } else {
  244. PHALCON_INIT_VAR(before);
  245. ZVAL_LONG(before, 1);
  246. }
  247. phalcon_update_property_zval(page, SL("before"), before TSRMLS_CC);
  248. phalcon_update_property_zval(page, SL("current"), page_number TSRMLS_CC);
  249. PHALCON_ALLOC_ZVAL_MM(r22);
  250. mod_function(r22, n, show TSRMLS_CC);
  251. if (zend_is_true(r22)) {
  252. PHALCON_ALLOC_ZVAL_MM(r23);
  253. div_function(r23, n, show TSRMLS_CC);
  254. PHALCON_INIT_VAR(total_pages);
  255. PHALCON_CALL_FUNC_PARAMS_1(total_pages, "intval", r23);
  256. PHALCON_INIT_VAR(t12);
  257. ZVAL_LONG(t12, 1);
  258. PHALCON_ALLOC_ZVAL_MM(r24);
  259. phalcon_add_function(r24, total_pages, t12 TSRMLS_CC);
  260. PHALCON_CPY_WRT(total_pages, r24);
  261. } else {
  262. PHALCON_ALLOC_ZVAL_MM(r25);
  263. div_function(r25, n, show TSRMLS_CC);
  264. PHALCON_CPY_WRT(total_pages, r25);
  265. }
  266. phalcon_update_property_zval(page, SL("last"), total_pages TSRMLS_CC);
  267. phalcon_update_property_zval(page, SL("total_pages"), total_pages TSRMLS_CC);
  268. RETURN_CTOR(page);
  269. }