excel.c 12 KB

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