excel.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. /* {{{ \Vtiful\Kernel\Excel::__construct(array $config)
  37. */
  38. PHP_METHOD(vtiful_excel, __construct)
  39. {
  40. zval *config;
  41. zend_string *key;
  42. ZEND_PARSE_PARAMETERS_START(1, 1)
  43. Z_PARAM_ARRAY(config)
  44. ZEND_PARSE_PARAMETERS_END();
  45. key = zend_string_init(V_EXCEL_PAT, sizeof(V_EXCEL_PAT)-1, 0);
  46. if(zend_hash_find(Z_ARRVAL_P(config), key) == NULL)
  47. {
  48. zend_throw_exception(vtiful_exception_ce, "Lack of 'path' configuration", 110);
  49. }
  50. zend_update_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_COF), config);
  51. zend_string_release(key);
  52. }
  53. /* }}} */
  54. /* {{{ \Vtiful\Kernel\Excel::filename(string $fileName)
  55. */
  56. PHP_METHOD(vtiful_excel, fileName)
  57. {
  58. zval rv, tmp_file_name, file_path, handle, *config, *tmp_path;
  59. zend_string *file_name, *key;
  60. excel_resource_t *res;
  61. ZEND_PARSE_PARAMETERS_START(1, 1)
  62. Z_PARAM_STR(file_name)
  63. ZEND_PARSE_PARAMETERS_END();
  64. ZVAL_COPY(return_value, getThis());
  65. key = zend_string_init(V_EXCEL_PAT, sizeof(V_EXCEL_PAT)-1, 0);
  66. config = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_COF), 0, &rv TSRMLS_DC);
  67. tmp_path = zend_hash_find(Z_ARRVAL_P(config), key);
  68. zend_string_release(key);
  69. if(!tmp_path && Z_TYPE_P(tmp_path) != IS_STRING)
  70. {
  71. zend_throw_exception(vtiful_exception_ce, "Configure 'path' must be a string type", 120);
  72. }
  73. ZVAL_STR(&tmp_file_name, file_name);
  74. concat_function(&file_path, tmp_path, &tmp_file_name);
  75. res = malloc(sizeof(excel_resource_t));
  76. res->workbook = workbook_new(ZSTR_VAL(zval_get_string(&file_path)));
  77. res->worksheet = workbook_add_worksheet(res->workbook, NULL);
  78. ZVAL_RES(&handle, zend_register_resource(res, le_vtiful));
  79. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_FIL), &file_path);
  80. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &handle);
  81. zval_ptr_dtor(&file_path);
  82. zval_ptr_dtor(&file_path);
  83. }
  84. /* }}} */
  85. /* {{{ \Vtiful\Kernel\Excel::header(array $header)
  86. */
  87. PHP_METHOD(vtiful_excel, header)
  88. {
  89. zval rv, res_handle, *attr_handle, *header, *header_value;
  90. zend_long header_l_key;
  91. excel_resource_t *res;
  92. ZEND_PARSE_PARAMETERS_START(1, 1)
  93. Z_PARAM_ARRAY(header)
  94. ZEND_PARSE_PARAMETERS_END();
  95. ZVAL_COPY(return_value, getThis());
  96. attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  97. if((res = (excel_resource_t *)zend_fetch_resource(Z_RES_P(attr_handle), VTIFUL_RESOURCE_NAME, le_vtiful)) == NULL) {
  98. zend_throw_exception(vtiful_exception_ce, "Excel resources resolution fail", 210);
  99. }
  100. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(header), header_l_key, header_value) {
  101. type_writer(header_value, 0, header_l_key, res);
  102. zval_ptr_dtor(header_value);
  103. } ZEND_HASH_FOREACH_END();
  104. ZVAL_RES(&res_handle, zend_register_resource(res, le_vtiful));
  105. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  106. }
  107. /* }}} */
  108. /* {{{ \Vtiful\Kernel\Excel::data(array $data)
  109. */
  110. PHP_METHOD(vtiful_excel, data)
  111. {
  112. zval rv, *data, *attr_handle, res_handle, *data_r_value, *data_l_value;
  113. zend_long data_r_key, data_l_key;
  114. excel_resource_t *res;
  115. ZEND_PARSE_PARAMETERS_START(1, 1)
  116. Z_PARAM_ARRAY(data)
  117. ZEND_PARSE_PARAMETERS_END();
  118. ZVAL_COPY(return_value, getThis());
  119. attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  120. if((res = (excel_resource_t *)zend_fetch_resource(Z_RES_P(attr_handle), VTIFUL_RESOURCE_NAME, le_vtiful)) == NULL) {
  121. zend_throw_exception(vtiful_exception_ce, "Excel resources resolution fail", 210);
  122. }
  123. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data), data_r_key, data_r_value) {
  124. if(Z_TYPE_P(data_r_value) == IS_ARRAY) {
  125. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data_r_value), data_l_key, data_l_value) {
  126. type_writer(data_l_value, data_r_key+1, data_l_key, res);
  127. zval_ptr_dtor(data_l_value);
  128. } ZEND_HASH_FOREACH_END();
  129. }
  130. } ZEND_HASH_FOREACH_END();
  131. ZVAL_RES(&res_handle, zend_register_resource(res, le_vtiful));
  132. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
  133. }
  134. /* }}} */
  135. /* {{{ \Vtiful\Kernel\Excel::output()
  136. */
  137. PHP_METHOD(vtiful_excel, output)
  138. {
  139. zval rv, *handle, null_handle;
  140. excel_resource_t *res;
  141. handle = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  142. if((res = (excel_resource_t *)zend_fetch_resource(Z_RES_P(handle), VTIFUL_RESOURCE_NAME, le_vtiful)) == NULL) {
  143. zend_throw_exception(vtiful_exception_ce, "Excel resources resolution fail", 210);
  144. }
  145. workbook_file(res, handle);
  146. ZVAL_NULL(&null_handle);
  147. zend_update_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HANDLE), &null_handle);
  148. }
  149. /* }}} */
  150. /* {{{ \Vtiful\Kernel\Excel::getHandle()
  151. */
  152. PHP_METHOD(vtiful_excel, getHandle)
  153. {
  154. zval rv;
  155. zval *handle;
  156. handle = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
  157. ZVAL_COPY(return_value, handle);
  158. }
  159. /* }}} */
  160. zend_function_entry excel_methods[] = {
  161. PHP_ME(vtiful_excel, __construct, excel_construct_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  162. PHP_ME(vtiful_excel, fileName, excel_file_name_arginfo, ZEND_ACC_PUBLIC)
  163. PHP_ME(vtiful_excel, header, excel_header_arginfo, ZEND_ACC_PUBLIC)
  164. PHP_ME(vtiful_excel, data, excel_data_arginfo, ZEND_ACC_PUBLIC)
  165. PHP_ME(vtiful_excel, output, NULL, ZEND_ACC_PUBLIC)
  166. PHP_ME(vtiful_excel, getHandle, NULL, ZEND_ACC_PUBLIC)
  167. PHP_FE_END
  168. };
  169. VTIFUL_STARTUP_FUNCTION(excel) {
  170. zend_class_entry ce;
  171. INIT_NS_CLASS_ENTRY(ce, "Vtiful\\Kernel", "Excel", excel_methods);
  172. vtiful_excel_ce = zend_register_internal_class(&ce);
  173. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_COF), ZEND_ACC_PRIVATE);
  174. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_FIL), ZEND_ACC_PRIVATE);
  175. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_HANDLE), ZEND_ACC_PRIVATE);
  176. return SUCCESS;
  177. }