excel.c 12 KB

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