DonationFormsRequestController.php
113 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonationForms\Controllers; |
| 4 | |
| 5 | use Give\DonationForms\ValueObjects\DonationFormMetaKeys; |
| 6 | use Give\Framework\Database\DB; |
| 7 | use WP_REST_Request; |
| 8 | |
| 9 | /** |
| 10 | * Donation Forms Request Controller class |
| 11 | * |
| 12 | * @since 2.21.0 |
| 13 | */ |
| 14 | class DonationFormsRequestController |
| 15 | { |
| 16 | /** |
| 17 | * @var WP_REST_Request |
| 18 | */ |
| 19 | private $request; |
| 20 | |
| 21 | /** |
| 22 | * @param WP_REST_Request $request |
| 23 | */ |
| 24 | public function __construct(WP_REST_Request $request) |
| 25 | { |
| 26 | $this->request = $request; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @return array |
| 31 | */ |
| 32 | public function getForms(): array |
| 33 | { |
| 34 | $page = $this->request->get_param('page'); |
| 35 | $perPage = $this->request->get_param('perPage'); |
| 36 | $search = $this->request->get_param('search'); |
| 37 | $status = $this->request->get_param('status'); |
| 38 | |
| 39 | $query = DB::table('posts') |
| 40 | ->select( |
| 41 | 'id', |
| 42 | ['post_date', 'createdAt'], |
| 43 | ['post_date_gmt', 'createdAtGmt'], |
| 44 | ['post_status', 'status'], |
| 45 | ['post_title', 'title'] |
| 46 | ) |
| 47 | ->attachMeta('give_formmeta', 'id', 'form_id', |
| 48 | [DonationFormMetaKeys::FORM_EARNINGS, 'revenue'], |
| 49 | [DonationFormMetaKeys::DONATION_LEVELS, 'donationLevels'], |
| 50 | [DonationFormMetaKeys::SET_PRICE, 'setPrice'], |
| 51 | [DonationFormMetaKeys::GOAL_OPTION, 'goalEnabled'] |
| 52 | ) |
| 53 | ->where('post_type', 'give_forms') |
| 54 | ->limit($perPage) |
| 55 | ->orderBy('id', 'DESC') |
| 56 | ->offset(($page - 1) * $perPage); |
| 57 | |
| 58 | // Status |
| 59 | if ($status === 'any') { |
| 60 | $query->whereIn('post_status', ['publish', 'draft', 'pending', 'private']); |
| 61 | } else { |
| 62 | $query->where('post_status', $status); |
| 63 | } |
| 64 | |
| 65 | // Search |
| 66 | if ($search) { |
| 67 | if (ctype_digit($search)) { |
| 68 | $query->where('ID', $search); |
| 69 | } else { |
| 70 | $searchTerms = array_map('trim', explode(' ', $search)); |
| 71 | foreach ($searchTerms as $term) { |
| 72 | if ($term) { |
| 73 | $query->whereLike('post_title', $term); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return $query->getAll(); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @return int |
| 84 | */ |
| 85 | public function getTotalFormsCount(): int |
| 86 | { |
| 87 | $search = $this->request->get_param('search'); |
| 88 | $status = $this->request->get_param('status'); |
| 89 | $perPage = $this->request->get_param('perPage'); |
| 90 | |
| 91 | $query = DB::table('posts') |
| 92 | ->where('post_type', 'give_forms'); |
| 93 | |
| 94 | if ($status === 'any') { |
| 95 | $query->whereIn('post_status', ['publish', 'draft', 'pending']); |
| 96 | } else { |
| 97 | $query->where('post_status', $status); |
| 98 | } |
| 99 | |
| 100 | if ($search) { |
| 101 | if (ctype_digit($search)) { |
| 102 | $query->where('ID', $search); |
| 103 | } else { |
| 104 | $query->whereLike('post_title', $search); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | $query->limit($perPage); |
| 109 | |
| 110 | return $query->count(); |
| 111 | } |
| 112 | } |
| 113 |