excel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. +----------------------------------------------------------------------+
  3. | XlsWriter Extension |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2017-2018 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_xls_ce;
  17. static zend_object_handlers vtiful_xls_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. /* {{{ xls_objects_new
  24. */
  25. PHP_VTIFUL_API zend_object *vtiful_xls_objects_new(zend_class_entry *ce)
  26. {
  27. xls_object *intern = vtiful_object_alloc(sizeof(xls_object), ce);
  28. zend_object_std_init(&intern->zo, ce);
  29. object_properties_init(&intern->zo, ce);
  30. intern->zo.handlers = &vtiful_xls_handlers;
  31. return &intern->zo;
  32. }
  33. /* }}} */
  34. /* {{{ vtiful_xls_objects_free
  35. */
  36. static void vtiful_xls_objects_free(zend_object *object)
  37. {
  38. xls_object *intern = php_vtiful_xls_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(xls_construct_arginfo, 0, 0, 1)
  46. ZEND_ARG_INFO(0, config)
  47. ZEND_END_ARG_INFO()
  48. ZEND_BEGIN_ARG_INFO_EX(xls_file_name_arginfo, 0, 0, 1)
  49. ZEND_ARG_INFO(0, file_name)
  50. ZEND_END_ARG_INFO()
  51. ZEND_BEGIN_ARG_INFO_EX(xls_header_arginfo, 0, 0, 1)
  52. ZEND_ARG_INFO(0, header)
  53. ZEND_END_ARG_INFO()
  54. ZEND_BEGIN_ARG_INFO_EX(xls_data_arginfo, 0, 0, 1)
  55. ZEND_ARG_INFO(0, data)
  56. ZEND_END_ARG_INFO()
  57. ZEND_BEGIN_ARG_INFO_EX(xls_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(xls_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(xls_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(xls_auto_filter_arginfo, 0, 0, 1)
  74. ZEND_ARG_INFO(0, range)
  75. ZEND_END_ARG_INFO()
  76. ZEND_BEGIN_ARG_INFO_EX(xls_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(xls_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(xls_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\xls::__construct(array $config)
  92. */
  93. PHP_METHOD(vtiful_xls, __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_XLS_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_XLS_COF, config);
  110. }
  111. /* }}} */
  112. /** {{{ \Vtiful\Kernel\xls::filename(string $fileName)
  113. */
  114. PHP_METHOD(vtiful_xls, 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_xls_ce, return_value);
  123. xls_object *obj = Z_XLS_P(getThis());
  124. if(obj->ptr.workbook == NULL) {
  125. xls_file_path(file_name, dir_path, &file_path);
  126. obj->ptr.workbook = workbook_new(Z_STRVAL(file_path));
  127. obj->ptr.worksheet = workbook_add_worksheet(obj->ptr.workbook, NULL);
  128. add_property_zval(return_value, V_XLS_FIL, &file_path);
  129. zval_ptr_dtor(&file_path);
  130. }
  131. }
  132. /* }}} */
  133. /** {{{ \Vtiful\Kernel\xls::constMemory(string $fileName)
  134. */
  135. PHP_METHOD(vtiful_xls, constMemory)
  136. {
  137. zval file_path, *dir_path;
  138. zend_string *file_name;
  139. ZEND_PARSE_PARAMETERS_START(1, 1)
  140. Z_PARAM_STR(file_name)
  141. ZEND_PARSE_PARAMETERS_END();
  142. ZVAL_COPY(return_value, getThis());
  143. GET_CONFIG_PATH(dir_path, vtiful_xls_ce, return_value);
  144. xls_object *obj = Z_XLS_P(getThis());
  145. if(obj->ptr.workbook == NULL) {
  146. xls_file_path(file_name, dir_path, &file_path);
  147. lxw_workbook_options options = {.constant_memory = LXW_TRUE, .tmpdir = NULL};
  148. obj->ptr.workbook = workbook_new_opt(Z_STRVAL(file_path), &options);
  149. obj->ptr.worksheet = workbook_add_worksheet(obj->ptr.workbook, NULL);
  150. add_property_zval(return_value, V_XLS_FIL, &file_path);
  151. zval_ptr_dtor(&file_path);
  152. }
  153. }
  154. /* }}} */
  155. /** {{{ \Vtiful\Kernel\xls::header(array $header)
  156. */
  157. PHP_METHOD(vtiful_xls, header)
  158. {
  159. zval *header, *header_value;
  160. zend_long header_l_key;
  161. ZEND_PARSE_PARAMETERS_START(1, 1)
  162. Z_PARAM_ARRAY(header)
  163. ZEND_PARSE_PARAMETERS_END();
  164. ZVAL_COPY(return_value, getThis());
  165. xls_object *obj = Z_XLS_P(getThis());
  166. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(header), header_l_key, header_value) {
  167. type_writer(header_value, 0, header_l_key, &obj->ptr, NULL);
  168. zval_ptr_dtor(header_value);
  169. } ZEND_HASH_FOREACH_END();
  170. }
  171. /* }}} */
  172. /** {{{ \Vtiful\Kernel\xls::data(array $data)
  173. */
  174. PHP_METHOD(vtiful_xls, data)
  175. {
  176. zval *data, *data_r_value, *data_l_value;
  177. zend_long data_r_key, data_l_key;
  178. ZEND_PARSE_PARAMETERS_START(1, 1)
  179. Z_PARAM_ARRAY(data)
  180. ZEND_PARSE_PARAMETERS_END();
  181. ZVAL_COPY(return_value, getThis());
  182. xls_object *obj = Z_XLS_P(getThis());
  183. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data), data_r_key, data_r_value) {
  184. if(Z_TYPE_P(data_r_value) == IS_ARRAY) {
  185. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data_r_value), data_l_key, data_l_value) {
  186. type_writer(data_l_value, data_r_key+1, data_l_key, &obj->ptr, NULL);
  187. zval_ptr_dtor(data_l_value);
  188. } ZEND_HASH_FOREACH_END();
  189. }
  190. } ZEND_HASH_FOREACH_END();
  191. }
  192. /* }}} */
  193. /** {{{ \Vtiful\Kernel\xls::output()
  194. */
  195. PHP_METHOD(vtiful_xls, output)
  196. {
  197. zval rv, *file_path;
  198. file_path = zend_read_property(vtiful_xls_ce, getThis(), ZEND_STRL(V_XLS_FIL), 0, &rv TSRMLS_DC);
  199. xls_object *obj = Z_XLS_P(getThis());
  200. workbook_file(&obj->ptr);
  201. add_property_null(getThis(), V_XLS_HANDLE);
  202. add_property_null(getThis(), V_XLS_PAT);
  203. ZVAL_COPY(return_value, file_path);
  204. }
  205. /* }}} */
  206. /** {{{ \Vtiful\Kernel\xls::getHandle()
  207. */
  208. PHP_METHOD(vtiful_xls, getHandle)
  209. {
  210. xls_object *obj = Z_XLS_P(getThis());
  211. RETURN_RES(zend_register_resource(&obj->ptr, le_xls_writer));
  212. }
  213. /* }}} */
  214. /** {{{ \Vtiful\Kernel\xls::insertText(int $row, int $column, string|int|double $data)
  215. */
  216. PHP_METHOD(vtiful_xls, insertText)
  217. {
  218. zval *data;
  219. zend_long row, column;
  220. zend_string *format = NULL;
  221. ZEND_PARSE_PARAMETERS_START(3, 4)
  222. Z_PARAM_LONG(row)
  223. Z_PARAM_LONG(column)
  224. Z_PARAM_ZVAL(data)
  225. Z_PARAM_OPTIONAL
  226. Z_PARAM_STR(format)
  227. ZEND_PARSE_PARAMETERS_END();
  228. ZVAL_COPY(return_value, getThis());
  229. xls_object *obj = Z_XLS_P(getThis());
  230. type_writer(data, row, column, &obj->ptr, format);
  231. }
  232. /* }}} */
  233. /** {{{ \Vtiful\Kernel\xls::insertImage(int $row, int $column, string $imagePath)
  234. */
  235. PHP_METHOD(vtiful_xls, insertImage)
  236. {
  237. zval *image;
  238. zend_long row, column;
  239. ZEND_PARSE_PARAMETERS_START(3, 3)
  240. Z_PARAM_LONG(row)
  241. Z_PARAM_LONG(column)
  242. Z_PARAM_ZVAL(image)
  243. ZEND_PARSE_PARAMETERS_END();
  244. ZVAL_COPY(return_value, getThis());
  245. xls_object *obj = Z_XLS_P(getThis());
  246. image_writer(image, row, column, &obj->ptr);
  247. }
  248. /* }}} */
  249. /** {{{ \Vtiful\Kernel\xls::insertImage(int $row, int $column, string $imagePath)
  250. */
  251. PHP_METHOD(vtiful_xls, insertFormula)
  252. {
  253. zval *formula;
  254. zend_long row, column;
  255. ZEND_PARSE_PARAMETERS_START(3, 3)
  256. Z_PARAM_LONG(row)
  257. Z_PARAM_LONG(column)
  258. Z_PARAM_ZVAL(formula)
  259. ZEND_PARSE_PARAMETERS_END();
  260. ZVAL_COPY(return_value, getThis());
  261. xls_object *obj = Z_XLS_P(getThis());
  262. formula_writer(formula, row, column, &obj->ptr);
  263. }
  264. /* }}} */
  265. /** {{{ \Vtiful\Kernel\xls::autoFilter(int $rowStart, int $rowEnd, int $columnStart, int $columnEnd)
  266. */
  267. PHP_METHOD(vtiful_xls, autoFilter)
  268. {
  269. zend_string *range;
  270. ZEND_PARSE_PARAMETERS_START(1, 1)
  271. Z_PARAM_STR(range)
  272. ZEND_PARSE_PARAMETERS_END();
  273. ZVAL_COPY(return_value, getThis());
  274. xls_object *obj = Z_XLS_P(getThis());
  275. auto_filter(range, &obj->ptr);
  276. }
  277. /* }}} */
  278. /** {{{ \Vtiful\Kernel\xls::mergeCells(string $range, string $data)
  279. */
  280. PHP_METHOD(vtiful_xls, mergeCells)
  281. {
  282. zend_string *range, *data;
  283. ZEND_PARSE_PARAMETERS_START(2, 2)
  284. Z_PARAM_STR(range)
  285. Z_PARAM_STR(data)
  286. ZEND_PARSE_PARAMETERS_END();
  287. ZVAL_COPY(return_value, getThis());
  288. xls_object *obj = Z_XLS_P(getThis());
  289. merge_cells(range, data, &obj->ptr);
  290. }
  291. /* }}} */
  292. /** {{{ \Vtiful\Kernel\xls::setColumn(resource $format, string $range [, int $width])
  293. */
  294. PHP_METHOD(vtiful_xls, setColumn)
  295. {
  296. zval *format_handle;
  297. zend_string *range;
  298. double width = 0;
  299. int argc = ZEND_NUM_ARGS();
  300. ZEND_PARSE_PARAMETERS_START(2, 3)
  301. Z_PARAM_RESOURCE(format_handle)
  302. Z_PARAM_STR(range)
  303. Z_PARAM_OPTIONAL
  304. Z_PARAM_DOUBLE(width)
  305. ZEND_PARSE_PARAMETERS_END();
  306. ZVAL_COPY(return_value, getThis());
  307. if (argc == 2) {
  308. width = 10;
  309. }
  310. xls_object *obj = Z_XLS_P(getThis());
  311. set_column(range, width, &obj->ptr, zval_get_format(format_handle));
  312. }
  313. /* }}} */
  314. /** {{{ \Vtiful\Kernel\xls::setRow(resource $format, string $range [, int $heitght])
  315. */
  316. PHP_METHOD(vtiful_xls, setRow)
  317. {
  318. zval *format_handle;
  319. zend_string *range;
  320. double height = 0;
  321. int argc = ZEND_NUM_ARGS();
  322. ZEND_PARSE_PARAMETERS_START(2, 3)
  323. Z_PARAM_RESOURCE(format_handle)
  324. Z_PARAM_STR(range)
  325. Z_PARAM_OPTIONAL
  326. Z_PARAM_DOUBLE(height)
  327. ZEND_PARSE_PARAMETERS_END();
  328. ZVAL_COPY(return_value, getThis());
  329. if (argc == 2) {
  330. height = 18;
  331. }
  332. xls_object *obj = Z_XLS_P(getThis());
  333. set_row(range, height, &obj->ptr, zval_get_format(format_handle));
  334. }
  335. /* }}} */
  336. /** {{{ xls_methods
  337. */
  338. zend_function_entry xls_methods[] = {
  339. PHP_ME(vtiful_xls, __construct, xls_construct_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  340. PHP_ME(vtiful_xls, fileName, xls_file_name_arginfo, ZEND_ACC_PUBLIC)
  341. PHP_ME(vtiful_xls, constMemory, xls_file_name_arginfo, ZEND_ACC_PUBLIC)
  342. PHP_ME(vtiful_xls, header, xls_header_arginfo, ZEND_ACC_PUBLIC)
  343. PHP_ME(vtiful_xls, data, xls_data_arginfo, ZEND_ACC_PUBLIC)
  344. PHP_ME(vtiful_xls, output, NULL, ZEND_ACC_PUBLIC)
  345. PHP_ME(vtiful_xls, getHandle, NULL, ZEND_ACC_PUBLIC)
  346. PHP_ME(vtiful_xls, autoFilter, xls_auto_filter_arginfo, ZEND_ACC_PUBLIC)
  347. PHP_ME(vtiful_xls, insertText, xls_insert_text_arginfo, ZEND_ACC_PUBLIC)
  348. PHP_ME(vtiful_xls, insertImage, xls_insert_image_arginfo, ZEND_ACC_PUBLIC)
  349. PHP_ME(vtiful_xls, insertFormula, xls_insert_formula_arginfo, ZEND_ACC_PUBLIC)
  350. PHP_ME(vtiful_xls, mergeCells, xls_merge_cells_arginfo, ZEND_ACC_PUBLIC)
  351. PHP_ME(vtiful_xls, setColumn, xls_set_column_arginfo, ZEND_ACC_PUBLIC)
  352. PHP_ME(vtiful_xls, setRow, xls_set_row_arginfo, ZEND_ACC_PUBLIC)
  353. PHP_FE_END
  354. };
  355. /* }}} */
  356. /** {{{ VTIFUL_STARTUP_FUNCTION
  357. */
  358. VTIFUL_STARTUP_FUNCTION(excel) {
  359. zend_class_entry ce;
  360. INIT_NS_CLASS_ENTRY(ce, "Vtiful\\Kernel", "Excel", xls_methods);
  361. ce.create_object = vtiful_xls_objects_new;
  362. vtiful_xls_ce = zend_register_internal_class(&ce);
  363. memcpy(&vtiful_xls_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  364. vtiful_xls_handlers.offset = XtOffsetOf(xls_object, zo);
  365. vtiful_xls_handlers.free_obj = vtiful_xls_objects_free;
  366. REGISTER_CLASS_PROPERTY_NULL(vtiful_xls_ce, V_XLS_COF, ZEND_ACC_PRIVATE);
  367. REGISTER_CLASS_PROPERTY_NULL(vtiful_xls_ce, V_XLS_FIL, ZEND_ACC_PRIVATE);
  368. return SUCCESS;
  369. }
  370. /* }}} */