excel.c 11 KB

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