excel.c 13 KB

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