excel.c 14 KB

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