DonationFormsRepository.php
124 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonationForms\Repositories; |
| 4 | |
| 5 | use Give\Framework\Database\DB; |
| 6 | use WP_REST_Request; |
| 7 | |
| 8 | /** |
| 9 | * @since 2.19.0 |
| 10 | */ |
| 11 | class DonationFormsRepository |
| 12 | { |
| 13 | /** |
| 14 | * @param WP_REST_Request $request |
| 15 | * |
| 16 | * @return array |
| 17 | */ |
| 18 | public function getFormsForRequest(WP_REST_Request $request) |
| 19 | { |
| 20 | $page = $request->get_param('page'); |
| 21 | $perPage = $request->get_param('perPage'); |
| 22 | $search = $request->get_param('search'); |
| 23 | $status = $request->get_param('status'); |
| 24 | |
| 25 | $query = DB::table('posts') |
| 26 | ->select( |
| 27 | ['ID', 'id'], |
| 28 | ['post_date', 'createdAt'], |
| 29 | ['post_date_gmt', 'createdAtGmt'], |
| 30 | ['post_status', 'status'], |
| 31 | ['post_title', 'title'] |
| 32 | ) |
| 33 | ->attachMeta('give_formmeta', 'id', 'form_id', |
| 34 | ['_give_form_earnings', 'revenue'], |
| 35 | ['_give_donation_levels', 'donationLevels'], |
| 36 | ['_give_set_price', 'setPrice'], |
| 37 | ['_give_goal_option', 'goalEnabled'] |
| 38 | ) |
| 39 | ->where('post_type', 'give_forms') |
| 40 | ->limit($perPage) |
| 41 | ->orderBy('id', 'DESC') |
| 42 | ->offset(($page - 1) * $perPage); |
| 43 | |
| 44 | // Status |
| 45 | if ($status === 'any') { |
| 46 | $query->whereIn('post_status', ['publish', 'draft', 'pending']); |
| 47 | } else { |
| 48 | $query->where('post_status', $status); |
| 49 | } |
| 50 | |
| 51 | // Search |
| 52 | if ($search) { |
| 53 | if (ctype_digit($search)) { |
| 54 | $query->where('ID', $search); |
| 55 | } else { |
| 56 | $searchTerms = array_map('trim', explode(' ', $search)); |
| 57 | foreach ($searchTerms as $term) |
| 58 | { |
| 59 | if ($term) |
| 60 | { |
| 61 | $query->whereLike('post_title', $term); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return $query->getAll(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param WP_REST_Request $request |
| 72 | * |
| 73 | * @return int |
| 74 | */ |
| 75 | public function getTotalFormsCountForRequest(WP_REST_Request $request) |
| 76 | { |
| 77 | $search = $request->get_param('search'); |
| 78 | $status = $request->get_param('status'); |
| 79 | $perPage = $request->get_param('perPage'); |
| 80 | |
| 81 | $query = DB::table('posts') |
| 82 | ->selectRaw('SELECT COUNT(ID) AS count') |
| 83 | ->where('post_type', 'give_forms'); |
| 84 | |
| 85 | if ($status === 'any') { |
| 86 | $query->whereIn('post_status', ['publish', 'draft', 'pending']); |
| 87 | } else { |
| 88 | $query->where('post_status', $status); |
| 89 | } |
| 90 | |
| 91 | if ($search) { |
| 92 | if (ctype_digit($search)) { |
| 93 | $query->where('ID', $search); |
| 94 | } else { |
| 95 | $query->whereLike('post_title', $search); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | $query->limit($perPage); |
| 100 | |
| 101 | $total = $query->get(); |
| 102 | |
| 103 | return $total->count; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @param int $formId |
| 108 | * |
| 109 | * @return int |
| 110 | */ |
| 111 | public function getFormDonationsCount($formId) |
| 112 | { |
| 113 | $donations = DB::table('posts') |
| 114 | ->selectRaw('SELECT COUNT(ID) as count') |
| 115 | ->leftJoin('give_donationmeta', 'ID', 'donation_id') |
| 116 | ->where('meta_key', '_give_payment_form_id') |
| 117 | ->where('meta_value', $formId) |
| 118 | ->get(); |
| 119 | |
| 120 | |
| 121 | return $donations->count; |
| 122 | } |
| 123 | } |
| 124 |