excel.c 13 KB

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