Caldera_Forms_Entry

CRUD for Caldera Forms entries.

Defined (1)

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

/classes/entry.php  
  1. class Caldera_Forms_Entry { 
  2.  
  3. /** 
  4. * Form config 
  5. * @since 1.4.0 
  6. * @var array 
  7. */ 
  8. protected $form; 
  9.  
  10. /** 
  11. * Entry ID 
  12. * @since 1.4.0 
  13. * @var array 
  14. */ 
  15. protected $entry_id; 
  16.  
  17.  
  18. /** 
  19. * Field values for entry 
  20. * @since 1.4.0 
  21. * @var array 
  22. */ 
  23. protected $fields; 
  24.  
  25. /** 
  26. * Meta values for entry 
  27. * @since 1.4.0 
  28. * @var array 
  29. */ 
  30. protected $meta; 
  31.  
  32. /** 
  33. * Entry object 
  34. * @since 1.4.0 
  35. * @var Caldera_Forms_Entry_Entry 
  36. */ 
  37. protected $entry; 
  38.  
  39. /** 
  40. * Was an entry queried for and found? 
  41. * @since 1.4.0 
  42. * @var bool 
  43. */ 
  44. protected $found; 
  45.  
  46. /** 
  47. * Holds the map of field_id to index of $this->fields. Lazy loaded by $this->get_field_map() 
  48. * @since 1.5.0.7 
  49. * @var array 
  50. */ 
  51. private $field_map; 
  52.  
  53.  
  54. /** 
  55. * Caldera_Forms_Entry constructor. 
  56. * @since 1.4.0 
  57. * @param array $form For config 
  58. * @param int|bool $entry_id Optional. Passing ID will load saved entry, leave false, the default when creating new entry. 
  59. * @param Caldera_Forms_Entry_Entry $entry Optional. Entry object. 
  60. */ 
  61. public function __construct( array $form, $entry_id = false, Caldera_Forms_Entry_Entry $entry = null ) { 
  62. $this->form = $form; 
  63. if( null !== $entry ) { 
  64. if( null != $entry->id ) { 
  65. $this->entry_id = $entry->id; 
  66.  
  67. $this->entry = $entry; 
  68. }elseif( is_numeric( $entry_id ) ) { 
  69. $this->entry_id = $entry_id; 
  70.  
  71. if( is_numeric( $this->entry_id ) ) { 
  72. $this->query(); 
  73.  
  74.  
  75. /** 
  76. * Get entry object 
  77. * @since 1.4.0 
  78. * @param \Caldera_Forms_Entry_Entry $entry 
  79. */ 
  80. public function get_entry_object( Caldera_Forms_Entry_Entry $entry ) { 
  81. $this->entry = $entry; 
  82.  
  83. /** 
  84. * Get form ID 
  85. * @since 1.4.0 
  86. * @return mixed 
  87. */ 
  88. public function get_form_id() { 
  89. return $this->form[ 'id' ]; 
  90.  
  91. /** 
  92. * Get entry object 
  93. * Has basic info, no fields or values 
  94. * @since 1.4.0 
  95. * @return \Caldera_Forms_Entry_Entry 
  96. */ 
  97. public function get_entry() { 
  98. return $this->entry; 
  99.  
  100. /** 
  101. * Was a query performed and an entry found? 
  102. * @since 1.4.0 
  103. * @return bool 
  104. */ 
  105. public function found() { 
  106. return $this->found; 
  107.  
  108. /** 
  109. * Get entry ID 
  110. * @since 1.4.0 
  111. * @return int 
  112. */ 
  113. public function get_entry_id() { 
  114. if( null == $this->entry_id && is_object( $this->entry ) ) { 
  115. $this->entry_id = $this->entry->id; 
  116. return $this->entry_id; 
  117.  
  118. /** 
  119. * Get all field values 
  120. * @since 1.4.0 
  121. * @return array 
  122. */ 
  123. public function get_fields() { 
  124. if ( ! empty( $this->fields ) ) { 
  125. /** @var Caldera_Forms_Entry_Field $field */ 
  126. foreach ( $this->fields as $index => $field ) { 
  127. $field->value = $field->apply_filter( 'value', $field->value ); 
  128. $this->fields[ $index ] = $field; 
  129.  
  130.  
  131. return $this->fields; 
  132.  
  133.  
  134.  
  135. /** 
  136. * Get a specific field 
  137. * @since 1.4.0 
  138. * @param string $id Field ID 
  139. * @return Caldera_Forms_Entry_Field|null 
  140. */ 
  141. public function get_field( $id ) { 
  142. return $this->find_field_by_id( $id ); 
  143.  
  144. /** 
  145. * Get all meta values 
  146. * @since 1.4.0 
  147. * @return array 
  148. */ 
  149. public function get_meta() { 
  150. return $this->meta; 
  151.  
  152.  
  153. /** 
  154. * Query DB for saved entry data 
  155. * @since 1.4.0 
  156. */ 
  157. public function query() { 
  158. if ( ! is_object( $this->entry ) ) { 
  159. $this->find_entry(); 
  160.  
  161. if( is_numeric( $this->entry_id ) ) { 
  162. $this->find_fields(); 
  163. $this->find_metas(); 
  164.  
  165. /** 
  166. * Find the entry in DB and set the entry property of this class 
  167. * @since 1.4.0 
  168. */ 
  169. protected function find_entry() { 
  170. global $wpdb; 
  171. $table = $wpdb->prefix . 'cf_form_entries'; 
  172. $sql = $wpdb->prepare( "SELECT * FROM $table WHERE `id` = %d AND `form_id` = %s", $this->entry_id, $this->form[ 'ID' ] ); 
  173. $results = $wpdb->get_results( $sql ); 
  174. if ( ! empty( $results ) ) { 
  175. $this->found = true; 
  176. $this->entry = new Caldera_Forms_Entry_Entry(); 
  177. $this->entry->set_form_object( $results[ 0 ] ); 
  178. }else{ 
  179. $this->found = false; 
  180.  
  181.  
  182. /** 
  183. * Find field values and set in fields property of this class 
  184. * @since 1.4.0 
  185. */ 
  186. protected function find_fields() { 
  187. global $wpdb; 
  188. $table = $wpdb->prefix . 'cf_form_entry_values'; 
  189. $sql = $wpdb->prepare( "SELECT * FROM $table WHERE `entry_id` = %d", $this->entry_id ); 
  190. $results = $wpdb->get_results( $sql ); 
  191. if( ! empty( $results ) ) { 
  192. foreach( $results as $result ) { 
  193. $_field = new Caldera_Forms_Entry_Field( $result ); 
  194. $this->fields[] = $_field; 
  195.  
  196. /** 
  197. * Find metas and set in meta property of this class 
  198. * @since 1.4.0 
  199. */ 
  200. protected function find_metas() { 
  201. global $wpdb; 
  202. $table = $wpdb->prefix . 'cf_form_entry_meta'; 
  203. $sql = $wpdb->prepare( "SELECT * FROM $table WHERE `entry_id` = %d", $this->entry_id ); 
  204. $results = $wpdb->get_results( $sql ); 
  205. if( ! empty( $results ) ) { 
  206. foreach ( $results as $result ) { 
  207. $_meta = new Caldera_Forms_Entry_Meta(); 
  208. $_meta = $_meta->set_form_object( $result ); 
  209. $this->meta[] = $_meta; 
  210.  
  211. /** 
  212. * Save entry 
  213. * @since 1.4.0 
  214. * @return array|int|string 
  215. */ 
  216. public function save( ) { 
  217. $this->save_entry(); 
  218. if( is_numeric( $this->entry_id ) ) { 
  219. if ( ! empty( $this->fields ) ) { 
  220. foreach ( $this->fields as $i => $field ) { 
  221. if ( $field instanceof Caldera_Forms_Entry_Field ) { 
  222. $this->fields[ $i ] = $this->save_field( $field ); 
  223.  
  224.  
  225.  
  226. if ( ! empty( $this->meta ) ) { 
  227. foreach ( $this->meta as $i => $meta ) { 
  228. if ( $meta instanceof Caldera_Forms_Entry_Meta ) { 
  229. $this->meta[ $i ] = $this->save_meta( $meta ); 
  230.  
  231.  
  232. return $this->entry_id; 
  233.  
  234. /** 
  235. * Update entry status 
  236. * @since 1.4.0 
  237. * @param string $status 
  238. * @return bool 
  239. */ 
  240. public function update_status( $status ) { 
  241. if( ! $this->allowed_status( $status ) ) { 
  242. return false; 
  243.  
  244. global $wpdb; 
  245. $wpdb->update( $wpdb->prefix . 'cf_form_entries', array('status' => $status ), array( 'id' => $this->entry_id ) ); 
  246.  
  247. /** 
  248. * Add a field value to this entry 
  249. * @since 1.4.0 
  250. * @param \Caldera_Forms_Entry_Field $field 
  251. */ 
  252. public function add_field( Caldera_Forms_Entry_Field $field ) { 
  253.  
  254. $field->entry_id = $this->entry_id; 
  255. $key = $this->find_field_index( $field->field_id ); 
  256. if( ! is_null( $key ) ) { 
  257. $this->fields[ $key ] = $field; 
  258. }else{ 
  259. $this->fields[] = $field; 
  260.  
  261.  
  262.  
  263. /** 
  264. * Add a meta to this entry 
  265. * @since 1.4.0 
  266. * @param \Caldera_Forms_Entry_Meta $item 
  267. */ 
  268. public function add_meta( Caldera_Forms_Entry_Meta $item ) { 
  269. $this->meta[] = $item; 
  270.  
  271. /** 
  272. * Save entry in DB 
  273. * @since 1.4.0 
  274. */ 
  275. protected function save_entry() { 
  276. global $wpdb; 
  277. if( null == $this->entry ) { 
  278. //@todo some error or exception or something 
  279. return; 
  280. if ( ! $this->entry_id ) { 
  281. $wpdb->insert( $wpdb->prefix . 'cf_form_entries', $this->entry->to_array() ); 
  282. $this->entry_id = $this->entry->id = $wpdb->insert_id; 
  283. } else { 
  284. $wpdb->update( $wpdb->prefix . 'cf_form_entries', $this->entry->to_array(), array( 
  285. 'id' => $this->entry_id 
  286. )); 
  287.  
  288. /** 
  289. * Save a field in db 
  290. * @since 1.4.0 
  291. * @param \Caldera_Forms_Entry_Field $field 
  292. * @return Caldera_Forms_Entry_Field 
  293. */ 
  294. protected function save_field( Caldera_Forms_Entry_Field $field ) { 
  295. $field->entry_id = $this->entry_id; 
  296. global $wpdb; 
  297. $data = $field->to_array(); 
  298. if ( ! isset( $data[ 'id' ] ) ) { 
  299. $wpdb->insert( $wpdb->prefix . 'cf_form_entry_values', $data ); 
  300. $field->id = $wpdb->insert_id; 
  301. }else{ 
  302. Caldera_Forms_Entry_Update::update_field( $field ); 
  303.  
  304.  
  305. return $field; 
  306.  
  307. /** 
  308. * Save a meta in DB 
  309. * @since 1.4.0 
  310. * @param \Caldera_Forms_Entry_Meta $meta 
  311. * @return Caldera_Forms_Entry_Meta 
  312. */ 
  313. protected function save_meta( Caldera_Forms_Entry_Meta $meta ) { 
  314. $meta->entry_id = $this->entry_id; 
  315. global $wpdb; 
  316. $data = $meta->to_array(); 
  317. unset( $data[ 'id' ] ); 
  318. $wpdb->insert( $wpdb->prefix . 'cf_form_entry_meta', $data ); 
  319. $meta->meta_id = $wpdb->insert_id; 
  320. return $meta; 
  321.  
  322. /** 
  323. * Check if is an allowed status 
  324. * @since 1.4.0 
  325. * @param string $status Status 
  326. * @return bool 
  327. */ 
  328. protected function allowed_status( $status ) { 
  329. if( ! in_array( $status, array( 
  330. 'active',  
  331. 'pending',  
  332. 'trash' 
  333. ) ) ) { 
  334. return false; 
  335.  
  336. return true; 
  337.  
  338. /** 
  339. * Find field object by field ID 
  340. * @since 1.5.0.7 
  341. * @param $field_id 
  342. * @return Caldera_Forms_Entry_Field|null 
  343. */ 
  344. protected function find_field_by_id( $field_id ) { 
  345. $key = $this->find_field_index( $field_id ); 
  346. if( ! is_null( $key ) ) { 
  347. return $this->fields[ $key ]; 
  348.  
  349. return null; 
  350.  
  351. /** 
  352. * Lazy-loader for "field map" that provides field_id => index of $this->field 
  353. * @since 1.5.0.7 
  354. * @return array 
  355. */ 
  356. protected function get_field_map() { 
  357. $this->get_fields(); 
  358. if( empty( $this->field_map ) && ! empty( $this->fields ) ) { 
  359. //Wouldn't it be better to use array_column() ? Yes it would, but PHP 5.2 so :( 
  360. $this->field_map = array_combine( wp_list_pluck( $this->fields, 'field_id' ), array_keys( $this->fields ) ); 
  361.  
  362. return $this->field_map; 
  363.  
  364.  
  365. private function find_field_index( $field_id ) { 
  366. $this->get_field_map(); 
  367. if( ! empty( $this->field_map ) && isset( $this->field_map[ $field_id ] ) ) { 
  368. return $this->field_map[ $field_id ]; 
  369.  
  370. return null;