excel.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #ifdef HAVE_CONFIG_H
  2. #include "config.h"
  3. #endif
  4. #include "zend.h"
  5. #include "zend_API.h"
  6. #include "zend_exceptions.h"
  7. #include "php.h"
  8. #include "xlsxwriter.h"
  9. #include "php_vtiful.h"
  10. #include "excel.h"
  11. #include "exception.h"
  12. #include "ext/standard/php_var.h"
  13. typedef struct {
  14. lxw_workbook *workbook;
  15. lxw_worksheet *worksheet;
  16. } excel_resource_t;
  17. zend_class_entry *vtiful_excel_ce;
  18. ZEND_BEGIN_ARG_INFO_EX(excel_construct_arginfo, 0, 0, 1)
  19. ZEND_ARG_INFO(0, config)
  20. ZEND_END_ARG_INFO()
  21. ZEND_BEGIN_ARG_INFO_EX(excel_file_name_arginfo, 0, 0, 1)
  22. ZEND_ARG_INFO(0, file_name)
  23. ZEND_END_ARG_INFO()
  24. ZEND_BEGIN_ARG_INFO_EX(excel_header_arginfo, 0, 0, 1)
  25. ZEND_ARG_INFO(0, header)
  26. ZEND_END_ARG_INFO()
  27. ZEND_BEGIN_ARG_INFO_EX(excel_data_arginfo, 0, 0, 1)
  28. ZEND_ARG_INFO(0, data)
  29. ZEND_END_ARG_INFO()
  30. /* {{{ \Vtiful\Kernel\Excel::__construct(array $config)
  31. */
  32. PHP_METHOD(vtiful_excel, __construct)
  33. {
  34. zval *config;
  35. zend_string *key;
  36. ZEND_PARSE_PARAMETERS_START(1, 1)
  37. Z_PARAM_ARRAY(config)
  38. ZEND_PARSE_PARAMETERS_END();
  39. key = zend_string_init(V_EXCEL_PAT, sizeof(V_EXCEL_PAT)-1, 0);
  40. if(zend_hash_find(Z_ARRVAL_P(config), key) == NULL)
  41. {
  42. zend_throw_exception(vtiful_exception_ce, "Lack of 'path' configuration", 110);
  43. }
  44. zend_update_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_COF), config);
  45. zend_string_release(key);
  46. }
  47. /* }}} */
  48. /* {{{ \Vtiful\Kernel\Excel::filename(string $fileName)
  49. */
  50. PHP_METHOD(vtiful_excel, fileName)
  51. {
  52. zval rv, tmp_file_name, file_path, handle, *config, *tmp_path;
  53. zend_string *file_name, *key;
  54. excel_resource_t *res;
  55. ZEND_PARSE_PARAMETERS_START(1, 1)
  56. Z_PARAM_STR(file_name)
  57. ZEND_PARSE_PARAMETERS_END();
  58. ZVAL_COPY(return_value, getThis());
  59. key = zend_string_init(V_EXCEL_PAT, sizeof(V_EXCEL_PAT)-1, 0);
  60. config = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_COF), 0, &rv TSRMLS_DC);
  61. tmp_path = zend_hash_find(Z_ARRVAL_P(config), key);
  62. zend_string_release(key);
  63. if(!tmp_path && Z_TYPE_P(tmp_path) != IS_STRING)
  64. {
  65. zend_throw_exception(vtiful_exception_ce, "Configure 'path' must be a string type", 120);
  66. }
  67. ZVAL_STR(&tmp_file_name, file_name);
  68. concat_function(&file_path, tmp_path, &tmp_file_name);
  69. res = malloc(sizeof(excel_resource_t));
  70. res->workbook = workbook_new(ZSTR_VAL(zval_get_string(&file_path)));
  71. res->worksheet = workbook_add_worksheet(res->workbook, NULL);
  72. ZVAL_RES(&handle, zend_register_resource(res, le_vtiful));
  73. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_FIL), &file_path);
  74. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &handle);
  75. zval_ptr_dtor(&file_path);
  76. zval_ptr_dtor(&file_path);
  77. }
  78. /* }}} */
  79. /* {{{ \Vtiful\Kernel\Excel::header(array $header)
  80. */
  81. PHP_METHOD(vtiful_excel, header)
  82. {
  83. zval *header;
  84. ZEND_PARSE_PARAMETERS_START(1, 1)
  85. Z_PARAM_ARRAY(header)
  86. ZEND_PARSE_PARAMETERS_END();
  87. ZVAL_COPY(return_value, getThis());
  88. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HEADER), header);
  89. }
  90. /* }}} */
  91. /* {{{ \Vtiful\Kernel\Excel::data(array $data)
  92. */
  93. PHP_METHOD(vtiful_excel, data)
  94. {
  95. zval *data;
  96. ZEND_PARSE_PARAMETERS_START(1, 1)
  97. Z_PARAM_ARRAY(data)
  98. ZEND_PARSE_PARAMETERS_END();
  99. ZVAL_COPY(return_value, getThis());
  100. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_DATA), data);
  101. }
  102. /* }}} */
  103. /* {{{ \Vtiful\Kernel\Excel::output()
  104. */
  105. PHP_METHOD(vtiful_excel, output)
  106. {
  107. zval rv1, rv2, rv3;
  108. zval *file_name, *header, *data, *value, *data_r_value, *data_l_value;
  109. zend_long header_l_key, data_r_key, data_l_key;
  110. excel_resource_t *res;
  111. file_name = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_FIL), 0, &rv1 TSRMLS_DC);
  112. header = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HEADER), 0, &rv2 TSRMLS_DC);
  113. data = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_DATA), 0, &rv3 TSRMLS_DC);
  114. res = malloc(sizeof(excel_resource_t));
  115. res->workbook = workbook_new(ZSTR_VAL(zval_get_string(file_name)));
  116. res->worksheet = workbook_add_worksheet(res->workbook, NULL);
  117. zval_ptr_dtor(file_name);
  118. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(header), header_l_key, value) {
  119. worksheet_write_string(res->worksheet, 0, header_l_key, ZSTR_VAL(zval_get_string(value)), NULL);
  120. zval_ptr_dtor(value);
  121. } ZEND_HASH_FOREACH_END();
  122. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data), data_r_key, data_r_value) {
  123. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data_r_value), data_l_key, data_l_value) {
  124. switch (Z_TYPE_P(data_l_value)) {
  125. case IS_STRING:
  126. worksheet_write_string(res->worksheet, data_r_key+1, data_l_key, ZSTR_VAL(zval_get_string(data_l_value)), NULL);
  127. zval_ptr_dtor(data_l_value);
  128. break;
  129. case IS_LONG:
  130. worksheet_write_number(res->worksheet, data_r_key+1, data_l_key, zval_get_long(data_l_value), NULL);
  131. break;
  132. }
  133. } ZEND_HASH_FOREACH_END();
  134. } ZEND_HASH_FOREACH_END();
  135. workbook_close(res->workbook);
  136. }
  137. /* }}} */
  138. /* {{{ \Vtiful\Kernel\Excel::getHandle()
  139. */
  140. PHP_METHOD(vtiful_excel, getHandle)
  141. {
  142. zval rv;
  143. zval *file_name;
  144. excel_resource_t *res;
  145. file_name = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_FIL), 0, &rv TSRMLS_DC);
  146. res = malloc(sizeof(excel_resource_t));
  147. res->workbook = workbook_new(ZSTR_VAL(zval_get_string(file_name)));
  148. res->worksheet = workbook_add_worksheet(res->workbook, NULL);
  149. zval_ptr_dtor(file_name);
  150. RETURN_RES(zend_register_resource(res, le_vtiful));
  151. }
  152. /* }}} */
  153. zend_function_entry excel_methods[] = {
  154. PHP_ME(vtiful_excel, __construct, excel_construct_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  155. PHP_ME(vtiful_excel, fileName, excel_file_name_arginfo, ZEND_ACC_PUBLIC)
  156. PHP_ME(vtiful_excel, header, excel_header_arginfo, ZEND_ACC_PUBLIC)
  157. PHP_ME(vtiful_excel, data, excel_data_arginfo, ZEND_ACC_PUBLIC)
  158. PHP_ME(vtiful_excel, output, NULL, ZEND_ACC_PUBLIC)
  159. PHP_ME(vtiful_excel, getHandle, NULL, ZEND_ACC_PUBLIC)
  160. PHP_FE_END
  161. };
  162. VTIFUL_STARTUP_FUNCTION(excel) {
  163. zend_class_entry ce;
  164. INIT_NS_CLASS_ENTRY(ce, "Vtiful\\Kernel", "Excel", excel_methods);
  165. vtiful_excel_ce = zend_register_internal_class(&ce);
  166. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_COF), ZEND_ACC_PRIVATE);
  167. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_FIL), ZEND_ACC_PRIVATE);
  168. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_DATA), ZEND_ACC_PRIVATE);
  169. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_HEADER), ZEND_ACC_PRIVATE);
  170. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_HANDLE), ZEND_ACC_PRIVATE);
  171. return SUCCESS;
  172. }