excel.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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_vtiful)) == 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, tmp_file_name, file_path, handle, *config, *tmp_path;
  89. zend_string *file_name, *key;
  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. ZVAL_STR(&tmp_file_name, file_name);
  104. concat_function(&file_path, tmp_path, &tmp_file_name);
  105. res = emalloc(sizeof(excel_resource_t));
  106. res->workbook = workbook_new(ZSTR_VAL(zval_get_string(&file_path)));
  107. res->worksheet = workbook_add_worksheet(res->workbook, NULL);
  108. zval_ptr_dtor(&file_path);
  109. ZVAL_RES(&handle, zend_register_resource(res, le_vtiful));
  110. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_FIL), &file_path);
  111. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &handle);
  112. zval_ptr_dtor(&file_path);
  113. }
  114. /* }}} */
  115. /** {{{ \Vtiful\Kernel\Excel::header(array $header)
  116. */
  117. PHP_METHOD(vtiful_excel, header)
  118. {
  119. zval rv, res_handle, *attr_handle, *header, *header_value;
  120. zend_long header_l_key;
  121. excel_resource_t *res;
  122. ZEND_PARSE_PARAMETERS_START(1, 1)
  123. Z_PARAM_ARRAY(header)
  124. ZEND_PARSE_PARAMETERS_END();
  125. ZVAL_COPY(return_value, getThis());
  126. attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  127. res = zval_get_resource(attr_handle);
  128. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(header), header_l_key, header_value) {
  129. type_writer(header_value, 0, header_l_key, res);
  130. zval_ptr_dtor(header_value);
  131. } ZEND_HASH_FOREACH_END();
  132. ZVAL_RES(&res_handle, zend_register_resource(res, le_vtiful));
  133. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  134. }
  135. /* }}} */
  136. /** {{{ \Vtiful\Kernel\Excel::data(array $data)
  137. */
  138. PHP_METHOD(vtiful_excel, data)
  139. {
  140. zval rv, *data, *attr_handle, res_handle, *data_r_value, *data_l_value;
  141. zend_long data_r_key, data_l_key;
  142. excel_resource_t *res;
  143. ZEND_PARSE_PARAMETERS_START(1, 1)
  144. Z_PARAM_ARRAY(data)
  145. ZEND_PARSE_PARAMETERS_END();
  146. ZVAL_COPY(return_value, getThis());
  147. attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  148. res = zval_get_resource(attr_handle);
  149. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data), data_r_key, data_r_value) {
  150. if(Z_TYPE_P(data_r_value) == IS_ARRAY) {
  151. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data_r_value), data_l_key, data_l_value) {
  152. type_writer(data_l_value, data_r_key+1, data_l_key, res);
  153. zval_ptr_dtor(data_l_value);
  154. } ZEND_HASH_FOREACH_END();
  155. }
  156. } ZEND_HASH_FOREACH_END();
  157. ZVAL_RES(&res_handle, zend_register_resource(res, le_vtiful));
  158. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  159. }
  160. /* }}} */
  161. /** {{{ \Vtiful\Kernel\Excel::output()
  162. */
  163. PHP_METHOD(vtiful_excel, output)
  164. {
  165. zval rv, *handle, null_handle;
  166. excel_resource_t *res;
  167. handle = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  168. res = zval_get_resource(handle);
  169. workbook_file(res, handle);
  170. efree(res);
  171. ZVAL_NULL(&null_handle);
  172. zend_update_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HANDLE), &null_handle);
  173. }
  174. /* }}} */
  175. /** {{{ \Vtiful\Kernel\Excel::getHandle()
  176. */
  177. PHP_METHOD(vtiful_excel, getHandle)
  178. {
  179. zval rv;
  180. zval *handle;
  181. handle = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  182. ZVAL_COPY(return_value, handle);
  183. }
  184. /* }}} */
  185. /** {{{ \Vtiful\Kernel\Excel::insertText(int $row, int $column, string|int|double $data)
  186. */
  187. PHP_METHOD(vtiful_excel, insertText)
  188. {
  189. zval rv, res_handle;
  190. zval *attr_handle, *data;
  191. zend_long row, column;
  192. excel_resource_t *res;
  193. ZEND_PARSE_PARAMETERS_START(3, 3)
  194. Z_PARAM_LONG(row)
  195. Z_PARAM_LONG(column)
  196. Z_PARAM_ZVAL(data)
  197. ZEND_PARSE_PARAMETERS_END();
  198. ZVAL_COPY(return_value, getThis());
  199. attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  200. res = zval_get_resource(attr_handle);
  201. type_writer(data, row, column, res);
  202. ZVAL_RES(&res_handle, zend_register_resource(res, le_vtiful));
  203. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  204. }
  205. /* }}} */
  206. /** {{{ \Vtiful\Kernel\Excel::insertImage(int $row, int $column, string $imagePath)
  207. */
  208. PHP_METHOD(vtiful_excel, insertImage)
  209. {
  210. zval rv, res_handle;
  211. zval *attr_handle, *image;
  212. zend_long row, column;
  213. excel_resource_t *res;
  214. ZEND_PARSE_PARAMETERS_START(3, 3)
  215. Z_PARAM_LONG(row)
  216. Z_PARAM_LONG(column)
  217. Z_PARAM_ZVAL(image)
  218. ZEND_PARSE_PARAMETERS_END();
  219. ZVAL_COPY(return_value, getThis());
  220. attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  221. res = zval_get_resource(attr_handle);
  222. image_writer(image, row, column, res);
  223. ZVAL_RES(&res_handle, zend_register_resource(res, le_vtiful));
  224. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  225. }
  226. /* }}} */
  227. /** {{{ \Vtiful\Kernel\Excel::insertImage(int $row, int $column, string $imagePath)
  228. */
  229. PHP_METHOD(vtiful_excel, insertFormula)
  230. {
  231. zval rv, res_handle;
  232. zval *attr_handle, *formula;
  233. zend_long row, column;
  234. excel_resource_t *res;
  235. ZEND_PARSE_PARAMETERS_START(3, 3)
  236. Z_PARAM_LONG(row)
  237. Z_PARAM_LONG(column)
  238. Z_PARAM_ZVAL(formula)
  239. ZEND_PARSE_PARAMETERS_END();
  240. ZVAL_COPY(return_value, getThis());
  241. attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  242. res = zval_get_resource(attr_handle);
  243. formula_writer(formula, row, column, res);
  244. ZVAL_RES(&res_handle, zend_register_resource(res, le_vtiful));
  245. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  246. }
  247. /* }}} */
  248. /** {{{ \Vtiful\Kernel\Excel::autoFilter(int $rowStart, int $rowEnd, int $columnStart, int $columnEnd)
  249. */
  250. PHP_METHOD(vtiful_excel, autoFilter)
  251. {
  252. zval rv, res_handle;
  253. zval *attr_handle;
  254. zend_string *range;
  255. excel_resource_t *res;
  256. ZEND_PARSE_PARAMETERS_START(1, 1)
  257. Z_PARAM_STR(range)
  258. ZEND_PARSE_PARAMETERS_END();
  259. ZVAL_COPY(return_value, getThis());
  260. attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  261. res = zval_get_resource(attr_handle);
  262. auto_filter(range, res);
  263. ZVAL_RES(&res_handle, zend_register_resource(res, le_vtiful));
  264. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  265. }
  266. /* }}} */
  267. /** {{{ excel_methods
  268. */
  269. zend_function_entry excel_methods[] = {
  270. PHP_ME(vtiful_excel, __construct, excel_construct_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  271. PHP_ME(vtiful_excel, fileName, excel_file_name_arginfo, ZEND_ACC_PUBLIC)
  272. PHP_ME(vtiful_excel, header, excel_header_arginfo, ZEND_ACC_PUBLIC)
  273. PHP_ME(vtiful_excel, data, excel_data_arginfo, ZEND_ACC_PUBLIC)
  274. PHP_ME(vtiful_excel, output, NULL, ZEND_ACC_PUBLIC)
  275. PHP_ME(vtiful_excel, getHandle, NULL, ZEND_ACC_PUBLIC)
  276. PHP_ME(vtiful_excel, autoFilter, excel_auto_filter_arginfo, ZEND_ACC_PUBLIC)
  277. PHP_ME(vtiful_excel, insertText, excel_insert_text_arginfo, ZEND_ACC_PUBLIC)
  278. PHP_ME(vtiful_excel, insertImage, excel_insert_image_arginfo, ZEND_ACC_PUBLIC)
  279. PHP_ME(vtiful_excel, insertFormula, excel_insert_formula_arginfo, ZEND_ACC_PUBLIC)
  280. PHP_FE_END
  281. };
  282. /* }}} */
  283. /** {{{ VTIFUL_STARTUP_FUNCTION
  284. */
  285. VTIFUL_STARTUP_FUNCTION(excel) {
  286. zend_class_entry ce;
  287. INIT_NS_CLASS_ENTRY(ce, "Vtiful\\Kernel", "Excel", excel_methods);
  288. vtiful_excel_ce = zend_register_internal_class(&ce);
  289. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_COF), ZEND_ACC_PRIVATE);
  290. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_FIL), ZEND_ACC_PRIVATE);
  291. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_HANDLE), ZEND_ACC_PRIVATE);
  292. return SUCCESS;
  293. }
  294. /* }}} */