excel.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. /* }}} */
  55. /** {{{ \Vtiful\Kernel\Excel::__construct(array $config)
  56. */
  57. PHP_METHOD(vtiful_excel, __construct)
  58. {
  59. zval *config;
  60. zend_string *key;
  61. ZEND_PARSE_PARAMETERS_START(1, 1)
  62. Z_PARAM_ARRAY(config)
  63. ZEND_PARSE_PARAMETERS_END();
  64. key = zend_string_init(V_EXCEL_PAT, sizeof(V_EXCEL_PAT)-1, 0);
  65. if(zend_hash_find(Z_ARRVAL_P(config), key) == NULL)
  66. {
  67. zend_throw_exception(vtiful_exception_ce, "Lack of 'path' configuration", 110);
  68. }
  69. zend_update_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_COF), config);
  70. zend_string_release(key);
  71. }
  72. /* }}} */
  73. /** {{{ \Vtiful\Kernel\Excel::filename(string $fileName)
  74. */
  75. PHP_METHOD(vtiful_excel, fileName)
  76. {
  77. zval rv, file_path, handle, *config, *tmp_path;
  78. zend_string *file_name, *key, *full_path;
  79. ZEND_PARSE_PARAMETERS_START(1, 1)
  80. Z_PARAM_STR(file_name)
  81. ZEND_PARSE_PARAMETERS_END();
  82. ZVAL_COPY(return_value, getThis());
  83. key = zend_string_init(V_EXCEL_PAT, sizeof(V_EXCEL_PAT)-1, 0);
  84. config = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_COF), 0, &rv TSRMLS_DC);
  85. tmp_path = zend_hash_find(Z_ARRVAL_P(config), key);
  86. zend_string_release(key);
  87. if(!tmp_path && Z_TYPE_P(tmp_path) != IS_STRING)
  88. {
  89. zend_throw_exception(vtiful_exception_ce, "Configure 'path' must be a string type", 120);
  90. }
  91. char *tmp_dir = emalloc(Z_STRLEN_P(tmp_path)+ZSTR_LEN(file_name)+2);
  92. strcpy(tmp_dir, Z_STRVAL_P(tmp_path));
  93. strcat(tmp_dir, "/");
  94. strcat(tmp_dir, ZSTR_VAL(file_name));
  95. full_path = zend_string_init(tmp_dir, strlen(tmp_dir), 0);
  96. ZVAL_STR(&file_path, full_path);
  97. excel_res->workbook = workbook_new(tmp_dir);
  98. excel_res->worksheet = workbook_add_worksheet(excel_res->workbook, NULL);
  99. ZVAL_RES(&handle, zend_register_resource(excel_res, le_excel_writer));
  100. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_FIL), &file_path);
  101. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &handle);
  102. zval_ptr_dtor(&file_path);
  103. efree(tmp_dir);
  104. }
  105. /* }}} */
  106. /** {{{ \Vtiful\Kernel\Excel::header(array $header)
  107. */
  108. PHP_METHOD(vtiful_excel, header)
  109. {
  110. zval rv, res_handle, *header, *header_value;
  111. zend_long header_l_key;
  112. ZEND_PARSE_PARAMETERS_START(1, 1)
  113. Z_PARAM_ARRAY(header)
  114. ZEND_PARSE_PARAMETERS_END();
  115. ZVAL_COPY(return_value, getThis());
  116. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(header), header_l_key, header_value) {
  117. type_writer(header_value, 0, header_l_key, excel_res, NULL);
  118. zval_ptr_dtor(header_value);
  119. } ZEND_HASH_FOREACH_END();
  120. ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
  121. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  122. }
  123. /* }}} */
  124. /** {{{ \Vtiful\Kernel\Excel::data(array $data)
  125. */
  126. PHP_METHOD(vtiful_excel, data)
  127. {
  128. zval rv, *data, res_handle, *data_r_value, *data_l_value;
  129. zend_long data_r_key, data_l_key;
  130. ZEND_PARSE_PARAMETERS_START(1, 1)
  131. Z_PARAM_ARRAY(data)
  132. ZEND_PARSE_PARAMETERS_END();
  133. ZVAL_COPY(return_value, getThis());
  134. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data), data_r_key, data_r_value) {
  135. if(Z_TYPE_P(data_r_value) == IS_ARRAY) {
  136. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data_r_value), data_l_key, data_l_value) {
  137. type_writer(data_l_value, data_r_key+1, data_l_key, excel_res, NULL);
  138. zval_ptr_dtor(data_l_value);
  139. } ZEND_HASH_FOREACH_END();
  140. }
  141. } ZEND_HASH_FOREACH_END();
  142. ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
  143. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  144. }
  145. /* }}} */
  146. /** {{{ \Vtiful\Kernel\Excel::output()
  147. */
  148. PHP_METHOD(vtiful_excel, output)
  149. {
  150. zval rv, null_handle, *handle, *file_path;
  151. handle = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  152. file_path = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_FIL), 0, &rv TSRMLS_DC);
  153. workbook_file(excel_res, handle);
  154. efree(excel_res);
  155. ZVAL_NULL(&null_handle);
  156. zend_update_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HANDLE), &null_handle);
  157. ZVAL_COPY(return_value, file_path);
  158. }
  159. /* }}} */
  160. /** {{{ \Vtiful\Kernel\Excel::getHandle()
  161. */
  162. PHP_METHOD(vtiful_excel, getHandle)
  163. {
  164. zval rv;
  165. zval *handle;
  166. handle = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  167. ZVAL_COPY(return_value, handle);
  168. }
  169. /* }}} */
  170. /** {{{ \Vtiful\Kernel\Excel::insertText(int $row, int $column, string|int|double $data)
  171. */
  172. PHP_METHOD(vtiful_excel, insertText)
  173. {
  174. zval rv, res_handle;
  175. zval *data;
  176. zend_long row, column;
  177. zend_string *format = NULL;
  178. ZEND_PARSE_PARAMETERS_START(3, 4)
  179. Z_PARAM_LONG(row)
  180. Z_PARAM_LONG(column)
  181. Z_PARAM_ZVAL(data)
  182. Z_PARAM_OPTIONAL
  183. Z_PARAM_STR(format)
  184. ZEND_PARSE_PARAMETERS_END();
  185. ZVAL_COPY(return_value, getThis());
  186. type_writer(data, row, column, excel_res, format);
  187. ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
  188. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  189. }
  190. /* }}} */
  191. /** {{{ \Vtiful\Kernel\Excel::insertImage(int $row, int $column, string $imagePath)
  192. */
  193. PHP_METHOD(vtiful_excel, insertImage)
  194. {
  195. zval rv, res_handle;
  196. zval *image;
  197. zend_long row, column;
  198. ZEND_PARSE_PARAMETERS_START(3, 3)
  199. Z_PARAM_LONG(row)
  200. Z_PARAM_LONG(column)
  201. Z_PARAM_ZVAL(image)
  202. ZEND_PARSE_PARAMETERS_END();
  203. ZVAL_COPY(return_value, getThis());
  204. image_writer(image, row, column, excel_res);
  205. ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
  206. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  207. }
  208. /* }}} */
  209. /** {{{ \Vtiful\Kernel\Excel::insertImage(int $row, int $column, string $imagePath)
  210. */
  211. PHP_METHOD(vtiful_excel, insertFormula)
  212. {
  213. zval rv, res_handle;
  214. zval *formula;
  215. zend_long row, column;
  216. ZEND_PARSE_PARAMETERS_START(3, 3)
  217. Z_PARAM_LONG(row)
  218. Z_PARAM_LONG(column)
  219. Z_PARAM_ZVAL(formula)
  220. ZEND_PARSE_PARAMETERS_END();
  221. ZVAL_COPY(return_value, getThis());
  222. formula_writer(formula, row, column, excel_res);
  223. ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
  224. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  225. }
  226. /* }}} */
  227. /** {{{ \Vtiful\Kernel\Excel::autoFilter(int $rowStart, int $rowEnd, int $columnStart, int $columnEnd)
  228. */
  229. PHP_METHOD(vtiful_excel, autoFilter)
  230. {
  231. zval rv, res_handle;
  232. zend_string *range;
  233. ZEND_PARSE_PARAMETERS_START(1, 1)
  234. Z_PARAM_STR(range)
  235. ZEND_PARSE_PARAMETERS_END();
  236. ZVAL_COPY(return_value, getThis());
  237. auto_filter(range, excel_res);
  238. ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
  239. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  240. }
  241. /* }}} */
  242. /** {{{ \Vtiful\Kernel\Excel::mergeCells(string $range, string $data)
  243. */
  244. PHP_METHOD(vtiful_excel, mergeCells)
  245. {
  246. zval rv, res_handle;
  247. zend_string *range, *data;
  248. ZEND_PARSE_PARAMETERS_START(2, 2)
  249. Z_PARAM_STR(range)
  250. Z_PARAM_STR(data)
  251. ZEND_PARSE_PARAMETERS_END();
  252. ZVAL_COPY(return_value, getThis());
  253. merge_cells(range, data, excel_res);
  254. ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
  255. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  256. }
  257. /* }}} */
  258. /** {{{ excel_methods
  259. */
  260. zend_function_entry excel_methods[] = {
  261. PHP_ME(vtiful_excel, __construct, excel_construct_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  262. PHP_ME(vtiful_excel, fileName, excel_file_name_arginfo, ZEND_ACC_PUBLIC)
  263. PHP_ME(vtiful_excel, header, excel_header_arginfo, ZEND_ACC_PUBLIC)
  264. PHP_ME(vtiful_excel, data, excel_data_arginfo, ZEND_ACC_PUBLIC)
  265. PHP_ME(vtiful_excel, output, NULL, ZEND_ACC_PUBLIC)
  266. PHP_ME(vtiful_excel, getHandle, NULL, ZEND_ACC_PUBLIC)
  267. PHP_ME(vtiful_excel, autoFilter, excel_auto_filter_arginfo, ZEND_ACC_PUBLIC)
  268. PHP_ME(vtiful_excel, insertText, excel_insert_text_arginfo, ZEND_ACC_PUBLIC)
  269. PHP_ME(vtiful_excel, insertImage, excel_insert_image_arginfo, ZEND_ACC_PUBLIC)
  270. PHP_ME(vtiful_excel, insertFormula, excel_insert_formula_arginfo, ZEND_ACC_PUBLIC)
  271. PHP_ME(vtiful_excel, mergeCells, excel_merge_cells_arginfo, ZEND_ACC_PUBLIC)
  272. PHP_FE_END
  273. };
  274. /* }}} */
  275. /** {{{ VTIFUL_STARTUP_FUNCTION
  276. */
  277. VTIFUL_STARTUP_FUNCTION(excel) {
  278. zend_class_entry ce;
  279. INIT_NS_CLASS_ENTRY(ce, "Vtiful\\Kernel", "Excel", excel_methods);
  280. vtiful_excel_ce = zend_register_internal_class(&ce);
  281. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_COF), ZEND_ACC_PRIVATE);
  282. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_FIL), ZEND_ACC_PRIVATE);
  283. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_HANDLE), ZEND_ACC_PRIVATE);
  284. return SUCCESS;
  285. }
  286. /* }}} */