excel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 "include.h"
  16. zend_class_entry *vtiful_excel_ce;
  17. static zend_object_handlers vtiful_excel_handlers;
  18. static zend_always_inline void *vtiful_object_alloc(size_t obj_size, zend_class_entry *ce) {
  19. void *obj = emalloc(obj_size + zend_object_properties_size(ce));
  20. memset(obj, 0, obj_size);
  21. return obj;
  22. }
  23. /* {{{ excel_objects_new
  24. */
  25. PHP_VTIFUL_API zend_object *excel_objects_new(zend_class_entry *ce)
  26. {
  27. excel_object *intern = vtiful_object_alloc(sizeof(excel_object), ce);
  28. zend_object_std_init(&intern->zo, ce);
  29. object_properties_init(&intern->zo, ce);
  30. intern->zo.handlers = &vtiful_excel_handlers;
  31. return &intern->zo;
  32. }
  33. /* }}} */
  34. /* {{{ vtiful_excel_objects_free
  35. */
  36. static void vtiful_excel_objects_free(zend_object *object)
  37. {
  38. excel_object *intern = php_vtiful_excel_fetch_object(object);
  39. lxw_workbook_free(intern->ptr.workbook);
  40. zend_object_std_dtor(&intern->zo);
  41. }
  42. /* }}} */
  43. /* {{{ ARG_INFO
  44. */
  45. ZEND_BEGIN_ARG_INFO_EX(excel_construct_arginfo, 0, 0, 1)
  46. ZEND_ARG_INFO(0, config)
  47. ZEND_END_ARG_INFO()
  48. ZEND_BEGIN_ARG_INFO_EX(excel_file_name_arginfo, 0, 0, 1)
  49. ZEND_ARG_INFO(0, file_name)
  50. ZEND_END_ARG_INFO()
  51. ZEND_BEGIN_ARG_INFO_EX(excel_header_arginfo, 0, 0, 1)
  52. ZEND_ARG_INFO(0, header)
  53. ZEND_END_ARG_INFO()
  54. ZEND_BEGIN_ARG_INFO_EX(excel_data_arginfo, 0, 0, 1)
  55. ZEND_ARG_INFO(0, data)
  56. ZEND_END_ARG_INFO()
  57. ZEND_BEGIN_ARG_INFO_EX(excel_insert_text_arginfo, 0, 0, 4)
  58. ZEND_ARG_INFO(0, row)
  59. ZEND_ARG_INFO(0, column)
  60. ZEND_ARG_INFO(0, data)
  61. ZEND_ARG_INFO(0, format)
  62. ZEND_END_ARG_INFO()
  63. ZEND_BEGIN_ARG_INFO_EX(excel_insert_image_arginfo, 0, 0, 3)
  64. ZEND_ARG_INFO(0, row)
  65. ZEND_ARG_INFO(0, column)
  66. ZEND_ARG_INFO(0, image)
  67. ZEND_END_ARG_INFO()
  68. ZEND_BEGIN_ARG_INFO_EX(excel_insert_formula_arginfo, 0, 0, 3)
  69. ZEND_ARG_INFO(0, row)
  70. ZEND_ARG_INFO(0, column)
  71. ZEND_ARG_INFO(0, formula)
  72. ZEND_END_ARG_INFO()
  73. ZEND_BEGIN_ARG_INFO_EX(excel_auto_filter_arginfo, 0, 0, 1)
  74. ZEND_ARG_INFO(0, range)
  75. ZEND_END_ARG_INFO()
  76. ZEND_BEGIN_ARG_INFO_EX(excel_merge_cells_arginfo, 0, 0, 2)
  77. ZEND_ARG_INFO(0, range)
  78. ZEND_ARG_INFO(0, data)
  79. ZEND_END_ARG_INFO()
  80. ZEND_BEGIN_ARG_INFO_EX(excel_set_column_arginfo, 0, 0, 3)
  81. ZEND_ARG_INFO(0, format_handle)
  82. ZEND_ARG_INFO(0, range)
  83. ZEND_ARG_INFO(0, width)
  84. ZEND_END_ARG_INFO()
  85. ZEND_BEGIN_ARG_INFO_EX(excel_set_row_arginfo, 0, 0, 3)
  86. ZEND_ARG_INFO(0, format_handle)
  87. ZEND_ARG_INFO(0, range)
  88. ZEND_ARG_INFO(0, height)
  89. ZEND_END_ARG_INFO()
  90. /* }}} */
  91. /** {{{ \Vtiful\Kernel\Excel::__construct(array $config)
  92. */
  93. PHP_METHOD(vtiful_excel, __construct)
  94. {
  95. zval *config, *c_path;
  96. ZEND_PARSE_PARAMETERS_START(1, 1)
  97. Z_PARAM_ARRAY(config)
  98. ZEND_PARSE_PARAMETERS_END();
  99. if((c_path = zend_hash_str_find(Z_ARRVAL_P(config), ZEND_STRL(V_EXCEL_PAT))) == NULL)
  100. {
  101. zend_throw_exception(vtiful_exception_ce, "Lack of 'path' configuration", 110);
  102. return;
  103. }
  104. if(Z_TYPE_P(c_path) != IS_STRING)
  105. {
  106. zend_throw_exception(vtiful_exception_ce, "Configure 'path' must be a string type", 120);
  107. return;
  108. }
  109. add_property_zval(getThis(), V_EXCEL_COF, config);
  110. }
  111. /* }}} */
  112. /** {{{ \Vtiful\Kernel\Excel::filename(string $fileName)
  113. */
  114. PHP_METHOD(vtiful_excel, fileName)
  115. {
  116. zval file_path, *dir_path;
  117. zend_string *file_name;
  118. ZEND_PARSE_PARAMETERS_START(1, 1)
  119. Z_PARAM_STR(file_name)
  120. ZEND_PARSE_PARAMETERS_END();
  121. ZVAL_COPY(return_value, getThis());
  122. GET_CONFIG_PATH(dir_path, vtiful_excel_ce, return_value);
  123. excel_file_path(file_name, dir_path, &file_path);
  124. excel_object *obj = Z_EXCEL_P(getThis());
  125. obj->ptr.workbook = workbook_new(Z_STRVAL(file_path));
  126. obj->ptr.worksheet = workbook_add_worksheet(obj->ptr.workbook, NULL);
  127. add_property_zval(return_value, V_EXCEL_FIL, &file_path);
  128. zval_ptr_dtor(&file_path);
  129. }
  130. /* }}} */
  131. /** {{{ \Vtiful\Kernel\Excel::constMemory(string $fileName)
  132. */
  133. PHP_METHOD(vtiful_excel, constMemory)
  134. {
  135. zval file_path, *dir_path;
  136. zend_string *file_name;
  137. ZEND_PARSE_PARAMETERS_START(1, 1)
  138. Z_PARAM_STR(file_name)
  139. ZEND_PARSE_PARAMETERS_END();
  140. ZVAL_COPY(return_value, getThis());
  141. GET_CONFIG_PATH(dir_path, vtiful_excel_ce, return_value);
  142. excel_file_path(file_name, dir_path, &file_path);
  143. lxw_workbook_options options = {.constant_memory = LXW_TRUE, .tmpdir = NULL};
  144. excel_object *obj = Z_EXCEL_P(getThis());
  145. obj->ptr.workbook = workbook_new_opt(Z_STRVAL(file_path), &options);
  146. obj->ptr.worksheet = workbook_add_worksheet(obj->ptr.workbook, NULL);
  147. add_property_zval(return_value, V_EXCEL_FIL, &file_path);
  148. zval_ptr_dtor(&file_path);
  149. }
  150. /* }}} */
  151. /** {{{ \Vtiful\Kernel\Excel::header(array $header)
  152. */
  153. PHP_METHOD(vtiful_excel, header)
  154. {
  155. zval *header, *header_value;
  156. zend_long header_l_key;
  157. ZEND_PARSE_PARAMETERS_START(1, 1)
  158. Z_PARAM_ARRAY(header)
  159. ZEND_PARSE_PARAMETERS_END();
  160. ZVAL_COPY(return_value, getThis());
  161. excel_object *obj = Z_EXCEL_P(getThis());
  162. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(header), header_l_key, header_value) {
  163. type_writer(header_value, 0, header_l_key, &obj->ptr, NULL);
  164. zval_ptr_dtor(header_value);
  165. } ZEND_HASH_FOREACH_END();
  166. }
  167. /* }}} */
  168. /** {{{ \Vtiful\Kernel\Excel::data(array $data)
  169. */
  170. PHP_METHOD(vtiful_excel, data)
  171. {
  172. zval *data, *data_r_value, *data_l_value;
  173. zend_long data_r_key, data_l_key;
  174. ZEND_PARSE_PARAMETERS_START(1, 1)
  175. Z_PARAM_ARRAY(data)
  176. ZEND_PARSE_PARAMETERS_END();
  177. ZVAL_COPY(return_value, getThis());
  178. excel_object *obj = Z_EXCEL_P(getThis());
  179. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data), data_r_key, data_r_value) {
  180. if(Z_TYPE_P(data_r_value) == IS_ARRAY) {
  181. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data_r_value), data_l_key, data_l_value) {
  182. type_writer(data_l_value, data_r_key+1, data_l_key, &obj->ptr, NULL);
  183. zval_ptr_dtor(data_l_value);
  184. } ZEND_HASH_FOREACH_END();
  185. }
  186. } ZEND_HASH_FOREACH_END();
  187. }
  188. /* }}} */
  189. /** {{{ \Vtiful\Kernel\Excel::output()
  190. */
  191. PHP_METHOD(vtiful_excel, output)
  192. {
  193. zval rv, *file_path;
  194. file_path = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_FIL), 0, &rv TSRMLS_DC);
  195. excel_object *obj = Z_EXCEL_P(getThis());
  196. workbook_file(&obj->ptr);
  197. add_property_null(getThis(), V_EXCEL_HANDLE);
  198. add_property_null(getThis(), V_EXCEL_PAT);
  199. ZVAL_COPY(return_value, file_path);
  200. }
  201. /* }}} */
  202. /** {{{ \Vtiful\Kernel\Excel::getHandle()
  203. */
  204. PHP_METHOD(vtiful_excel, getHandle)
  205. {
  206. excel_object *obj = Z_EXCEL_P(getThis());
  207. RETURN_RES(zend_register_resource(&obj->ptr, le_excel_writer));
  208. }
  209. /* }}} */
  210. /** {{{ \Vtiful\Kernel\Excel::insertText(int $row, int $column, string|int|double $data)
  211. */
  212. PHP_METHOD(vtiful_excel, insertText)
  213. {
  214. zval *data;
  215. zend_long row, column;
  216. zend_string *format = NULL;
  217. ZEND_PARSE_PARAMETERS_START(3, 4)
  218. Z_PARAM_LONG(row)
  219. Z_PARAM_LONG(column)
  220. Z_PARAM_ZVAL(data)
  221. Z_PARAM_OPTIONAL
  222. Z_PARAM_STR(format)
  223. ZEND_PARSE_PARAMETERS_END();
  224. ZVAL_COPY(return_value, getThis());
  225. excel_object *obj = Z_EXCEL_P(getThis());
  226. type_writer(data, row, column, &obj->ptr, format);
  227. }
  228. /* }}} */
  229. /** {{{ \Vtiful\Kernel\Excel::insertImage(int $row, int $column, string $imagePath)
  230. */
  231. PHP_METHOD(vtiful_excel, insertImage)
  232. {
  233. zval *image;
  234. zend_long row, column;
  235. ZEND_PARSE_PARAMETERS_START(3, 3)
  236. Z_PARAM_LONG(row)
  237. Z_PARAM_LONG(column)
  238. Z_PARAM_ZVAL(image)
  239. ZEND_PARSE_PARAMETERS_END();
  240. ZVAL_COPY(return_value, getThis());
  241. excel_object *obj = Z_EXCEL_P(getThis());
  242. image_writer(image, row, column, &obj->ptr);
  243. }
  244. /* }}} */
  245. /** {{{ \Vtiful\Kernel\Excel::insertImage(int $row, int $column, string $imagePath)
  246. */
  247. PHP_METHOD(vtiful_excel, insertFormula)
  248. {
  249. zval *formula;
  250. zend_long row, column;
  251. ZEND_PARSE_PARAMETERS_START(3, 3)
  252. Z_PARAM_LONG(row)
  253. Z_PARAM_LONG(column)
  254. Z_PARAM_ZVAL(formula)
  255. ZEND_PARSE_PARAMETERS_END();
  256. ZVAL_COPY(return_value, getThis());
  257. excel_object *obj = Z_EXCEL_P(getThis());
  258. formula_writer(formula, row, column, &obj->ptr);
  259. }
  260. /* }}} */
  261. /** {{{ \Vtiful\Kernel\Excel::autoFilter(int $rowStart, int $rowEnd, int $columnStart, int $columnEnd)
  262. */
  263. PHP_METHOD(vtiful_excel, autoFilter)
  264. {
  265. zend_string *range;
  266. ZEND_PARSE_PARAMETERS_START(1, 1)
  267. Z_PARAM_STR(range)
  268. ZEND_PARSE_PARAMETERS_END();
  269. ZVAL_COPY(return_value, getThis());
  270. excel_object *obj = Z_EXCEL_P(getThis());
  271. auto_filter(range, &obj->ptr);
  272. }
  273. /* }}} */
  274. /** {{{ \Vtiful\Kernel\Excel::mergeCells(string $range, string $data)
  275. */
  276. PHP_METHOD(vtiful_excel, mergeCells)
  277. {
  278. zval res_handle;
  279. zend_string *range, *data;
  280. ZEND_PARSE_PARAMETERS_START(2, 2)
  281. Z_PARAM_STR(range)
  282. Z_PARAM_STR(data)
  283. ZEND_PARSE_PARAMETERS_END();
  284. ZVAL_COPY(return_value, getThis());
  285. excel_object *obj = Z_EXCEL_P(getThis());
  286. merge_cells(range, data, &obj->ptr);
  287. }
  288. /* }}} */
  289. /** {{{ \Vtiful\Kernel\Excel::setColumn(resource $format, string $range [, int $width])
  290. */
  291. PHP_METHOD(vtiful_excel, setColumn)
  292. {
  293. zval *format_handle, res_handle;
  294. zend_string *range;
  295. double width = 0;
  296. int argc = ZEND_NUM_ARGS();
  297. ZEND_PARSE_PARAMETERS_START(2, 3)
  298. Z_PARAM_RESOURCE(format_handle)
  299. Z_PARAM_STR(range)
  300. Z_PARAM_OPTIONAL
  301. Z_PARAM_DOUBLE(width)
  302. ZEND_PARSE_PARAMETERS_END();
  303. ZVAL_COPY(return_value, getThis());
  304. if (argc == 2) {
  305. width = 10;
  306. }
  307. excel_object *obj = Z_EXCEL_P(getThis());
  308. set_column(range, width, &obj->ptr, zval_get_format(format_handle));
  309. }
  310. /* }}} */
  311. /** {{{ \Vtiful\Kernel\Excel::setRow(resource $format, string $range [, int $heitght])
  312. */
  313. PHP_METHOD(vtiful_excel, setRow)
  314. {
  315. zval *format_handle, res_handle;
  316. zend_string *range;
  317. double height = 0;
  318. int argc = ZEND_NUM_ARGS();
  319. ZEND_PARSE_PARAMETERS_START(2, 3)
  320. Z_PARAM_RESOURCE(format_handle)
  321. Z_PARAM_STR(range)
  322. Z_PARAM_OPTIONAL
  323. Z_PARAM_DOUBLE(height)
  324. ZEND_PARSE_PARAMETERS_END();
  325. ZVAL_COPY(return_value, getThis());
  326. if (argc == 2) {
  327. height = 18;
  328. }
  329. excel_object *obj = Z_EXCEL_P(getThis());
  330. set_row(range, height, &obj->ptr, zval_get_format(format_handle));
  331. }
  332. /* }}} */
  333. /** {{{ excel_methods
  334. */
  335. zend_function_entry excel_methods[] = {
  336. PHP_ME(vtiful_excel, __construct, excel_construct_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  337. PHP_ME(vtiful_excel, fileName, excel_file_name_arginfo, ZEND_ACC_PUBLIC)
  338. PHP_ME(vtiful_excel, constMemory, excel_file_name_arginfo, ZEND_ACC_PUBLIC)
  339. PHP_ME(vtiful_excel, header, excel_header_arginfo, ZEND_ACC_PUBLIC)
  340. PHP_ME(vtiful_excel, data, excel_data_arginfo, ZEND_ACC_PUBLIC)
  341. PHP_ME(vtiful_excel, output, NULL, ZEND_ACC_PUBLIC)
  342. PHP_ME(vtiful_excel, getHandle, NULL, ZEND_ACC_PUBLIC)
  343. PHP_ME(vtiful_excel, autoFilter, excel_auto_filter_arginfo, ZEND_ACC_PUBLIC)
  344. PHP_ME(vtiful_excel, insertText, excel_insert_text_arginfo, ZEND_ACC_PUBLIC)
  345. PHP_ME(vtiful_excel, insertImage, excel_insert_image_arginfo, ZEND_ACC_PUBLIC)
  346. PHP_ME(vtiful_excel, insertFormula, excel_insert_formula_arginfo, ZEND_ACC_PUBLIC)
  347. PHP_ME(vtiful_excel, mergeCells, excel_merge_cells_arginfo, ZEND_ACC_PUBLIC)
  348. PHP_ME(vtiful_excel, setColumn, excel_set_column_arginfo, ZEND_ACC_PUBLIC)
  349. PHP_ME(vtiful_excel, setRow, excel_set_row_arginfo, ZEND_ACC_PUBLIC)
  350. PHP_FE_END
  351. };
  352. /* }}} */
  353. /** {{{ VTIFUL_STARTUP_FUNCTION
  354. */
  355. VTIFUL_STARTUP_FUNCTION(excel) {
  356. zend_class_entry ce;
  357. INIT_NS_CLASS_ENTRY(ce, "Vtiful\\Kernel", "Excel", excel_methods);
  358. ce.create_object = excel_objects_new;
  359. vtiful_excel_ce = zend_register_internal_class(&ce);
  360. memcpy(&vtiful_excel_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  361. vtiful_excel_handlers.offset = XtOffsetOf(excel_object, zo);
  362. vtiful_excel_handlers.free_obj = vtiful_excel_objects_free;
  363. REGISTER_CLASS_PROPERTY_NULL(vtiful_excel_ce, V_EXCEL_COF, ZEND_ACC_PRIVATE);
  364. REGISTER_CLASS_PROPERTY_NULL(vtiful_excel_ce, V_EXCEL_FIL, ZEND_ACC_PRIVATE);
  365. return SUCCESS;
  366. }
  367. /* }}} */