NF_Abstracts_ModelFactory

Class NF_Abstracts_ModelFactory.

Defined (1)

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

/includes/Abstracts/ModelFactory.php  
  1. class NF_Abstracts_ModelFactory 
  2. /** 
  3. * Database Object 
  4. * @var 
  5. */ 
  6. protected $_db; 
  7.  
  8. /** 
  9. * The last set object. 
  10. * Used to create context between two objects in a chain. 
  11. * @var object 
  12. */ 
  13. protected $_object; 
  14.  
  15. /** 
  16. * Form 
  17. */ 
  18. protected $_form; 
  19.  
  20. /** 
  21. * Fields 
  22. * An array of field model objects. 
  23. * @var array 
  24. */ 
  25. protected $_fields = array(); 
  26.  
  27. /** 
  28. * Actions 
  29. * An array of action model objects. 
  30. * @var array 
  31. */ 
  32. protected $_actions = array(); 
  33.  
  34. /** 
  35. * Objects 
  36. * An array of generic model objects. 
  37. * @var array 
  38. */ 
  39. protected $_objects = array(); 
  40.  
  41. //----------------------------------------------------- 
  42. // Public Methods 
  43. //----------------------------------------------------- 
  44.  
  45. /** 
  46. * NF_Abstracts_ModelFactory constructor. 
  47. * @param $db 
  48. * @param $id 
  49. */ 
  50. public function __construct( $db, $id ) 
  51. $this->_db = $db; 
  52.  
  53. $this->_object = $this->_form = new NF_Database_Models_Form( $this->_db, $id ); 
  54.  
  55. $form_cache = get_option( 'nf_form_' . $id, false ); 
  56.  
  57. if( $form_cache ) { 
  58. $this->_object->update_settings( $form_cache[ 'settings' ] ); 
  59.  
  60. return $this; 
  61.  
  62. /** 
  63. * Returns the parent object set by the constructor for chained methods. 
  64. * @return object 
  65. */ 
  66. public function get() 
  67. $object = $this->_object; 
  68. $this->_object = $this->_form; 
  69. return $object; 
  70.  
  71. /** 
  72. * Get Forms 
  73. * Returns an array of Form Model Objects. 
  74. * @param array $where 
  75. * @return array|bool 
  76. */ 
  77. public function get_forms( array $where = array() ) 
  78. if( 'form' != $this->_object->get_type() ) return FALSE; 
  79.  
  80. return $this->_object->find( NULL, $where ); 
  81.  
  82. /** 
  83. * Export Form 
  84. * A wrapper for the Form Model export method. 
  85. * @param bool|FALSE $return 
  86. * @return array 
  87. */ 
  88. public function export_form( $return = FALSE ) 
  89. $form_id = $this->_object->get_id(); 
  90.  
  91. return NF_Database_Models_Form::export( $form_id, $return ); 
  92.  
  93. /** 
  94. * Import Form 
  95. * A wrapper for the Form Model import method. 
  96. * @param $import 
  97. */ 
  98. public function import_form( $import, $id = FALSE, $is_conversion = FALSE ) 
  99. if( ! is_array( $import ) ) { 
  100.  
  101. $data = WPN_Helper::utf8_decode( json_decode( html_entity_decode( $import ), true ) ); 
  102.  
  103. if( ! is_array( $data ) ) { 
  104. $data = WPN_Helper::utf8_decode( json_decode( $import , true ) ); 
  105.  
  106. if( ! is_array( $data ) ) { 
  107. $data = WPN_Helper::maybe_unserialize( $import ); 
  108.  
  109. if( ! is_array( $data ) ) { 
  110. return false; 
  111. $import = $data; 
  112.  
  113. return NF_Database_Models_Form::import( $import, $id, $is_conversion ); 
  114.  
  115. /** 
  116. * FIELDS 
  117. */ 
  118.  
  119. /** 
  120. * Sets the parent object for chained methods as a Field. 
  121. * @param string $id 
  122. * @return $this 
  123. */ 
  124. public function field( $id = '' ) 
  125. $form_id = $this->_object->get_id(); 
  126.  
  127. $this->_object = new NF_Database_Models_Field( $this->_db, $id, $form_id ); 
  128.  
  129. return $this; 
  130.  
  131. /** 
  132. * Returns a field object. 
  133. * @param $id 
  134. * @return NF_Database_Models_Field 
  135. */ 
  136. public function get_field( $id ) 
  137. if( isset( $this->_fields[ $id ] ) ) { 
  138. return $this->_fields[ $id ]; 
  139.  
  140. /** MISSING FORM ID FALLBACK */ 
  141. /** 
  142. if( ! $form_id ) { 
  143. $form_id = $wpdb->get_var( $wpdb->prepare( 
  144. "SELECT parent_id from {$wpdb->prefix}nf3_fields WHERE id = %d", $id 
  145. )); 
  146. $this->_object = $this->_form = new NF_Database_Models_Form( $this->_db, $id ); 
  147. */ 
  148.  
  149. if( ! $this->_fields ) { 
  150. $this->get_fields(); 
  151.  
  152. if( ! isset( $this->_fields[ $id ] ) ) { 
  153. $form_id = $this->_object->get_id(); 
  154. $this->_fields[ $id ] = new NF_Database_Models_Field( $this->_db, $id, $form_id ); 
  155.  
  156. return $this->_fields[ $id ]; 
  157.  
  158. /** 
  159. * Returns an array of field objects for the set form (object). 
  160. * @param array $where 
  161. * @param bool|FALSE $fresh 
  162. * @return array 
  163. */ 
  164. public function get_fields( $where = array(), $fresh = FALSE) 
  165.  
  166. $field_by_key = array(); 
  167.  
  168. $form_id = $this->_object->get_id(); 
  169.  
  170. if( $where || $fresh || ! $this->_fields ) { 
  171.  
  172. $form_cache = get_option( 'nf_form_' . $form_id, false ); 
  173.  
  174. if( ! $form_cache ) { 
  175. $model_shell = new NF_Database_Models_Field($this->_db, 0); 
  176.  
  177. $fields = $model_shell->find($form_id, $where); 
  178.  
  179. foreach ($fields as $field) { 
  180. $this->_fields[$field->get_id()] = $field; 
  181. $field_by_key[ $field->get_setting( 'key' ) ] = $field; 
  182. } else { 
  183. foreach( $form_cache[ 'fields' ] as $cached_field ) { 
  184. $field = new NF_Database_Models_Field( $this->_db, $cached_field[ 'id' ], $form_id ); 
  185. $field->update_settings( $cached_field[ 'settings' ] ); 
  186. $this->_fields[$field->get_id()] = $field; 
  187. $field_by_key[ $field->get_setting( 'key' ) ] = $field; 
  188.  
  189. /** 
  190. * If a filter is registered to modify field order, then use that filter. 
  191. * If not, then usort??. 
  192. */ 
  193. $order = apply_filters( 'ninja_forms_get_fields_sorted', array(), $this->_fields, $field_by_key, $form_id ); 
  194.  
  195. if ( ! empty( $order ) ) { 
  196. $this->_fields = $order; 
  197.  
  198. /** 
  199. * Broke the sub edit screen order when I have this enabled. 
  200. */ 
  201. // usort( $this->_fields, "NF_Abstracts_Field::sort_by_order" ); 
  202.  
  203. return $this->_fields; 
  204.  
  205. /** 
  206. * Import Field 
  207. * A wrapper for the Form Model import method. 
  208. * @param $import 
  209. */ 
  210. public function import_field( $settings, $field_id = '', $is_conversion = FALSE ) 
  211. $settings = maybe_unserialize( $settings ); 
  212. NF_Database_Models_Field::import( $settings, $field_id, $is_conversion ); 
  213.  
  214.  
  215. /** 
  216. * ACTIONS 
  217. */ 
  218.  
  219. /** 
  220. * Sets the parent object for chained methods as an Action. 
  221. * @param string $id 
  222. * @return $this 
  223. */ 
  224. public function action( $id ='' ) 
  225. $form_id = $this->_object->get_id(); 
  226.  
  227. $this->_object = new NF_Database_Models_Action( $this->_db, $id, $form_id ); 
  228.  
  229. return $this; 
  230.  
  231. /** 
  232. * Returns an action object. 
  233. * @param $id 
  234. * @return NF_Database_Models_Action 
  235. */ 
  236. public function get_action( $id ) 
  237. $form_id = $this->_object->get_id(); 
  238.  
  239. return $this->_actions[ $id ] = new NF_Database_Models_Action( $this->_db, $id, $form_id ); 
  240.  
  241. /** 
  242. * Returns an array of action objects for the set form (object). 
  243. * @param array $where 
  244. * @param bool|FALSE $fresh 
  245. * @return array 
  246. */ 
  247. public function get_actions( $where = array(), $fresh = FALSE) 
  248. if( $where || $fresh || ! $this->_actions ) { 
  249.  
  250. $form_id = $this->_object->get_id(); 
  251.  
  252. $model_shell = new NF_Database_Models_Action($this->_db, 0); 
  253.  
  254. $actions = $model_shell->find($form_id, $where); 
  255.  
  256. foreach ($actions as $action) { 
  257. $action->get_setting( 'type' ); // Pre-load the type of action for usort() 
  258. $this->_actions[$action->get_id()] = $action; 
  259.  
  260. usort( $this->_actions, 'NF_Abstracts_Action::sort_actions' ); 
  261.  
  262. return $this->_actions; 
  263.  
  264. /** 
  265. * OBJECTS 
  266. */ 
  267.  
  268. /** 
  269. * Sets the parent object for chained methods as an Object. 
  270. * @param string $id 
  271. * @return $this 
  272. */ 
  273. public function object( $id = '' ) 
  274. $parent_id = $this->_object->get_id(); 
  275. $parent_type = $this->_object->get_type(); 
  276.  
  277. $this->_object = new NF_Database_Models_Object( $this->_db, $id, $parent_id, $parent_type ); 
  278.  
  279. return $this; 
  280.  
  281. /** 
  282. * Returns an object. 
  283. * @param $id 
  284. * @return NF_Database_Models_Object 
  285. */ 
  286. public function get_object( $id ) 
  287. return $this->_objects[ $id ] = new NF_Database_Models_Object( $this->_db, $id ); 
  288.  
  289. /** 
  290. * Returns an array of objects for the set object. 
  291. * @param array $where 
  292. * @param bool|FALSE $fresh 
  293. * @return array 
  294. */ 
  295. public function get_objects( $where = array(), $fresh = FALSE) 
  296. if( $where || $fresh || ! $this->_objects ) { 
  297.  
  298. $form_id = $this->_object->get_id(); 
  299.  
  300. $model_shell = new NF_Database_Models_Object( $this->_db, 0 ); 
  301.  
  302. $objects = $model_shell->find( $form_id, $where ); 
  303.  
  304. foreach( $objects as $object ) { 
  305. $this->_objects[ $object->get_id() ] = $object; 
  306.  
  307. return $this->_objects; 
  308.  
  309. /** 
  310. * SUBMISSIONS 
  311. */ 
  312.  
  313. /** 
  314. * Submission 
  315. * Returns a single submission by ID,  
  316. * or an empty submission. 
  317. * @param string $id 
  318. * @return $this 
  319. */ 
  320. public function sub( $id = '' ) 
  321. $form_id = $this->_object->get_id(); 
  322.  
  323. $this->_object = new NF_Database_Models_Submission( $id, $form_id ); 
  324.  
  325. return $this; 
  326.  
  327. /** 
  328. * Get Submission 
  329. * Returns a single submission by ID. 
  330. * @param $id 
  331. * @return NF_Database_Models_Submission 
  332. */ 
  333. public function get_sub( $id ) 
  334. $parent_id = $this->_object->get_id(); 
  335.  
  336. return $this->_objects[ $id ] = new NF_Database_Models_Submission( $id, $parent_id ); 
  337.  
  338. /** 
  339. * Get Submissions 
  340. * Returns an array of Submission Model Objects. 
  341. * @param array $where 
  342. * @param bool|FALSE $fresh 
  343. * @return array 
  344. */ 
  345. public function get_subs( $where = array(), $fresh = FALSE, $sub_ids = array() ) 
  346. if( $where || $fresh || $sub_ids || ! $this->_objects ) { 
  347.  
  348. $form_id = $this->_object->get_id(); 
  349.  
  350. $model_shell = new NF_Database_Models_Submission( 0 ); 
  351.  
  352. $objects = $model_shell->find( $form_id, $where, $sub_ids ); 
  353.  
  354. foreach( $objects as $object ) { 
  355. $this->_objects[ $object->get_id() ] = $object; 
  356.  
  357. return $this->_objects; 
  358.  
  359. /** 
  360. * Export Submissions 
  361. * A wrapper for the Submission Model export method. 
  362. * @param array $sub_ids 
  363. * @param bool|FALSE $return 
  364. * @return string 
  365. */ 
  366. public function export_subs( array $sub_ids = array(), $return = FALSE ) 
  367. $form_id = $this->_object->get_id(); 
  368.  
  369. return NF_Database_Models_Submission::export( $form_id, $sub_ids, $return ); 
  370.  
  371. /** 
  372. * GENERIC 
  373. */ 
  374.  
  375. /** 
  376. * Get Model 
  377. * A generic method call for any object model type. 
  378. * @param $id 
  379. * @param $type 
  380. * @return bool|NF_Database_Models_Action|NF_Database_Models_Field|NF_Database_Models_Form|NF_Database_Models_Object 
  381. */ 
  382. public function get_model( $id, $type ) 
  383. global $wpdb; 
  384.  
  385. switch( $type ) { 
  386. case 'form': 
  387. return new NF_Database_Models_Form( $wpdb, $id ); 
  388. break; 
  389. case 'field': 
  390. return new NF_Database_Models_Field( $wpdb, $id ); 
  391. break; 
  392. case 'action': 
  393. return new NF_Database_Models_Action( $wpdb, $id ); 
  394. break; 
  395. case 'object': 
  396. return new NF_Database_Models_Object( $wpdb, $id ); 
  397. break; 
  398. default: 
  399. return FALSE; 
  400.  
  401. } // End Class NF_Abstracts_ModelFactory