CartRepository.php
7 months ago
ContactListRepository.php
8 months ago
FormRepository.php
8 months ago
Repository.php
8 months ago
RepositoryInterface.php
8 months ago
FormRepository.php
142 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Repositories; |
| 4 | |
| 5 | use Exception; |
| 6 | use Hostinger\Reach\Admin\Database\FormsTable; |
| 7 | use Hostinger\Reach\Models\Form; |
| 8 | use wpdb; |
| 9 | |
| 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | die; |
| 12 | } |
| 13 | |
| 14 | class FormRepository extends Repository { |
| 15 | public function __construct( wpdb $db, FormsTable $table ) { |
| 16 | parent::__construct( $db ); |
| 17 | $this->table = $table; |
| 18 | } |
| 19 | |
| 20 | public function all( array $where = array() ): array { |
| 21 | $limit = apply_filters( 'hostinger_reach_forms_limit', 100 ); |
| 22 | $query = $this->build_query( $where, $limit ); |
| 23 | $results = $this->db->get_results( $query, ARRAY_A ); |
| 24 | |
| 25 | $forms = array(); |
| 26 | foreach ( $results as $result ) { |
| 27 | $forms[] = ( new Form( $result ) )->to_array(); |
| 28 | } |
| 29 | |
| 30 | return $forms; |
| 31 | } |
| 32 | |
| 33 | public function exists( string $id ): bool { |
| 34 | try { |
| 35 | $this->get( $id ); |
| 36 | |
| 37 | return true; |
| 38 | } catch ( Exception $e ) { |
| 39 | return false; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | public function insert( array $fields ): bool { |
| 44 | if ( $this->exists( $fields['form_id'] ) ) { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | return $this->db->insert( $this->table->table_name(), $fields ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @throws Exception |
| 53 | */ |
| 54 | public function get( string $id ): array { |
| 55 | $query = $this->db->prepare( 'SELECT * FROM %i WHERE form_id = %s', $this->table->table_name(), $id ); |
| 56 | $results = $this->db->get_results( $query, ARRAY_A ); |
| 57 | if ( ! empty( $results ) ) { |
| 58 | return ( new Form( $results[0] ) )->to_array(); |
| 59 | } |
| 60 | |
| 61 | throw new Exception( 'Form not found' ); |
| 62 | } |
| 63 | |
| 64 | public function update( array $fields ): bool { |
| 65 | if ( ! isset( $fields['form_id'] ) ) { |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | $data = array_diff_key( $fields, array_flip( array( 'form_id' ) ) ); |
| 70 | if ( empty( $data ) ) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | return $this->db->update( |
| 75 | $this->table->table_name(), |
| 76 | $data, |
| 77 | array( 'form_id' => $fields['form_id'] ) |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | public function delete( string $form_id ): bool { |
| 82 | if ( empty( $form_id ) ) { |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | return (bool) $this->db->delete( |
| 87 | $this->table->table_name(), |
| 88 | array( 'form_id' => $form_id ) |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | public function delete_all_from_post( int $post_id, string $type ): bool { |
| 93 | if ( empty( $type ) ) { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | return (bool) $this->db->delete( |
| 98 | $this->table->table_name(), |
| 99 | array( |
| 100 | 'post_id' => $post_id, |
| 101 | 'type' => $type, |
| 102 | ) |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @throws Exception |
| 108 | */ |
| 109 | public function submit( array $data ): bool { |
| 110 | if ( empty( $data['form_id'] ) ) { |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | try { |
| 115 | $form_id = $data['form_id']; |
| 116 | $form = $this->get( $form_id ); |
| 117 | |
| 118 | return $this->update( |
| 119 | array( |
| 120 | 'form_id' => $form_id, |
| 121 | 'submissions' => $form['submissions'] + 1, |
| 122 | ) |
| 123 | ); |
| 124 | } catch ( Exception $e ) { |
| 125 | return false; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | public function is_form_active( string $form_id ): bool { |
| 130 | if ( empty( $form_id ) ) { |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | try { |
| 135 | $form = $this->get( $form_id ); |
| 136 | return $form['is_active']; |
| 137 | } catch ( Exception $e ) { |
| 138 | return false; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 |