excel.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. #include "ext/standard/php_var.h"
  17. zend_class_entry *vtiful_excel_ce;
  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), V_EXCEL_PAT, sizeof(V_EXCEL_PAT)-1)) == 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. zend_update_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_COF), config);
  85. }
  86. /* }}} */
  87. /** {{{ \Vtiful\Kernel\Excel::filename(string $fileName)
  88. */
  89. PHP_METHOD(vtiful_excel, fileName)
  90. {
  91. zval rv, file_path, handle, *config, *tmp_path;
  92. zend_string *file_name, *full_path, *zstr_path;
  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. config = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_COF), 0, &rv TSRMLS_DC);
  98. tmp_path = zend_hash_str_find(Z_ARRVAL_P(config), V_EXCEL_PAT, sizeof(V_EXCEL_PAT)-1);
  99. zstr_path = zval_get_string(tmp_path);
  100. if (Z_STRVAL_P(tmp_path)[Z_STRLEN_P(tmp_path)-1] == '/') {
  101. full_path = zend_string_extend(zstr_path, ZSTR_LEN(zstr_path) + ZSTR_LEN(file_name), 0);
  102. memcpy(ZSTR_VAL(full_path)+ZSTR_LEN(zstr_path), ZSTR_VAL(file_name), ZSTR_LEN(file_name)+1);
  103. } else {
  104. full_path = zend_string_extend(zstr_path, ZSTR_LEN(zstr_path) + ZSTR_LEN(file_name) + 1, 0);
  105. ZSTR_VAL(full_path)[ZSTR_LEN(zstr_path)] ='/';
  106. memcpy(ZSTR_VAL(full_path)+ZSTR_LEN(zstr_path)+1, ZSTR_VAL(file_name), ZSTR_LEN(file_name)+1);
  107. }
  108. ZVAL_STR(&file_path, full_path);
  109. excel_res->workbook = workbook_new(Z_STRVAL(file_path));
  110. excel_res->worksheet = workbook_add_worksheet(excel_res->workbook, NULL);
  111. ZVAL_RES(&handle, zend_register_resource(excel_res, le_excel_writer));
  112. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_FIL), &file_path);
  113. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &handle);
  114. zval_ptr_dtor(&file_path);
  115. }
  116. /* }}} */
  117. /** {{{ \Vtiful\Kernel\Excel::header(array $header)
  118. */
  119. PHP_METHOD(vtiful_excel, header)
  120. {
  121. zval rv, res_handle, *header, *header_value;
  122. zend_long header_l_key;
  123. ZEND_PARSE_PARAMETERS_START(1, 1)
  124. Z_PARAM_ARRAY(header)
  125. ZEND_PARSE_PARAMETERS_END();
  126. ZVAL_COPY(return_value, getThis());
  127. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(header), header_l_key, header_value) {
  128. type_writer(header_value, 0, header_l_key, excel_res, NULL);
  129. zval_ptr_dtor(header_value);
  130. } ZEND_HASH_FOREACH_END();
  131. ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
  132. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  133. }
  134. /* }}} */
  135. /** {{{ \Vtiful\Kernel\Excel::data(array $data)
  136. */
  137. PHP_METHOD(vtiful_excel, data)
  138. {
  139. zval rv, *data, res_handle, *data_r_value, *data_l_value;
  140. zend_long data_r_key, data_l_key;
  141. ZEND_PARSE_PARAMETERS_START(1, 1)
  142. Z_PARAM_ARRAY(data)
  143. ZEND_PARSE_PARAMETERS_END();
  144. ZVAL_COPY(return_value, getThis());
  145. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data), data_r_key, data_r_value) {
  146. if(Z_TYPE_P(data_r_value) == IS_ARRAY) {
  147. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data_r_value), data_l_key, data_l_value) {
  148. type_writer(data_l_value, data_r_key+1, data_l_key, excel_res, NULL);
  149. zval_ptr_dtor(data_l_value);
  150. } ZEND_HASH_FOREACH_END();
  151. }
  152. } ZEND_HASH_FOREACH_END();
  153. ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
  154. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  155. }
  156. /* }}} */
  157. /** {{{ \Vtiful\Kernel\Excel::output()
  158. */
  159. PHP_METHOD(vtiful_excel, output)
  160. {
  161. zval rv, null_handle, *handle, *file_path;
  162. handle = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  163. file_path = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_FIL), 0, &rv TSRMLS_DC);
  164. workbook_file(excel_res, handle);
  165. free(excel_res);
  166. ZVAL_NULL(&null_handle);
  167. zend_update_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HANDLE), &null_handle);
  168. ZVAL_COPY(return_value, file_path);
  169. }
  170. /* }}} */
  171. /** {{{ \Vtiful\Kernel\Excel::getHandle()
  172. */
  173. PHP_METHOD(vtiful_excel, getHandle)
  174. {
  175. zval rv;
  176. zval *handle;
  177. handle = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  178. ZVAL_COPY(return_value, handle);
  179. }
  180. /* }}} */
  181. /** {{{ \Vtiful\Kernel\Excel::insertText(int $row, int $column, string|int|double $data)
  182. */
  183. PHP_METHOD(vtiful_excel, insertText)
  184. {
  185. zval rv, res_handle;
  186. zval *data;
  187. zend_long row, column;
  188. zend_string *format = NULL;
  189. ZEND_PARSE_PARAMETERS_START(3, 4)
  190. Z_PARAM_LONG(row)
  191. Z_PARAM_LONG(column)
  192. Z_PARAM_ZVAL(data)
  193. Z_PARAM_OPTIONAL
  194. Z_PARAM_STR(format)
  195. ZEND_PARSE_PARAMETERS_END();
  196. ZVAL_COPY(return_value, getThis());
  197. type_writer(data, row, column, excel_res, format);
  198. ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
  199. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  200. }
  201. /* }}} */
  202. /** {{{ \Vtiful\Kernel\Excel::insertImage(int $row, int $column, string $imagePath)
  203. */
  204. PHP_METHOD(vtiful_excel, insertImage)
  205. {
  206. zval rv, res_handle;
  207. zval *image;
  208. zend_long row, column;
  209. ZEND_PARSE_PARAMETERS_START(3, 3)
  210. Z_PARAM_LONG(row)
  211. Z_PARAM_LONG(column)
  212. Z_PARAM_ZVAL(image)
  213. ZEND_PARSE_PARAMETERS_END();
  214. ZVAL_COPY(return_value, getThis());
  215. image_writer(image, row, column, excel_res);
  216. ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
  217. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  218. }
  219. /* }}} */
  220. /** {{{ \Vtiful\Kernel\Excel::insertImage(int $row, int $column, string $imagePath)
  221. */
  222. PHP_METHOD(vtiful_excel, insertFormula)
  223. {
  224. zval rv, res_handle;
  225. zval *formula;
  226. zend_long row, column;
  227. ZEND_PARSE_PARAMETERS_START(3, 3)
  228. Z_PARAM_LONG(row)
  229. Z_PARAM_LONG(column)
  230. Z_PARAM_ZVAL(formula)
  231. ZEND_PARSE_PARAMETERS_END();
  232. ZVAL_COPY(return_value, getThis());
  233. formula_writer(formula, row, column, excel_res);
  234. ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
  235. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  236. }
  237. /* }}} */
  238. /** {{{ \Vtiful\Kernel\Excel::autoFilter(int $rowStart, int $rowEnd, int $columnStart, int $columnEnd)
  239. */
  240. PHP_METHOD(vtiful_excel, autoFilter)
  241. {
  242. zval rv, res_handle;
  243. zend_string *range;
  244. ZEND_PARSE_PARAMETERS_START(1, 1)
  245. Z_PARAM_STR(range)
  246. ZEND_PARSE_PARAMETERS_END();
  247. ZVAL_COPY(return_value, getThis());
  248. auto_filter(range, excel_res);
  249. ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
  250. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  251. }
  252. /* }}} */
  253. /** {{{ \Vtiful\Kernel\Excel::mergeCells(string $range, string $data)
  254. */
  255. PHP_METHOD(vtiful_excel, mergeCells)
  256. {
  257. zval rv, res_handle;
  258. zend_string *range, *data;
  259. ZEND_PARSE_PARAMETERS_START(2, 2)
  260. Z_PARAM_STR(range)
  261. Z_PARAM_STR(data)
  262. ZEND_PARSE_PARAMETERS_END();
  263. ZVAL_COPY(return_value, getThis());
  264. merge_cells(range, data, excel_res);
  265. ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
  266. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  267. }
  268. /* }}} */
  269. /** {{{ \Vtiful\Kernel\Excel::setColumn(resource $format, string $range [, int $width])
  270. */
  271. PHP_METHOD(vtiful_excel, setColumn)
  272. {
  273. zval *format_handle, res_handle;
  274. zend_string *range;
  275. double width = 0;
  276. int argc = ZEND_NUM_ARGS();
  277. ZEND_PARSE_PARAMETERS_START(2, 3)
  278. Z_PARAM_RESOURCE(format_handle)
  279. Z_PARAM_STR(range)
  280. Z_PARAM_OPTIONAL
  281. Z_PARAM_DOUBLE(width)
  282. ZEND_PARSE_PARAMETERS_END();
  283. ZVAL_COPY(return_value, getThis());
  284. if (argc == 2) {
  285. width = 10;
  286. }
  287. set_column(range, width, excel_res, zval_get_format(format_handle));
  288. ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
  289. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  290. }
  291. /* }}} */
  292. /** {{{ \Vtiful\Kernel\Excel::setRow(resource $format, string $range [, int $heitght])
  293. */
  294. PHP_METHOD(vtiful_excel, setRow)
  295. {
  296. zval *format_handle, res_handle;
  297. zend_string *range;
  298. double height = 0;
  299. int argc = ZEND_NUM_ARGS();
  300. ZEND_PARSE_PARAMETERS_START(2, 3)
  301. Z_PARAM_RESOURCE(format_handle)
  302. Z_PARAM_STR(range)
  303. Z_PARAM_OPTIONAL
  304. Z_PARAM_DOUBLE(height)
  305. ZEND_PARSE_PARAMETERS_END();
  306. ZVAL_COPY(return_value, getThis());
  307. if (argc == 2) {
  308. height = 18;
  309. }
  310. set_row(range, height, excel_res, zval_get_format(format_handle));
  311. ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
  312. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  313. }
  314. /* }}} */
  315. /** {{{ excel_methods
  316. */
  317. zend_function_entry excel_methods[] = {
  318. PHP_ME(vtiful_excel, __construct, excel_construct_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  319. PHP_ME(vtiful_excel, fileName, excel_file_name_arginfo, ZEND_ACC_PUBLIC)
  320. PHP_ME(vtiful_excel, header, excel_header_arginfo, ZEND_ACC_PUBLIC)
  321. PHP_ME(vtiful_excel, data, excel_data_arginfo, ZEND_ACC_PUBLIC)
  322. PHP_ME(vtiful_excel, output, NULL, ZEND_ACC_PUBLIC)
  323. PHP_ME(vtiful_excel, getHandle, NULL, ZEND_ACC_PUBLIC)
  324. PHP_ME(vtiful_excel, autoFilter, excel_auto_filter_arginfo, ZEND_ACC_PUBLIC)
  325. PHP_ME(vtiful_excel, insertText, excel_insert_text_arginfo, ZEND_ACC_PUBLIC)
  326. PHP_ME(vtiful_excel, insertImage, excel_insert_image_arginfo, ZEND_ACC_PUBLIC)
  327. PHP_ME(vtiful_excel, insertFormula, excel_insert_formula_arginfo, ZEND_ACC_PUBLIC)
  328. PHP_ME(vtiful_excel, mergeCells, excel_merge_cells_arginfo, ZEND_ACC_PUBLIC)
  329. PHP_ME(vtiful_excel, setColumn, excel_set_column_arginfo, ZEND_ACC_PUBLIC)
  330. PHP_ME(vtiful_excel, setRow, excel_set_row_arginfo, ZEND_ACC_PUBLIC)
  331. PHP_FE_END
  332. };
  333. /* }}} */
  334. /** {{{ VTIFUL_STARTUP_FUNCTION
  335. */
  336. VTIFUL_STARTUP_FUNCTION(excel) {
  337. zend_class_entry ce;
  338. INIT_NS_CLASS_ENTRY(ce, "Vtiful\\Kernel", "Excel", excel_methods);
  339. vtiful_excel_ce = zend_register_internal_class(&ce);
  340. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_COF), ZEND_ACC_PRIVATE);
  341. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_FIL), ZEND_ACC_PRIVATE);
  342. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_HANDLE), ZEND_ACC_PRIVATE);
  343. return SUCCESS;
  344. }
  345. /* }}} */