excel.c 13 KB

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