excel.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Vtiful Extension |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2017-2017 The Viest |
  6. +----------------------------------------------------------------------+
  7. | http://www.viest.me |
  8. +----------------------------------------------------------------------+
  9. | Author: viest <[email protected]> |
  10. +----------------------------------------------------------------------+
  11. */
  12. #ifdef HAVE_CONFIG_H
  13. #include "config.h"
  14. #endif
  15. #include "include.h"
  16. zend_class_entry *vtiful_excel_ce;
  17. static excel_resource_t *excel_res;
  18. /* {{{ ARG_INFO
  19. */
  20. ZEND_BEGIN_ARG_INFO_EX(excel_construct_arginfo, 0, 0, 1)
  21. ZEND_ARG_INFO(0, config)
  22. ZEND_END_ARG_INFO()
  23. ZEND_BEGIN_ARG_INFO_EX(excel_file_name_arginfo, 0, 0, 1)
  24. ZEND_ARG_INFO(0, file_name)
  25. ZEND_END_ARG_INFO()
  26. ZEND_BEGIN_ARG_INFO_EX(excel_header_arginfo, 0, 0, 1)
  27. ZEND_ARG_INFO(0, header)
  28. ZEND_END_ARG_INFO()
  29. ZEND_BEGIN_ARG_INFO_EX(excel_data_arginfo, 0, 0, 1)
  30. ZEND_ARG_INFO(0, data)
  31. ZEND_END_ARG_INFO()
  32. ZEND_BEGIN_ARG_INFO_EX(excel_insert_text_arginfo, 0, 0, 4)
  33. ZEND_ARG_INFO(0, row)
  34. ZEND_ARG_INFO(0, column)
  35. ZEND_ARG_INFO(0, data)
  36. ZEND_ARG_INFO(0, format)
  37. ZEND_END_ARG_INFO()
  38. ZEND_BEGIN_ARG_INFO_EX(excel_insert_image_arginfo, 0, 0, 3)
  39. ZEND_ARG_INFO(0, row)
  40. ZEND_ARG_INFO(0, column)
  41. ZEND_ARG_INFO(0, image)
  42. ZEND_END_ARG_INFO()
  43. ZEND_BEGIN_ARG_INFO_EX(excel_insert_formula_arginfo, 0, 0, 3)
  44. ZEND_ARG_INFO(0, row)
  45. ZEND_ARG_INFO(0, column)
  46. ZEND_ARG_INFO(0, formula)
  47. ZEND_END_ARG_INFO()
  48. ZEND_BEGIN_ARG_INFO_EX(excel_auto_filter_arginfo, 0, 0, 1)
  49. ZEND_ARG_INFO(0, range)
  50. ZEND_END_ARG_INFO()
  51. ZEND_BEGIN_ARG_INFO_EX(excel_merge_cells_arginfo, 0, 0, 2)
  52. ZEND_ARG_INFO(0, range)
  53. ZEND_ARG_INFO(0, data)
  54. ZEND_END_ARG_INFO()
  55. ZEND_BEGIN_ARG_INFO_EX(excel_set_column_arginfo, 0, 0, 3)
  56. ZEND_ARG_INFO(0, format_handle)
  57. ZEND_ARG_INFO(0, range)
  58. ZEND_ARG_INFO(0, width)
  59. ZEND_END_ARG_INFO()
  60. ZEND_BEGIN_ARG_INFO_EX(excel_set_row_arginfo, 0, 0, 3)
  61. ZEND_ARG_INFO(0, format_handle)
  62. ZEND_ARG_INFO(0, range)
  63. ZEND_ARG_INFO(0, height)
  64. ZEND_END_ARG_INFO()
  65. /* }}} */
  66. /** {{{ \Vtiful\Kernel\Excel::__construct(array $config)
  67. */
  68. PHP_METHOD(vtiful_excel, __construct)
  69. {
  70. zval *config, *c_path;
  71. ZEND_PARSE_PARAMETERS_START(1, 1)
  72. Z_PARAM_ARRAY(config)
  73. ZEND_PARSE_PARAMETERS_END();
  74. if((c_path = zend_hash_str_find(Z_ARRVAL_P(config), ZEND_STRL(V_EXCEL_PAT))) == NULL)
  75. {
  76. zend_throw_exception(vtiful_exception_ce, "Lack of 'path' configuration", 110);
  77. return;
  78. }
  79. if(Z_TYPE_P(c_path) != IS_STRING)
  80. {
  81. zend_throw_exception(vtiful_exception_ce, "Configure 'path' must be a string type", 120);
  82. return;
  83. }
  84. add_property_zval(getThis(), V_EXCEL_COF, config);
  85. }
  86. /* }}} */
  87. /** {{{ \Vtiful\Kernel\Excel::filename(string $fileName)
  88. */
  89. PHP_METHOD(vtiful_excel, fileName)
  90. {
  91. zval file_path, *dir_path;
  92. zend_string *file_name;
  93. ZEND_PARSE_PARAMETERS_START(1, 1)
  94. Z_PARAM_STR(file_name)
  95. ZEND_PARSE_PARAMETERS_END();
  96. ZVAL_COPY(return_value, getThis());
  97. GET_CONFIG_PATH(dir_path, vtiful_excel_ce, return_value);
  98. excel_file_path(file_name, dir_path, &file_path);
  99. excel_res = malloc(sizeof(excel_resource_t));
  100. excel_res->workbook = workbook_new(Z_STRVAL(file_path));
  101. excel_res->worksheet = workbook_add_worksheet(excel_res->workbook, NULL);
  102. add_property_zval(return_value, V_EXCEL_FIL, &file_path);
  103. add_property_resource(return_value, V_EXCEL_HANDLE, zend_register_resource(excel_res, le_excel_writer));
  104. zval_ptr_dtor(&file_path);
  105. }
  106. /* }}} */
  107. /** {{{ \Vtiful\Kernel\Excel::constMemory(string $fileName)
  108. */
  109. PHP_METHOD(vtiful_excel, constMemory)
  110. {
  111. zval file_path, *dir_path;
  112. zend_string *file_name;
  113. ZEND_PARSE_PARAMETERS_START(1, 1)
  114. Z_PARAM_STR(file_name)
  115. ZEND_PARSE_PARAMETERS_END();
  116. ZVAL_COPY(return_value, getThis());
  117. GET_CONFIG_PATH(dir_path, vtiful_excel_ce, return_value);
  118. excel_file_path(file_name, dir_path, &file_path);
  119. lxw_workbook_options options = {.constant_memory = LXW_TRUE, .tmpdir = NULL};
  120. excel_res = malloc(sizeof(excel_resource_t));
  121. excel_res->workbook = workbook_new_opt(Z_STRVAL(file_path), &options);
  122. excel_res->worksheet = workbook_add_worksheet(excel_res->workbook, NULL);
  123. add_property_zval(return_value, V_EXCEL_FIL, &file_path);
  124. add_property_resource(return_value, V_EXCEL_HANDLE, zend_register_resource(excel_res, le_excel_writer));
  125. zval_ptr_dtor(&file_path);
  126. }
  127. /* }}} */
  128. /** {{{ \Vtiful\Kernel\Excel::header(array $header)
  129. */
  130. PHP_METHOD(vtiful_excel, header)
  131. {
  132. zval *header, *header_value;
  133. zend_long header_l_key;
  134. ZEND_PARSE_PARAMETERS_START(1, 1)
  135. Z_PARAM_ARRAY(header)
  136. ZEND_PARSE_PARAMETERS_END();
  137. ZVAL_COPY(return_value, getThis());
  138. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(header), header_l_key, header_value) {
  139. type_writer(header_value, 0, header_l_key, excel_res, NULL);
  140. zval_ptr_dtor(header_value);
  141. } ZEND_HASH_FOREACH_END();
  142. add_property_resource(return_value, V_EXCEL_HANDLE, zend_register_resource(excel_res, le_excel_writer));
  143. }
  144. /* }}} */
  145. /** {{{ \Vtiful\Kernel\Excel::data(array $data)
  146. */
  147. PHP_METHOD(vtiful_excel, data)
  148. {
  149. zval *data, *data_r_value, *data_l_value;
  150. zend_long data_r_key, data_l_key;
  151. ZEND_PARSE_PARAMETERS_START(1, 1)
  152. Z_PARAM_ARRAY(data)
  153. ZEND_PARSE_PARAMETERS_END();
  154. ZVAL_COPY(return_value, getThis());
  155. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data), data_r_key, data_r_value) {
  156. if(Z_TYPE_P(data_r_value) == IS_ARRAY) {
  157. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data_r_value), data_l_key, data_l_value) {
  158. type_writer(data_l_value, data_r_key+1, data_l_key, excel_res, NULL);
  159. zval_ptr_dtor(data_l_value);
  160. } ZEND_HASH_FOREACH_END();
  161. }
  162. } ZEND_HASH_FOREACH_END();
  163. add_property_resource(return_value, V_EXCEL_HANDLE, zend_register_resource(excel_res, le_excel_writer));
  164. }
  165. /* }}} */
  166. /** {{{ \Vtiful\Kernel\Excel::output()
  167. */
  168. PHP_METHOD(vtiful_excel, output)
  169. {
  170. zval rv, *handle, *file_path;
  171. handle = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  172. file_path = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_FIL), 0, &rv TSRMLS_DC);
  173. workbook_file(excel_res, handle);
  174. free(excel_res);
  175. add_property_null(getThis(), V_EXCEL_HANDLE);
  176. add_property_null(getThis(), V_EXCEL_PAT);
  177. ZVAL_COPY(return_value, file_path);
  178. }
  179. /* }}} */
  180. /** {{{ \Vtiful\Kernel\Excel::getHandle()
  181. */
  182. PHP_METHOD(vtiful_excel, getHandle)
  183. {
  184. zval rv;
  185. zval *handle;
  186. handle = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  187. ZVAL_COPY(return_value, handle);
  188. }
  189. /* }}} */
  190. /** {{{ \Vtiful\Kernel\Excel::insertText(int $row, int $column, string|int|double $data)
  191. */
  192. PHP_METHOD(vtiful_excel, insertText)
  193. {
  194. zval res_handle;
  195. zval *data;
  196. zend_long row, column;
  197. zend_string *format = NULL;
  198. ZEND_PARSE_PARAMETERS_START(3, 4)
  199. Z_PARAM_LONG(row)
  200. Z_PARAM_LONG(column)
  201. Z_PARAM_ZVAL(data)
  202. Z_PARAM_OPTIONAL
  203. Z_PARAM_STR(format)
  204. ZEND_PARSE_PARAMETERS_END();
  205. ZVAL_COPY(return_value, getThis());
  206. type_writer(data, row, column, excel_res, format);
  207. ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
  208. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  209. }
  210. /* }}} */
  211. /** {{{ \Vtiful\Kernel\Excel::insertImage(int $row, int $column, string $imagePath)
  212. */
  213. PHP_METHOD(vtiful_excel, insertImage)
  214. {
  215. zval res_handle;
  216. zval *image;
  217. zend_long row, column;
  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. image_writer(image, row, column, excel_res);
  225. ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
  226. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  227. }
  228. /* }}} */
  229. /** {{{ \Vtiful\Kernel\Excel::insertImage(int $row, int $column, string $imagePath)
  230. */
  231. PHP_METHOD(vtiful_excel, insertFormula)
  232. {
  233. zval res_handle;
  234. zval *formula;
  235. zend_long row, column;
  236. ZEND_PARSE_PARAMETERS_START(3, 3)
  237. Z_PARAM_LONG(row)
  238. Z_PARAM_LONG(column)
  239. Z_PARAM_ZVAL(formula)
  240. ZEND_PARSE_PARAMETERS_END();
  241. ZVAL_COPY(return_value, getThis());
  242. formula_writer(formula, row, column, excel_res);
  243. ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
  244. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  245. }
  246. /* }}} */
  247. /** {{{ \Vtiful\Kernel\Excel::autoFilter(int $rowStart, int $rowEnd, int $columnStart, int $columnEnd)
  248. */
  249. PHP_METHOD(vtiful_excel, autoFilter)
  250. {
  251. zval res_handle;
  252. zend_string *range;
  253. ZEND_PARSE_PARAMETERS_START(1, 1)
  254. Z_PARAM_STR(range)
  255. ZEND_PARSE_PARAMETERS_END();
  256. ZVAL_COPY(return_value, getThis());
  257. auto_filter(range, excel_res);
  258. ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
  259. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  260. }
  261. /* }}} */
  262. /** {{{ \Vtiful\Kernel\Excel::mergeCells(string $range, string $data)
  263. */
  264. PHP_METHOD(vtiful_excel, mergeCells)
  265. {
  266. zval res_handle;
  267. zend_string *range, *data;
  268. ZEND_PARSE_PARAMETERS_START(2, 2)
  269. Z_PARAM_STR(range)
  270. Z_PARAM_STR(data)
  271. ZEND_PARSE_PARAMETERS_END();
  272. ZVAL_COPY(return_value, getThis());
  273. merge_cells(range, data, excel_res);
  274. ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
  275. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  276. }
  277. /* }}} */
  278. /** {{{ \Vtiful\Kernel\Excel::setColumn(resource $format, string $range [, int $width])
  279. */
  280. PHP_METHOD(vtiful_excel, setColumn)
  281. {
  282. zval *format_handle, res_handle;
  283. zend_string *range;
  284. double width = 0;
  285. int argc = ZEND_NUM_ARGS();
  286. ZEND_PARSE_PARAMETERS_START(2, 3)
  287. Z_PARAM_RESOURCE(format_handle)
  288. Z_PARAM_STR(range)
  289. Z_PARAM_OPTIONAL
  290. Z_PARAM_DOUBLE(width)
  291. ZEND_PARSE_PARAMETERS_END();
  292. ZVAL_COPY(return_value, getThis());
  293. if (argc == 2) {
  294. width = 10;
  295. }
  296. set_column(range, width, excel_res, zval_get_format(format_handle));
  297. ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
  298. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  299. }
  300. /* }}} */
  301. /** {{{ \Vtiful\Kernel\Excel::setRow(resource $format, string $range [, int $heitght])
  302. */
  303. PHP_METHOD(vtiful_excel, setRow)
  304. {
  305. zval *format_handle, res_handle;
  306. zend_string *range;
  307. double height = 0;
  308. int argc = ZEND_NUM_ARGS();
  309. ZEND_PARSE_PARAMETERS_START(2, 3)
  310. Z_PARAM_RESOURCE(format_handle)
  311. Z_PARAM_STR(range)
  312. Z_PARAM_OPTIONAL
  313. Z_PARAM_DOUBLE(height)
  314. ZEND_PARSE_PARAMETERS_END();
  315. ZVAL_COPY(return_value, getThis());
  316. if (argc == 2) {
  317. height = 18;
  318. }
  319. set_row(range, height, excel_res, zval_get_format(format_handle));
  320. ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
  321. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  322. }
  323. /* }}} */
  324. /** {{{ excel_methods
  325. */
  326. zend_function_entry excel_methods[] = {
  327. PHP_ME(vtiful_excel, __construct, excel_construct_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  328. PHP_ME(vtiful_excel, fileName, excel_file_name_arginfo, ZEND_ACC_PUBLIC)
  329. PHP_ME(vtiful_excel, constMemory, excel_file_name_arginfo, ZEND_ACC_PUBLIC)
  330. PHP_ME(vtiful_excel, header, excel_header_arginfo, ZEND_ACC_PUBLIC)
  331. PHP_ME(vtiful_excel, data, excel_data_arginfo, ZEND_ACC_PUBLIC)
  332. PHP_ME(vtiful_excel, output, NULL, ZEND_ACC_PUBLIC)
  333. PHP_ME(vtiful_excel, getHandle, NULL, ZEND_ACC_PUBLIC)
  334. PHP_ME(vtiful_excel, autoFilter, excel_auto_filter_arginfo, ZEND_ACC_PUBLIC)
  335. PHP_ME(vtiful_excel, insertText, excel_insert_text_arginfo, ZEND_ACC_PUBLIC)
  336. PHP_ME(vtiful_excel, insertImage, excel_insert_image_arginfo, ZEND_ACC_PUBLIC)
  337. PHP_ME(vtiful_excel, insertFormula, excel_insert_formula_arginfo, ZEND_ACC_PUBLIC)
  338. PHP_ME(vtiful_excel, mergeCells, excel_merge_cells_arginfo, ZEND_ACC_PUBLIC)
  339. PHP_ME(vtiful_excel, setColumn, excel_set_column_arginfo, ZEND_ACC_PUBLIC)
  340. PHP_ME(vtiful_excel, setRow, excel_set_row_arginfo, ZEND_ACC_PUBLIC)
  341. PHP_FE_END
  342. };
  343. /* }}} */
  344. /** {{{ VTIFUL_STARTUP_FUNCTION
  345. */
  346. VTIFUL_STARTUP_FUNCTION(excel) {
  347. zend_class_entry ce;
  348. INIT_NS_CLASS_ENTRY(ce, "Vtiful\\Kernel", "Excel", excel_methods);
  349. vtiful_excel_ce = zend_register_internal_class(&ce);
  350. return SUCCESS;
  351. }
  352. /* }}} */