/public/memberList.php

https://github.com/ciniki/customers · PHP · 213 lines · 149 code · 7 blank · 57 comment · 24 complexity · 49a443ab1dd687b2bf1876d605b71781 MD5 · raw file

  1. <?php
  2. //
  3. // Description
  4. // -----------
  5. //
  6. // Arguments
  7. // ---------
  8. // api_key:
  9. // auth_token:
  10. // tnid: The ID of the tenant to get members for.
  11. // type: The type of participants to get. Refer to participantAdd for
  12. // more information on types.
  13. //
  14. // Returns
  15. // -------
  16. //
  17. function ciniki_customers_memberList($ciniki) {
  18. //
  19. // Find all the required and optional arguments
  20. //
  21. ciniki_core_loadMethod($ciniki, 'ciniki', 'core', 'private', 'prepareArgs');
  22. $rc = ciniki_core_prepareArgs($ciniki, 'no', array(
  23. 'tnid'=>array('required'=>'yes', 'blank'=>'no', 'name'=>'Tenant'),
  24. 'category'=>array('required'=>'no', 'blank'=>'yes', 'name'=>'Category'),
  25. ));
  26. if( $rc['stat'] != 'ok' ) {
  27. return $rc;
  28. }
  29. $args = $rc['args'];
  30. //
  31. // Check access to tnid as owner, or sys admin.
  32. //
  33. ciniki_core_loadMethod($ciniki, 'ciniki', 'customers', 'private', 'checkAccess');
  34. $ac = ciniki_customers_checkAccess($ciniki, $args['tnid'], 'ciniki.customers.memberList', 0);
  35. if( $ac['stat'] != 'ok' ) {
  36. return $ac;
  37. }
  38. //
  39. // Load maps
  40. //
  41. ciniki_core_loadMethod($ciniki, 'ciniki', 'customers', 'private', 'maps');
  42. $rc = ciniki_customers_maps($ciniki);
  43. if( $rc['stat'] != 'ok' ) {
  44. return $rc;
  45. }
  46. $maps = $rc['maps'];
  47. //
  48. // Get the tenant settings
  49. //
  50. ciniki_core_loadMethod($ciniki, 'ciniki', 'tenants', 'private', 'intlSettings');
  51. $rc = ciniki_tenants_intlSettings($ciniki, $args['tnid']);
  52. if( $rc['stat'] != 'ok' ) {
  53. return $rc;
  54. }
  55. $intl_timezone = $rc['settings']['intl-default-timezone'];
  56. // $intl_currency_fmt = numfmt_create($rc['settings']['intl-default-locale'], NumberFormatter::CURRENCY);
  57. // $intl_currency = $rc['settings']['intl-default-currency'];
  58. ciniki_core_loadMethod($ciniki, 'ciniki', 'users', 'private', 'dateFormat');
  59. $date_format = ciniki_users_dateFormat($ciniki, 'php');
  60. $mysql_date_format = ciniki_users_dateFormat($ciniki, 'mysql');
  61. //
  62. // Load the list of members for a tenant
  63. //
  64. $strsql = "SELECT ciniki_customers.id, "
  65. . "ciniki_customers.first, "
  66. . "ciniki_customers.last, "
  67. . "ciniki_customers.display_name, "
  68. . "ciniki_customers.member_status AS member_status_text, "
  69. . "ciniki_customers.member_lastpaid, "
  70. . "ciniki_customers.member_expires, "
  71. . "DATEDIFF(NOW(), ciniki_customers.member_lastpaid) AS member_lastpaid_age, "
  72. . "DATEDIFF(NOW(), ciniki_customers.member_expires) AS member_expires_age, "
  73. . "ciniki_customers.membership_length AS membership_length_text, "
  74. . "ciniki_customers.membership_type, "
  75. . "ciniki_customers.membership_type AS membership_type_text, "
  76. . "ciniki_customers.company ";
  77. if( isset($args['category']) && $args['category'] != '' ) {
  78. $strsql .= "FROM ciniki_customer_tags "
  79. . "LEFT JOIN ciniki_customers ON ("
  80. . "ciniki_customer_tags.customer_id = ciniki_customers.id "
  81. . "AND ciniki_customers.tnid = '" . ciniki_core_dbQuote($ciniki, $args['tnid']) . "' "
  82. . "AND ciniki_customers.member_status = 10 "
  83. . ") ";
  84. if( ($ciniki['tenant']['modules']['ciniki.customers']['flags']&0x02000000) > 0 ) {
  85. $strsql .= "LEFT JOIN ciniki_customer_season_members ON ("
  86. . "ciniki_customers.id = ciniki_customer_season_members.customer_id "
  87. . "AND ciniki_customer_season_members.tnid = '" . ciniki_core_dbQuote($ciniki, $args['tnid']) . "' "
  88. . ") ";
  89. }
  90. $strsql .= "WHERE ciniki_customer_tags.tnid = '" . ciniki_core_dbQuote($ciniki, $args['tnid']) . "' "
  91. . "AND ciniki_customer_tags.permalink = '" . ciniki_core_dbQuote($ciniki, $args['category']) . "' "
  92. . "AND ciniki_customer_tags.tag_type = '40' "
  93. . "ORDER BY sort_name, last, first, company"
  94. . "";
  95. } elseif( isset($args['category']) && $args['category'] == '' ) {
  96. // $strsql = "SELECT ciniki_customers.id, "
  97. // . "ciniki_customers.first, "
  98. // . "ciniki_customers.last, "
  99. // . "ciniki_customers.display_name, "
  100. // . "ciniki_customers.member_status AS member_status_text, "
  101. // . "ciniki_customers.member_lastpaid, "
  102. // . "DATEDIFF(NOW(), ciniki_customers.member_lastpaid) AS member_lastpaid_age, "
  103. // . "ciniki_customers.membership_length AS membership_length_text, "
  104. // . "ciniki_customers.membership_type, "
  105. // . "ciniki_customers.membership_type AS membership_type_text, "
  106. // . "ciniki_customers.company "
  107. $strsql .= "FROM ciniki_customers "
  108. . "LEFT JOIN ciniki_customer_tags ON ("
  109. . "ciniki_customers.id = ciniki_customer_tags.customer_id "
  110. . "AND ciniki_customer_tags.tag_type = '40' "
  111. . "AND ciniki_customer_tags.tnid = '" . ciniki_core_dbQuote($ciniki, $args['tnid']) . "' "
  112. . ") ";
  113. if( ($ciniki['tenant']['modules']['ciniki.customers']['flags']&0x02000000) > 0 ) {
  114. $strsql .= "LEFT JOIN ciniki_customer_season_members ON ("
  115. . "ciniki_customers.id = ciniki_customer_season_members.customer_id "
  116. . "AND ciniki_customer_season_members.tnid = '" . ciniki_core_dbQuote($ciniki, $args['tnid']) . "' "
  117. . ") ";
  118. }
  119. $strsql .="WHERE ciniki_customers.tnid = '" . ciniki_core_dbQuote($ciniki, $args['tnid']) . "' "
  120. . "AND ciniki_customers.member_status = 10 "
  121. . "AND ISNULL(ciniki_customer_tags.tag_name) "
  122. . "ORDER BY sort_name, last, first, company"
  123. . "";
  124. } else {
  125. // $strsql = "SELECT ciniki_customers.id, "
  126. // . "ciniki_customers.first, "
  127. // . "ciniki_customers.last, "
  128. // . "ciniki_customers.display_name, "
  129. // . "ciniki_customers.member_status AS member_status_text, "
  130. // . "ciniki_customers.member_lastpaid, "
  131. // . "DATEDIFF(NOW(), ciniki_customers.member_lastpaid) AS member_lastpaid_age, "
  132. // . "ciniki_customers.membership_length AS membership_length_text, "
  133. // . "ciniki_customers.membership_type, "
  134. // . "ciniki_customers.membership_type AS membership_type_text, "
  135. // . "ciniki_customers.company "
  136. $strsql .= "FROM ciniki_customers ";
  137. $strsql .= "WHERE ciniki_customers.tnid = '" . ciniki_core_dbQuote($ciniki, $args['tnid']) . "' "
  138. . "AND ciniki_customers.member_status = 10 "
  139. . "ORDER BY sort_name, last, first, company"
  140. . "";
  141. }
  142. ciniki_core_loadMethod($ciniki, 'ciniki', 'core', 'private', 'dbHashQueryTree');
  143. $rc = ciniki_core_dbHashQueryTree($ciniki, $strsql, 'ciniki.customer', array(
  144. array('container'=>'members', 'fname'=>'id', 'name'=>'member',
  145. 'fields'=>array('id', 'first', 'last', 'display_name', 'company',
  146. 'member_status_text',
  147. 'member_lastpaid', 'member_lastpaid_age',
  148. 'member_expires', 'member_expires_age',
  149. 'membership_length_text',
  150. 'membership_type', 'membership_type_text'),
  151. 'maps'=>array(
  152. 'member_status_text'=>$maps['customer']['member_status'],
  153. 'membership_length_text'=>$maps['customer']['membership_length'],
  154. 'membership_type_text'=>$maps['customer']['membership_type'],
  155. ),
  156. 'utctotz'=>array(
  157. 'member_lastpaid'=>array('timezone'=>'UTC', 'format'=>$date_format),
  158. 'member_expires'=>array('timezone'=>'UTC', 'format'=>$date_format),
  159. ),
  160. ),
  161. ));
  162. if( $rc['stat'] != 'ok' ) {
  163. return $rc;
  164. }
  165. $rsp = array('stat'=>'ok', 'members'=>array());
  166. if( isset($rc['members']) ) {
  167. $rsp['members'] = $rc['members'];
  168. //
  169. // Get the seasons if enabled for the last_paid date
  170. //
  171. if( ($ciniki['tenant']['modules']['ciniki.customers']['flags']&0x02000000) > 0 ) {
  172. $strsql = "SELECT ciniki_customer_season_members.customer_id, "
  173. . "ciniki_customer_seasons.name, "
  174. . "ciniki_customer_season_members.status AS status_text, "
  175. . "DATE_FORMAT(ciniki_customer_season_members.date_paid, '" . ciniki_core_dbQuote($ciniki, $mysql_date_format) . "') AS date_paid "
  176. . "FROM ciniki_customer_season_members, ciniki_customer_seasons "
  177. . "WHERE ciniki_customer_season_members.tnid = '" . ciniki_core_dbQuote($ciniki, $args['tnid']) . "' "
  178. . "AND ciniki_customer_season_members.status > 0 "
  179. . "AND ciniki_customer_season_members.season_id = ciniki_customer_seasons.id "
  180. . "AND ciniki_customer_seasons.tnid = '" . ciniki_core_dbQuote($ciniki, $args['tnid']) . "' "
  181. . "ORDER BY ciniki_customer_season_members.customer_id, ciniki_customer_seasons.start_date DESC "
  182. . "";
  183. ciniki_core_loadMethod($ciniki, 'ciniki', 'core', 'private', 'dbHashQueryIDTree');
  184. $rc = ciniki_core_dbHashQueryIDTree($ciniki, $strsql, 'ciniki.customers', array(
  185. array('container'=>'customers', 'fname'=>'customer_id',
  186. 'fields'=>array('customer_id', 'name', 'status_text', 'date_paid'),
  187. 'maps'=>array('status_text'=>$maps['season_member']['status'])),
  188. ));
  189. if( $rc['stat'] != 'ok' ) {
  190. return $rc;
  191. }
  192. if( isset($rc['customers']) ) {
  193. $customers = $rc['customers'];
  194. foreach($rsp['members'] as $mid => $member) {
  195. if( isset($customers[$member['member']['id']]) ) {
  196. $rsp['members'][$mid]['member']['season_name'] = $customers[$member['member']['id']]['name'];
  197. $rsp['members'][$mid]['member']['season_status_text'] = $customers[$member['member']['id']]['status_text'];
  198. $rsp['members'][$mid]['member']['season_date_paid'] = $customers[$member['member']['id']]['date_paid'];
  199. }
  200. }
  201. }
  202. }
  203. }
  204. return $rsp;
  205. }
  206. ?>