Caldera_Forms_Entry_Entries

A collection of entries for one form.

Defined (1)

The class is defined in the following location(s).

/classes/entry/entries.php  
  1. class Caldera_Forms_Entry_Entries { 
  2.  
  3. /** 
  4. * Form config 
  5. * @since 1.4.0 
  6. * @var array 
  7. */ 
  8. protected $form; 
  9.  
  10. /** 
  11. * Holds all entries, index by status and then page 
  12. * @since 1.4.0 
  13. * @var array 
  14. */ 
  15. protected $entries = array(); 
  16.  
  17. /** 
  18. * Holds totals by page 
  19. * @since 1.4.0 
  20. * @var array 
  21. */ 
  22. protected $totals = array(); 
  23.  
  24. /** 
  25. * Allowed statuses 
  26. * @todo need to make sure this matches in other places, use filter or something 
  27. * @since 1.4.0 
  28. * @var array 
  29. */ 
  30. protected $statuses = array( 
  31. 'active',  
  32. 'pending',  
  33. 'trash' 
  34. ); 
  35.  
  36. /** 
  37. * Total per page 
  38. * @since 1.4.0 
  39. * @var int 
  40. */ 
  41. protected $perpage; 
  42.  
  43. /** 
  44. * Caldera_Forms_Entry_Entries constructor. 
  45. * @since 1.4.0 
  46. * @param array $form Form config 
  47. * @param int $perpage Number of entries per pags 
  48. */ 
  49. public function __construct( array $form, $perpage ) { 
  50. $this->form = $form; 
  51. $this->perpage = $perpage; 
  52. $this->prepare_entries_prop(); 
  53.  
  54.  
  55. /** 
  56. * Get one page of results 
  57. * @since 1.4.0 
  58. * @param int $page 
  59. * @param string $status 
  60. * @return array 
  61. */ 
  62. public function get_page( $page = 1, $status = 'active' ) { 
  63. $page = (int) $page; 
  64. if ( ! in_array( $status, $this->statuses ) ) { 
  65. return array(); 
  66.  
  67. if ( empty( $this->entries[ $status ][ $page ] ) ) { 
  68. $this->query_page( $page, $status ); 
  69.  
  70. return $this->entries[ $status ][ $page ]; 
  71.  
  72. /** 
  73. * Get an entry row merging entry and field value 
  74. * This is for use inside of Caldera_Forms_Admin::get_entries() for backwards compat reasons 
  75. * @since 1.4.0 
  76. * @param int $page What page of results to get 
  77. * @param int $entry_id ID of entry 
  78. * @param string $status Optional. Status, default is 'active' 
  79. * @return array 
  80. */ 
  81. public function get_rows( $page, $entry_id, $status = 'active' ) { 
  82. $page = (int) $page; 
  83. if ( ! in_array( $status, $this->statuses ) ) { 
  84. return array(); 
  85.  
  86. if ( ! isset( $this->entries[ $status ][ $page ] ) || empty( $this->entries[ $status ][ $page ] ) ) { 
  87. $this->query_page( $page, $status ); 
  88.  
  89. if ( ! isset( $this->entries[ $status ][ $page ] ) || ! isset( $this->entries[ $status ][ $page ][ $entry_id ] ) ) { 
  90. return array(); 
  91.  
  92. $data = $_entry = array(); 
  93. /** @var Caldera_Forms_Entry $entry */ 
  94. $entry = $this->entries[ $status ][ $page ][ $entry_id ]; 
  95.  
  96. foreach ( $entry->get_entry()->to_array() as $key => $value ) { 
  97. $_entry[ '_' . $key ] = $value; 
  98.  
  99. $field_values = $entry->get_fields(); 
  100. if ( ! empty( $field_values ) ) { 
  101. /** @var Caldera_Forms_Entry_Field $_field */ 
  102. foreach ( $field_values as $_field ) { 
  103. $field = array_merge( $_field->to_array(), $_entry ); 
  104. $data[] = (object) $field; 
  105.  
  106. return $data; 
  107.  
  108.  
  109. /** 
  110. * Get total number of entries with a given status 
  111. * @since 1.4.0 
  112. * @param string $status Which status 
  113. * @return int 
  114. */ 
  115. public function get_total( $status ) { 
  116. if ( ! in_array( $status, $this->statuses ) ) { 
  117. return 0; 
  118.  
  119. return $this->totals[ $status ]; 
  120.  
  121. /** 
  122. * Query for a page of results 
  123. * @since 1.4.0 
  124. * @param int $page What page 
  125. * @param string $status Which status 
  126. */ 
  127. protected function query_page( $page, $status ) { 
  128. global $wpdb; 
  129. $table = $wpdb->prefix . "cf_form_entries"; 
  130. $offset = ( $page - 1 ) * $this->perpage; 
  131. $limit = $offset . ', ' . $this->perpage; 
  132. $sql = $wpdb->prepare( "SELECT * FROM $table WHERE `form_id` = %s AND `status` = %s ORDER BY `datestamp` DESC LIMIT " . $limit . ";", $this->form[ 'ID' ], $status ); 
  133. $_entries = $wpdb->get_results( $sql ); 
  134. if ( ! empty( $_entries ) ) { 
  135. $this->entries[ $status ][ $page ] = array(); 
  136. foreach ( $_entries as $_entry ) { 
  137. $entry = new Caldera_Forms_Entry_Entry( $_entry ); 
  138. $this->entries[ $status ][ $page ][ (int) $_entry->id ] = new Caldera_Forms_Entry( $this->form, $entry->id, $entry ); 
  139.  
  140.  
  141. /** 
  142. * Query for total rows 
  143. * @since 1.4.0 
  144. * @param string $status Which status 
  145. * @return int 
  146. */ 
  147. protected function query_total( $status ) { 
  148. return Caldera_Forms_Entry_Bulk::count( $this->form[ 'ID' ], $status ); 
  149.  
  150. /** 
  151. * Populates the entries property of this class with arrays per status and per page as needed. 
  152. * @since 1.4.0 
  153. */ 
  154. protected function prepare_entries_prop() { 
  155. foreach ( $this->statuses as $status ) { 
  156. $total = $this->query_total( $status ); 
  157. $this->totals[ $status ] = $total; 
  158. $pages = ceil( $total / $this->perpage ); 
  159. $this->entries[ $status ] = array(); 
  160. if ( 1 >= $pages ) { 
  161. $this->entries[ $status ] = array( 1 => array() ); 
  162. } else { 
  163. $this->entries[ $status ] = array_fill( 1, $pages, array() ); 
  164.  
  165.  
  166.