excel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 "zend.h"
  16. #include "zend_API.h"
  17. #include "zend_exceptions.h"
  18. #include "php.h"
  19. #include "excel.h"
  20. #include "exception.h"
  21. #include "write.h"
  22. zend_class_entry *vtiful_excel_ce;
  23. /* {{{ ARG_INFO
  24. */
  25. ZEND_BEGIN_ARG_INFO_EX(excel_construct_arginfo, 0, 0, 1)
  26. ZEND_ARG_INFO(0, config)
  27. ZEND_END_ARG_INFO()
  28. ZEND_BEGIN_ARG_INFO_EX(excel_file_name_arginfo, 0, 0, 1)
  29. ZEND_ARG_INFO(0, file_name)
  30. ZEND_END_ARG_INFO()
  31. ZEND_BEGIN_ARG_INFO_EX(excel_header_arginfo, 0, 0, 1)
  32. ZEND_ARG_INFO(0, header)
  33. ZEND_END_ARG_INFO()
  34. ZEND_BEGIN_ARG_INFO_EX(excel_data_arginfo, 0, 0, 1)
  35. ZEND_ARG_INFO(0, data)
  36. ZEND_END_ARG_INFO()
  37. ZEND_BEGIN_ARG_INFO_EX(excel_insert_text_arginfo, 0, 0, 4)
  38. ZEND_ARG_INFO(0, row)
  39. ZEND_ARG_INFO(0, column)
  40. ZEND_ARG_INFO(0, data)
  41. ZEND_ARG_INFO(0, format)
  42. ZEND_END_ARG_INFO()
  43. ZEND_BEGIN_ARG_INFO_EX(excel_insert_image_arginfo, 0, 0, 3)
  44. ZEND_ARG_INFO(0, row)
  45. ZEND_ARG_INFO(0, column)
  46. ZEND_ARG_INFO(0, image)
  47. ZEND_END_ARG_INFO()
  48. ZEND_BEGIN_ARG_INFO_EX(excel_insert_formula_arginfo, 0, 0, 3)
  49. ZEND_ARG_INFO(0, row)
  50. ZEND_ARG_INFO(0, column)
  51. ZEND_ARG_INFO(0, formula)
  52. ZEND_END_ARG_INFO()
  53. ZEND_BEGIN_ARG_INFO_EX(excel_auto_filter_arginfo, 0, 0, 1)
  54. ZEND_ARG_INFO(0, range)
  55. ZEND_END_ARG_INFO()
  56. ZEND_BEGIN_ARG_INFO_EX(excel_merge_cells_arginfo, 0, 0, 2)
  57. ZEND_ARG_INFO(0, range)
  58. ZEND_ARG_INFO(0, data)
  59. ZEND_END_ARG_INFO()
  60. /* }}} */
  61. /* {{{ */
  62. excel_resource_t * zval_get_resource(zval *handle)
  63. {
  64. excel_resource_t *res;
  65. if((res = (excel_resource_t *)zend_fetch_resource(Z_RES_P(handle), VTIFUL_RESOURCE_NAME, le_excel_writer)) == NULL) {
  66. zend_throw_exception(vtiful_exception_ce, "Excel resources resolution fail", 210);
  67. }
  68. return res;
  69. }
  70. /* }}} */
  71. /** {{{ \Vtiful\Kernel\Excel::__construct(array $config)
  72. */
  73. PHP_METHOD(vtiful_excel, __construct)
  74. {
  75. zval *config;
  76. zend_string *key;
  77. ZEND_PARSE_PARAMETERS_START(1, 1)
  78. Z_PARAM_ARRAY(config)
  79. ZEND_PARSE_PARAMETERS_END();
  80. key = zend_string_init(V_EXCEL_PAT, sizeof(V_EXCEL_PAT)-1, 0);
  81. if(zend_hash_find(Z_ARRVAL_P(config), key) == NULL)
  82. {
  83. zend_throw_exception(vtiful_exception_ce, "Lack of 'path' configuration", 110);
  84. }
  85. zend_update_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_COF), config);
  86. zend_string_release(key);
  87. }
  88. /* }}} */
  89. /** {{{ \Vtiful\Kernel\Excel::filename(string $fileName)
  90. */
  91. PHP_METHOD(vtiful_excel, fileName)
  92. {
  93. zval rv, file_path, handle, *config, *tmp_path;
  94. zend_string *file_name, *key, *full_path;
  95. excel_resource_t *res;
  96. ZEND_PARSE_PARAMETERS_START(1, 1)
  97. Z_PARAM_STR(file_name)
  98. ZEND_PARSE_PARAMETERS_END();
  99. ZVAL_COPY(return_value, getThis());
  100. key = zend_string_init(V_EXCEL_PAT, sizeof(V_EXCEL_PAT)-1, 0);
  101. config = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_COF), 0, &rv TSRMLS_DC);
  102. tmp_path = zend_hash_find(Z_ARRVAL_P(config), key);
  103. zend_string_release(key);
  104. if(!tmp_path && Z_TYPE_P(tmp_path) != IS_STRING)
  105. {
  106. zend_throw_exception(vtiful_exception_ce, "Configure 'path' must be a string type", 120);
  107. }
  108. char *tmp_dir = emalloc(Z_STRLEN_P(tmp_path)+ZSTR_LEN(file_name)+2);
  109. strcpy(tmp_dir, Z_STRVAL_P(tmp_path));
  110. strcat(tmp_dir, "/");
  111. strcat(tmp_dir, ZSTR_VAL(file_name));
  112. full_path = zend_string_init(tmp_dir, strlen(tmp_dir), 0);
  113. ZVAL_STR(&file_path, full_path);
  114. res = emalloc(sizeof(excel_resource_t));
  115. res->workbook = workbook_new(tmp_dir);
  116. res->worksheet = workbook_add_worksheet(res->workbook, NULL);
  117. ZVAL_RES(&handle, zend_register_resource(res, le_excel_writer));
  118. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_FIL), &file_path);
  119. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &handle);
  120. zval_ptr_dtor(&file_path);
  121. efree(tmp_dir);
  122. }
  123. /* }}} */
  124. /** {{{ \Vtiful\Kernel\Excel::header(array $header)
  125. */
  126. PHP_METHOD(vtiful_excel, header)
  127. {
  128. zval rv, res_handle, *attr_handle, *header, *header_value;
  129. zend_long header_l_key;
  130. excel_resource_t *res;
  131. ZEND_PARSE_PARAMETERS_START(1, 1)
  132. Z_PARAM_ARRAY(header)
  133. ZEND_PARSE_PARAMETERS_END();
  134. ZVAL_COPY(return_value, getThis());
  135. attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  136. res = zval_get_resource(attr_handle);
  137. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(header), header_l_key, header_value) {
  138. type_writer(header_value, 0, header_l_key, res, NULL);
  139. zval_ptr_dtor(header_value);
  140. } ZEND_HASH_FOREACH_END();
  141. ZVAL_RES(&res_handle, zend_register_resource(res, le_excel_writer));
  142. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  143. }
  144. /* }}} */
  145. /** {{{ \Vtiful\Kernel\Excel::data(array $data)
  146. */
  147. PHP_METHOD(vtiful_excel, data)
  148. {
  149. zval rv, *data, *attr_handle, res_handle, *data_r_value, *data_l_value;
  150. zend_long data_r_key, data_l_key;
  151. excel_resource_t *res;
  152. ZEND_PARSE_PARAMETERS_START(1, 1)
  153. Z_PARAM_ARRAY(data)
  154. ZEND_PARSE_PARAMETERS_END();
  155. ZVAL_COPY(return_value, getThis());
  156. attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  157. res = zval_get_resource(attr_handle);
  158. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data), data_r_key, data_r_value) {
  159. if(Z_TYPE_P(data_r_value) == IS_ARRAY) {
  160. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data_r_value), data_l_key, data_l_value) {
  161. type_writer(data_l_value, data_r_key+1, data_l_key, res, NULL);
  162. zval_ptr_dtor(data_l_value);
  163. } ZEND_HASH_FOREACH_END();
  164. }
  165. } ZEND_HASH_FOREACH_END();
  166. ZVAL_RES(&res_handle, zend_register_resource(res, le_excel_writer));
  167. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  168. }
  169. /* }}} */
  170. /** {{{ \Vtiful\Kernel\Excel::output()
  171. */
  172. PHP_METHOD(vtiful_excel, output)
  173. {
  174. zval rv, null_handle, *handle, *file_path;
  175. excel_resource_t *res;
  176. handle = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  177. file_path = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_FIL), 0, &rv TSRMLS_DC);
  178. res = zval_get_resource(handle);
  179. workbook_file(res, handle);
  180. efree(res);
  181. ZVAL_NULL(&null_handle);
  182. zend_update_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HANDLE), &null_handle);
  183. ZVAL_COPY(return_value, file_path);
  184. }
  185. /* }}} */
  186. /** {{{ \Vtiful\Kernel\Excel::getHandle()
  187. */
  188. PHP_METHOD(vtiful_excel, getHandle)
  189. {
  190. zval rv;
  191. zval *handle;
  192. handle = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  193. ZVAL_COPY(return_value, handle);
  194. }
  195. /* }}} */
  196. /** {{{ \Vtiful\Kernel\Excel::insertText(int $row, int $column, string|int|double $data)
  197. */
  198. PHP_METHOD(vtiful_excel, insertText)
  199. {
  200. zval rv, res_handle;
  201. zval *attr_handle, *data;
  202. zend_long row, column;
  203. zend_string *format = NULL;
  204. excel_resource_t *res;
  205. ZEND_PARSE_PARAMETERS_START(3, 4)
  206. Z_PARAM_LONG(row)
  207. Z_PARAM_LONG(column)
  208. Z_PARAM_ZVAL(data)
  209. Z_PARAM_OPTIONAL
  210. Z_PARAM_STR(format)
  211. ZEND_PARSE_PARAMETERS_END();
  212. ZVAL_COPY(return_value, getThis());
  213. attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  214. res = zval_get_resource(attr_handle);
  215. type_writer(data, row, column, res, format);
  216. ZVAL_RES(&res_handle, zend_register_resource(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, insertImage)
  223. {
  224. zval rv, res_handle;
  225. zval *attr_handle, *image;
  226. zend_long row, column;
  227. excel_resource_t *res;
  228. ZEND_PARSE_PARAMETERS_START(3, 3)
  229. Z_PARAM_LONG(row)
  230. Z_PARAM_LONG(column)
  231. Z_PARAM_ZVAL(image)
  232. ZEND_PARSE_PARAMETERS_END();
  233. ZVAL_COPY(return_value, getThis());
  234. attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  235. res = zval_get_resource(attr_handle);
  236. image_writer(image, row, column, res);
  237. ZVAL_RES(&res_handle, zend_register_resource(res, le_excel_writer));
  238. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  239. }
  240. /* }}} */
  241. /** {{{ \Vtiful\Kernel\Excel::insertImage(int $row, int $column, string $imagePath)
  242. */
  243. PHP_METHOD(vtiful_excel, insertFormula)
  244. {
  245. zval rv, res_handle;
  246. zval *attr_handle, *formula;
  247. zend_long row, column;
  248. excel_resource_t *res;
  249. ZEND_PARSE_PARAMETERS_START(3, 3)
  250. Z_PARAM_LONG(row)
  251. Z_PARAM_LONG(column)
  252. Z_PARAM_ZVAL(formula)
  253. ZEND_PARSE_PARAMETERS_END();
  254. ZVAL_COPY(return_value, getThis());
  255. attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  256. res = zval_get_resource(attr_handle);
  257. formula_writer(formula, row, column, res);
  258. ZVAL_RES(&res_handle, zend_register_resource(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::autoFilter(int $rowStart, int $rowEnd, int $columnStart, int $columnEnd)
  263. */
  264. PHP_METHOD(vtiful_excel, autoFilter)
  265. {
  266. zval rv, res_handle;
  267. zval *attr_handle;
  268. zend_string *range;
  269. excel_resource_t *res;
  270. ZEND_PARSE_PARAMETERS_START(1, 1)
  271. Z_PARAM_STR(range)
  272. ZEND_PARSE_PARAMETERS_END();
  273. ZVAL_COPY(return_value, getThis());
  274. attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  275. res = zval_get_resource(attr_handle);
  276. auto_filter(range, res);
  277. ZVAL_RES(&res_handle, zend_register_resource(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::mergeCells(string $range, string $data)
  282. */
  283. PHP_METHOD(vtiful_excel, mergeCells)
  284. {
  285. zval rv, res_handle;
  286. zval *attr_handle;
  287. zend_string *range, *data;
  288. excel_resource_t *res;
  289. ZEND_PARSE_PARAMETERS_START(2, 2)
  290. Z_PARAM_STR(range)
  291. Z_PARAM_STR(data)
  292. ZEND_PARSE_PARAMETERS_END();
  293. ZVAL_COPY(return_value, getThis());
  294. attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  295. res = zval_get_resource(attr_handle);
  296. merge_cells(range, data, res);
  297. ZVAL_RES(&res_handle, zend_register_resource(res, le_excel_writer));
  298. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  299. }
  300. /* }}} */
  301. /** {{{ excel_methods
  302. */
  303. zend_function_entry excel_methods[] = {
  304. PHP_ME(vtiful_excel, __construct, excel_construct_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  305. PHP_ME(vtiful_excel, fileName, excel_file_name_arginfo, ZEND_ACC_PUBLIC)
  306. PHP_ME(vtiful_excel, header, excel_header_arginfo, ZEND_ACC_PUBLIC)
  307. PHP_ME(vtiful_excel, data, excel_data_arginfo, ZEND_ACC_PUBLIC)
  308. PHP_ME(vtiful_excel, output, NULL, ZEND_ACC_PUBLIC)
  309. PHP_ME(vtiful_excel, getHandle, NULL, ZEND_ACC_PUBLIC)
  310. PHP_ME(vtiful_excel, autoFilter, excel_auto_filter_arginfo, ZEND_ACC_PUBLIC)
  311. PHP_ME(vtiful_excel, insertText, excel_insert_text_arginfo, ZEND_ACC_PUBLIC)
  312. PHP_ME(vtiful_excel, insertImage, excel_insert_image_arginfo, ZEND_ACC_PUBLIC)
  313. PHP_ME(vtiful_excel, insertFormula, excel_insert_formula_arginfo, ZEND_ACC_PUBLIC)
  314. PHP_ME(vtiful_excel, mergeCells, excel_merge_cells_arginfo, ZEND_ACC_PUBLIC)
  315. PHP_FE_END
  316. };
  317. /* }}} */
  318. /** {{{ VTIFUL_STARTUP_FUNCTION
  319. */
  320. VTIFUL_STARTUP_FUNCTION(excel) {
  321. zend_class_entry ce;
  322. INIT_NS_CLASS_ENTRY(ce, "Vtiful\\Kernel", "Excel", excel_methods);
  323. vtiful_excel_ce = zend_register_internal_class(&ce);
  324. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_COF), ZEND_ACC_PRIVATE);
  325. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_FIL), ZEND_ACC_PRIVATE);
  326. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_HANDLE), ZEND_ACC_PRIVATE);
  327. return SUCCESS;
  328. }
  329. /* }}} */