excel.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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, *config, *tmp_path, file_path;
  53. zend_string *file_name, *key;
  54. ZEND_PARSE_PARAMETERS_START(1, 1)
  55. Z_PARAM_STR(file_name)
  56. ZEND_PARSE_PARAMETERS_END();
  57. ZVAL_COPY(return_value, getThis());
  58. key = zend_string_init(V_EXCEL_PAT, sizeof(V_EXCEL_PAT)-1, 0);
  59. config = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_COF), 0, &rv TSRMLS_DC);
  60. tmp_path = zend_hash_find(Z_ARRVAL_P(config), key);
  61. zend_string_release(key);
  62. if(!tmp_path && Z_TYPE_P(tmp_path) != IS_STRING)
  63. {
  64. zend_throw_exception(vtiful_exception_ce, "Configure 'path' must be a string type", 120);
  65. }
  66. ZVAL_STR(&tmp_file_name, file_name);
  67. concat_function(&file_path, tmp_path, &tmp_file_name);
  68. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_FIL), &file_path);
  69. zval_ptr_dtor(&file_path);
  70. }
  71. /* }}} */
  72. /* {{{ \Vtiful\Kernel\Excel::header(array $header)
  73. */
  74. PHP_METHOD(vtiful_excel, header)
  75. {
  76. zval *header;
  77. ZEND_PARSE_PARAMETERS_START(1, 1)
  78. Z_PARAM_ARRAY(header)
  79. ZEND_PARSE_PARAMETERS_END();
  80. ZVAL_COPY(return_value, getThis());
  81. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HEADER), header);
  82. }
  83. /* }}} */
  84. /* {{{ \Vtiful\Kernel\Excel::data(array $data)
  85. */
  86. PHP_METHOD(vtiful_excel, data)
  87. {
  88. zval *data;
  89. ZEND_PARSE_PARAMETERS_START(1, 1)
  90. Z_PARAM_ARRAY(data)
  91. ZEND_PARSE_PARAMETERS_END();
  92. ZVAL_COPY(return_value, getThis());
  93. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_DATA), data);
  94. }
  95. /* }}} */
  96. /* {{{ \Vtiful\Kernel\Excel::output()
  97. */
  98. PHP_METHOD(vtiful_excel, output)
  99. {
  100. zval rv1, rv2, rv3;
  101. zval *file_name, *header, *data, *value, *data_r_value, *data_l_value;
  102. zend_long header_l_key, data_r_key, data_l_key;
  103. excel_resource_t *res;
  104. file_name = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_FIL), 0, &rv1 TSRMLS_DC);
  105. header = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HEADER), 0, &rv2 TSRMLS_DC);
  106. data = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_DATA), 0, &rv3 TSRMLS_DC);
  107. res = malloc(sizeof(excel_resource_t));
  108. res->workbook = workbook_new(ZSTR_VAL(zval_get_string(file_name)));
  109. res->worksheet = workbook_add_worksheet(res->workbook, NULL);
  110. zval_ptr_dtor(file_name);
  111. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(header), header_l_key, value) {
  112. worksheet_write_string(res->worksheet, 0, header_l_key, ZSTR_VAL(zval_get_string(value)), NULL);
  113. zval_ptr_dtor(value);
  114. } ZEND_HASH_FOREACH_END();
  115. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data), data_r_key, data_r_value) {
  116. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data_r_value), data_l_key, data_l_value) {
  117. switch (Z_TYPE_P(data_l_value)) {
  118. case IS_STRING:
  119. worksheet_write_string(res->worksheet, data_r_key+1, data_l_key, ZSTR_VAL(zval_get_string(data_l_value)), NULL);
  120. zval_ptr_dtor(data_l_value);
  121. break;
  122. case IS_LONG:
  123. worksheet_write_number(res->worksheet, data_r_key+1, data_l_key, zval_get_long(data_l_value), NULL);
  124. break;
  125. }
  126. } ZEND_HASH_FOREACH_END();
  127. } ZEND_HASH_FOREACH_END();
  128. workbook_close(res->workbook);
  129. }
  130. /* }}} */
  131. zend_function_entry excel_methods[] = {
  132. PHP_ME(vtiful_excel, __construct, excel_construct_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  133. PHP_ME(vtiful_excel, fileName, excel_file_name_arginfo, ZEND_ACC_PUBLIC)
  134. PHP_ME(vtiful_excel, header, excel_header_arginfo, ZEND_ACC_PUBLIC)
  135. PHP_ME(vtiful_excel, data, excel_data_arginfo, ZEND_ACC_PUBLIC)
  136. PHP_ME(vtiful_excel, output, NULL, ZEND_ACC_PUBLIC)
  137. PHP_FE_END
  138. };
  139. VTIFUL_STARTUP_FUNCTION(excel) {
  140. zend_class_entry ce;
  141. INIT_NS_CLASS_ENTRY(ce, "Vtiful\\Kernel", "Excel", excel_methods);
  142. vtiful_excel_ce = zend_register_internal_class(&ce);
  143. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_COF), ZEND_ACC_PRIVATE);
  144. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_FIL), ZEND_ACC_PRIVATE);
  145. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_HEADER), ZEND_ACC_PRIVATE);
  146. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_DATA), ZEND_ACC_PRIVATE);
  147. return SUCCESS;
  148. }