excel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Vtiful Extension |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2017-2017 The Viest |
  6. +----------------------------------------------------------------------+
  7. | http://www.vtiful.com |
  8. +----------------------------------------------------------------------+
  9. | Author: viest <[email protected]> |
  10. +----------------------------------------------------------------------+
  11. */
  12. #ifdef HAVE_CONFIG_H
  13. #include "config.h"
  14. #endif
  15. #include "zend.h"
  16. #include "zend_API.h"
  17. #include "zend_exceptions.h"
  18. #include "php.h"
  19. #include "php_vtiful.h"
  20. #include "excel.h"
  21. #include "exception.h"
  22. #include "write.h"
  23. zend_class_entry *vtiful_excel_ce;
  24. ZEND_BEGIN_ARG_INFO_EX(excel_construct_arginfo, 0, 0, 1)
  25. ZEND_ARG_INFO(0, config)
  26. ZEND_END_ARG_INFO()
  27. ZEND_BEGIN_ARG_INFO_EX(excel_file_name_arginfo, 0, 0, 1)
  28. ZEND_ARG_INFO(0, file_name)
  29. ZEND_END_ARG_INFO()
  30. ZEND_BEGIN_ARG_INFO_EX(excel_header_arginfo, 0, 0, 1)
  31. ZEND_ARG_INFO(0, header)
  32. ZEND_END_ARG_INFO()
  33. ZEND_BEGIN_ARG_INFO_EX(excel_data_arginfo, 0, 0, 1)
  34. ZEND_ARG_INFO(0, data)
  35. ZEND_END_ARG_INFO()
  36. ZEND_BEGIN_ARG_INFO_EX(excel_insert_text_arginfo, 0, 0, 3)
  37. ZEND_ARG_INFO(0, row)
  38. ZEND_ARG_INFO(0, column)
  39. ZEND_ARG_INFO(0, data)
  40. ZEND_END_ARG_INFO()
  41. ZEND_BEGIN_ARG_INFO_EX(excel_insert_image_arginfo, 0, 0, 3)
  42. ZEND_ARG_INFO(0, row)
  43. ZEND_ARG_INFO(0, column)
  44. ZEND_ARG_INFO(0, image)
  45. ZEND_END_ARG_INFO()
  46. ZEND_BEGIN_ARG_INFO_EX(excel_insert_formula_arginfo, 0, 0, 3)
  47. ZEND_ARG_INFO(0, row)
  48. ZEND_ARG_INFO(0, column)
  49. ZEND_ARG_INFO(0, formula)
  50. ZEND_END_ARG_INFO()
  51. ZEND_BEGIN_ARG_INFO_EX(excel_auto_filter_arginfo, 0, 0, 1)
  52. ZEND_ARG_INFO(0, range)
  53. ZEND_END_ARG_INFO()
  54. excel_resource_t * zval_get_resource(zval *handle)
  55. {
  56. excel_resource_t *res;
  57. if((res = (excel_resource_t *)zend_fetch_resource(Z_RES_P(handle), VTIFUL_RESOURCE_NAME, le_vtiful)) == NULL) {
  58. zend_throw_exception(vtiful_exception_ce, "Excel resources resolution fail", 210);
  59. }
  60. return res;
  61. }
  62. /* {{{ \Vtiful\Kernel\Excel::__construct(array $config)
  63. */
  64. PHP_METHOD(vtiful_excel, __construct)
  65. {
  66. zval *config;
  67. zend_string *key;
  68. ZEND_PARSE_PARAMETERS_START(1, 1)
  69. Z_PARAM_ARRAY(config)
  70. ZEND_PARSE_PARAMETERS_END();
  71. key = zend_string_init(V_EXCEL_PAT, sizeof(V_EXCEL_PAT)-1, 0);
  72. if(zend_hash_find(Z_ARRVAL_P(config), key) == NULL)
  73. {
  74. zend_throw_exception(vtiful_exception_ce, "Lack of 'path' configuration", 110);
  75. }
  76. zend_update_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_COF), config);
  77. zend_string_release(key);
  78. }
  79. /* }}} */
  80. /* {{{ \Vtiful\Kernel\Excel::filename(string $fileName)
  81. */
  82. PHP_METHOD(vtiful_excel, fileName)
  83. {
  84. zval rv, tmp_file_name, file_path, handle, *config, *tmp_path;
  85. zend_string *file_name, *key;
  86. excel_resource_t *res;
  87. ZEND_PARSE_PARAMETERS_START(1, 1)
  88. Z_PARAM_STR(file_name)
  89. ZEND_PARSE_PARAMETERS_END();
  90. ZVAL_COPY(return_value, getThis());
  91. key = zend_string_init(V_EXCEL_PAT, sizeof(V_EXCEL_PAT)-1, 0);
  92. config = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_COF), 0, &rv TSRMLS_DC);
  93. tmp_path = zend_hash_find(Z_ARRVAL_P(config), key);
  94. zend_string_release(key);
  95. if(!tmp_path && Z_TYPE_P(tmp_path) != IS_STRING)
  96. {
  97. zend_throw_exception(vtiful_exception_ce, "Configure 'path' must be a string type", 120);
  98. }
  99. ZVAL_STR(&tmp_file_name, file_name);
  100. concat_function(&file_path, tmp_path, &tmp_file_name);
  101. res = emalloc(sizeof(excel_resource_t));
  102. res->workbook = workbook_new(ZSTR_VAL(zval_get_string(&file_path)));
  103. res->worksheet = workbook_add_worksheet(res->workbook, NULL);
  104. zval_ptr_dtor(&file_path);
  105. ZVAL_RES(&handle, zend_register_resource(res, le_vtiful));
  106. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_FIL), &file_path);
  107. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &handle);
  108. zval_ptr_dtor(&file_path);
  109. }
  110. /* }}} */
  111. /* {{{ \Vtiful\Kernel\Excel::header(array $header)
  112. */
  113. PHP_METHOD(vtiful_excel, header)
  114. {
  115. zval rv, res_handle, *attr_handle, *header, *header_value;
  116. zend_long header_l_key;
  117. excel_resource_t *res;
  118. ZEND_PARSE_PARAMETERS_START(1, 1)
  119. Z_PARAM_ARRAY(header)
  120. ZEND_PARSE_PARAMETERS_END();
  121. ZVAL_COPY(return_value, getThis());
  122. attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  123. if((res = (excel_resource_t *)zend_fetch_resource(Z_RES_P(attr_handle), VTIFUL_RESOURCE_NAME, le_vtiful)) == NULL) {
  124. zend_throw_exception(vtiful_exception_ce, "Excel resources resolution fail", 210);
  125. }
  126. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(header), header_l_key, header_value) {
  127. type_writer(header_value, 0, header_l_key, res);
  128. zval_ptr_dtor(header_value);
  129. } ZEND_HASH_FOREACH_END();
  130. ZVAL_RES(&res_handle, zend_register_resource(res, le_vtiful));
  131. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  132. }
  133. /* }}} */
  134. /* {{{ \Vtiful\Kernel\Excel::data(array $data)
  135. */
  136. PHP_METHOD(vtiful_excel, data)
  137. {
  138. zval rv, *data, *attr_handle, res_handle, *data_r_value, *data_l_value;
  139. zend_long data_r_key, data_l_key;
  140. excel_resource_t *res;
  141. ZEND_PARSE_PARAMETERS_START(1, 1)
  142. Z_PARAM_ARRAY(data)
  143. ZEND_PARSE_PARAMETERS_END();
  144. ZVAL_COPY(return_value, getThis());
  145. attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  146. if((res = (excel_resource_t *)zend_fetch_resource(Z_RES_P(attr_handle), VTIFUL_RESOURCE_NAME, le_vtiful)) == NULL) {
  147. zend_throw_exception(vtiful_exception_ce, "Excel resources resolution fail", 210);
  148. }
  149. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data), data_r_key, data_r_value) {
  150. if(Z_TYPE_P(data_r_value) == IS_ARRAY) {
  151. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data_r_value), data_l_key, data_l_value) {
  152. type_writer(data_l_value, data_r_key+1, data_l_key, res);
  153. zval_ptr_dtor(data_l_value);
  154. } ZEND_HASH_FOREACH_END();
  155. }
  156. } ZEND_HASH_FOREACH_END();
  157. ZVAL_RES(&res_handle, zend_register_resource(res, le_vtiful));
  158. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  159. }
  160. /* }}} */
  161. /* {{{ \Vtiful\Kernel\Excel::output()
  162. */
  163. PHP_METHOD(vtiful_excel, output)
  164. {
  165. zval rv, *handle, null_handle;
  166. excel_resource_t *res;
  167. handle = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  168. if((res = (excel_resource_t *)zend_fetch_resource(Z_RES_P(handle), VTIFUL_RESOURCE_NAME, le_vtiful)) == NULL) {
  169. zend_throw_exception(vtiful_exception_ce, "Excel resources resolution fail", 210);
  170. }
  171. workbook_file(res, handle);
  172. efree(res);
  173. ZVAL_NULL(&null_handle);
  174. zend_update_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HANDLE), &null_handle);
  175. }
  176. /* }}} */
  177. /* {{{ \Vtiful\Kernel\Excel::getHandle()
  178. */
  179. PHP_METHOD(vtiful_excel, getHandle)
  180. {
  181. zval rv;
  182. zval *handle;
  183. handle = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  184. ZVAL_COPY(return_value, handle);
  185. }
  186. /* }}} */
  187. /* {{{ \Vtiful\Kernel\Excel::insertText(int $row, int $column, string|int|double $data)
  188. */
  189. PHP_METHOD(vtiful_excel, insertText)
  190. {
  191. zval rv, res_handle;
  192. zval *attr_handle, *data;
  193. zend_long *row, *column;
  194. excel_resource_t *res;
  195. ZEND_PARSE_PARAMETERS_START(3, 3)
  196. Z_PARAM_LONG(row)
  197. Z_PARAM_LONG(column)
  198. Z_PARAM_ZVAL(data)
  199. ZEND_PARSE_PARAMETERS_END();
  200. ZVAL_COPY(return_value, getThis());
  201. attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  202. if((res = (excel_resource_t *)zend_fetch_resource(Z_RES_P(attr_handle), VTIFUL_RESOURCE_NAME, le_vtiful)) == NULL) {
  203. zend_throw_exception(vtiful_exception_ce, "Excel resources resolution fail", 210);
  204. }
  205. type_writer(data, row, column, res);
  206. ZVAL_RES(&res_handle, zend_register_resource(res, le_vtiful));
  207. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  208. }
  209. /* }}} */
  210. /* {{{ \Vtiful\Kernel\Excel::insertImage(int $row, int $column, string $imagePath)
  211. */
  212. PHP_METHOD(vtiful_excel, insertImage)
  213. {
  214. zval rv, res_handle;
  215. zval *attr_handle, *image;
  216. zend_long *row, *column;
  217. excel_resource_t *res;
  218. ZEND_PARSE_PARAMETERS_START(3, 3)
  219. Z_PARAM_LONG(row)
  220. Z_PARAM_LONG(column)
  221. Z_PARAM_ZVAL(image)
  222. ZEND_PARSE_PARAMETERS_END();
  223. ZVAL_COPY(return_value, getThis());
  224. attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  225. if((res = (excel_resource_t *)zend_fetch_resource(Z_RES_P(attr_handle), VTIFUL_RESOURCE_NAME, le_vtiful)) == NULL) {
  226. zend_throw_exception(vtiful_exception_ce, "Excel resources resolution fail", 210);
  227. }
  228. image_writer(image, row, column, res);
  229. ZVAL_RES(&res_handle, zend_register_resource(res, le_vtiful));
  230. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  231. }
  232. /* }}} */
  233. /* {{{ \Vtiful\Kernel\Excel::insertImage(int $row, int $column, string $imagePath)
  234. */
  235. PHP_METHOD(vtiful_excel, insertFormula)
  236. {
  237. zval rv, res_handle;
  238. zval *attr_handle, *formula;
  239. zend_long *row, *column;
  240. excel_resource_t *res;
  241. ZEND_PARSE_PARAMETERS_START(3, 3)
  242. Z_PARAM_LONG(row)
  243. Z_PARAM_LONG(column)
  244. Z_PARAM_ZVAL(formula)
  245. ZEND_PARSE_PARAMETERS_END();
  246. ZVAL_COPY(return_value, getThis());
  247. attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  248. if((res = (excel_resource_t *)zend_fetch_resource(Z_RES_P(attr_handle), VTIFUL_RESOURCE_NAME, le_vtiful)) == NULL) {
  249. zend_throw_exception(vtiful_exception_ce, "Excel resources resolution fail", 210);
  250. }
  251. formula_writer(formula, row, column, res);
  252. ZVAL_RES(&res_handle, zend_register_resource(res, le_vtiful));
  253. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  254. }
  255. /* }}} */
  256. /* {{{ \Vtiful\Kernel\Excel::autoFilter(int $rowStart, int $rowEnd, int $columnStart, int $columnEnd)
  257. */
  258. PHP_METHOD(vtiful_excel, autoFilter)
  259. {
  260. zval rv, res_handle;
  261. zval *attr_handle;
  262. zend_string *range;
  263. excel_resource_t *res;
  264. ZEND_PARSE_PARAMETERS_START(1, 1)
  265. Z_PARAM_STR(range)
  266. ZEND_PARSE_PARAMETERS_END();
  267. ZVAL_COPY(return_value, getThis());
  268. attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  269. res = zval_get_resource(attr_handle);
  270. auto_filter(range, res);
  271. ZVAL_RES(&res_handle, zend_register_resource(res, le_vtiful));
  272. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  273. }
  274. /* }}} */
  275. zend_function_entry excel_methods[] = {
  276. PHP_ME(vtiful_excel, __construct, excel_construct_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  277. PHP_ME(vtiful_excel, fileName, excel_file_name_arginfo, ZEND_ACC_PUBLIC)
  278. PHP_ME(vtiful_excel, header, excel_header_arginfo, ZEND_ACC_PUBLIC)
  279. PHP_ME(vtiful_excel, data, excel_data_arginfo, ZEND_ACC_PUBLIC)
  280. PHP_ME(vtiful_excel, output, NULL, ZEND_ACC_PUBLIC)
  281. PHP_ME(vtiful_excel, getHandle, NULL, ZEND_ACC_PUBLIC)
  282. PHP_ME(vtiful_excel, autoFilter, excel_auto_filter_arginfo, ZEND_ACC_PUBLIC)
  283. PHP_ME(vtiful_excel, insertText, excel_insert_text_arginfo, ZEND_ACC_PUBLIC)
  284. PHP_ME(vtiful_excel, insertImage, excel_insert_image_arginfo, ZEND_ACC_PUBLIC)
  285. PHP_ME(vtiful_excel, insertFormula, excel_insert_formula_arginfo, ZEND_ACC_PUBLIC)
  286. PHP_FE_END
  287. };
  288. VTIFUL_STARTUP_FUNCTION(excel) {
  289. zend_class_entry ce;
  290. INIT_NS_CLASS_ENTRY(ce, "Vtiful\\Kernel", "Excel", excel_methods);
  291. vtiful_excel_ce = zend_register_internal_class(&ce);
  292. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_COF), ZEND_ACC_PRIVATE);
  293. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_FIL), ZEND_ACC_PRIVATE);
  294. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_HANDLE), ZEND_ACC_PRIVATE);
  295. return SUCCESS;
  296. }