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. * Fields 
  17. * An array of field model objects. 
  18. * @var array 
  19. */ 
  20. protected $_fields = array(); 
  21.  
  22. /** 
  23. * Actions 
  24. * An array of action model objects. 
  25. * @var array 
  26. */ 
  27. protected $_actions = array(); 
  28.  
  29. /** 
  30. * Objects 
  31. * An array of generic model objects. 
  32. * @var array 
  33. */ 
  34. protected $_objects = array(); 
  35.  
  36. //----------------------------------------------------- 
  37. // Public Methods 
  38. //----------------------------------------------------- 
  39.  
  40. /** 
  41. * NF_Abstracts_ModelFactory constructor. 
  42. * @param $db 
  43. * @param $id 
  44. */ 
  45. public function __construct( $db, $id ) 
  46. $this->_db = $db; 
  47.  
  48. $this->_object = new NF_Database_Models_Form( $this->_db, $id ); 
  49.  
  50. return $this; 
  51.  
  52. /** 
  53. * Returns the parent object set by the constructor for chained methods. 
  54. * @return object 
  55. */ 
  56. public function get() 
  57. return $this->_object; 
  58.  
  59. /** 
  60. * Get Forms 
  61. * Returns an array of Form Model Objects. 
  62. * @param array $where 
  63. * @return array|bool 
  64. */ 
  65. public function get_forms( array $where = array() ) 
  66. if( 'form' != $this->_object->get_type() ) return FALSE; 
  67.  
  68. return $this->_object->find( NULL, $where ); 
  69.  
  70. /** 
  71. * Export Form 
  72. * A wrapper for the Form Model export method. 
  73. * @param bool|FALSE $return 
  74. * @return array 
  75. */ 
  76. public function export_form( $return = FALSE ) 
  77. $form_id = $this->_object->get_id(); 
  78.  
  79. return NF_Database_Models_Form::export( $form_id, $return ); 
  80.  
  81. /** 
  82. * Import Form 
  83. * A wrapper for the Form Model import method. 
  84. * @param $import 
  85. */ 
  86. public function import_form( $import, $id = FALSE, $is_conversion = FALSE ) 
  87. if( ! is_array( $import ) ) { 
  88. $data = json_decode( html_entity_decode( $import ), true ); 
  89.  
  90. if( ! is_array( $data ) ) { 
  91. $data = maybe_unserialize( $import ); 
  92.  
  93. if( ! is_array( $data ) ) { 
  94. return false; 
  95. $import = $data; 
  96.  
  97. return NF_Database_Models_Form::import( $import, $id, $is_conversion ); 
  98.  
  99. /** 
  100. * FIELDS 
  101. */ 
  102.  
  103. /** 
  104. * Sets the parent object for chained methods as a Field. 
  105. * @param string $id 
  106. * @return $this 
  107. */ 
  108. public function field( $id = '' ) 
  109. $form_id = $this->_object->get_id(); 
  110.  
  111. $this->_object = new NF_Database_Models_Field( $this->_db, $id, $form_id ); 
  112.  
  113. return $this; 
  114.  
  115. /** 
  116. * Returns a field object. 
  117. * @param $id 
  118. * @return NF_Database_Models_Field 
  119. */ 
  120. public function get_field( $id ) 
  121. $form_id = $this->_object->get_id(); 
  122.  
  123. return $this->_fields[ $id ] = new NF_Database_Models_Field( $this->_db, $id, $form_id ); 
  124.  
  125. /** 
  126. * Returns an array of field objects for the set form (object). 
  127. * @param array $where 
  128. * @param bool|FALSE $fresh 
  129. * @return array 
  130. */ 
  131. public function get_fields( $where = array(), $fresh = FALSE) 
  132. if( $where || $fresh || ! $this->_fields ) { 
  133.  
  134. $form_id = $this->_object->get_id(); 
  135.  
  136. $model_shell = new NF_Database_Models_Field( $this->_db, 0 ); 
  137.  
  138. $fields = $model_shell->find( $form_id, $where ); 
  139.  
  140. foreach( $fields as $field ) { 
  141. $this->_fields[ $field->get_id() ] = $field; 
  142.  
  143. usort( $this->_fields, "NF_Abstracts_Field::sort_by_order" ); 
  144.  
  145. return $this->_fields; 
  146.  
  147. /** 
  148. * Import Field 
  149. * A wrapper for the Form Model import method. 
  150. * @param $import 
  151. */ 
  152. public function import_field( $settings, $field_id = '', $is_conversion = FALSE ) 
  153. $settings = maybe_unserialize( $settings ); 
  154. NF_Database_Models_Field::import( $settings, $field_id, $is_conversion ); 
  155.  
  156.  
  157. /** 
  158. * ACTIONS 
  159. */ 
  160.  
  161. /** 
  162. * Sets the parent object for chained methods as an Action. 
  163. * @param string $id 
  164. * @return $this 
  165. */ 
  166. public function action( $id ='' ) 
  167. $form_id = $this->_object->get_id(); 
  168.  
  169. $this->_object = new NF_Database_Models_Action( $this->_db, $id, $form_id ); 
  170.  
  171. return $this; 
  172.  
  173. /** 
  174. * Returns an action object. 
  175. * @param $id 
  176. * @return NF_Database_Models_Action 
  177. */ 
  178. public function get_action( $id ) 
  179. $form_id = $this->_object->get_id(); 
  180.  
  181. return $this->_actions[ $id ] = new NF_Database_Models_Action( $this->_db, $id, $form_id ); 
  182.  
  183. /** 
  184. * Returns an array of action objects for the set form (object). 
  185. * @param array $where 
  186. * @param bool|FALSE $fresh 
  187. * @return array 
  188. */ 
  189. public function get_actions( $where = array(), $fresh = FALSE) 
  190. if( $where || $fresh || ! $this->_actions ) { 
  191.  
  192. $form_id = $this->_object->get_id(); 
  193.  
  194. $model_shell = new NF_Database_Models_Action( $this->_db, 0 ); 
  195.  
  196. $actions = $model_shell->find( $form_id, $where ); 
  197.  
  198. foreach( $actions as $action ) { 
  199. $this->_actions[ $action->get_id() ] = $action; 
  200.  
  201. usort( $this->_actions, 'NF_Abstracts_Action::sort_actions' ); 
  202.  
  203. return $this->_actions; 
  204.  
  205. /** 
  206. * OBJECTS 
  207. */ 
  208.  
  209. /** 
  210. * Sets the parent object for chained methods as an Object. 
  211. * @param string $id 
  212. * @return $this 
  213. */ 
  214. public function object( $id = '' ) 
  215. $parent_id = $this->_object->get_id(); 
  216. $parent_type = $this->_object->get_type(); 
  217.  
  218. $this->_object = new NF_Database_Models_Object( $this->_db, $id, $parent_id, $parent_type ); 
  219.  
  220. return $this; 
  221.  
  222. /** 
  223. * Returns an object. 
  224. * @param $id 
  225. * @return NF_Database_Models_Object 
  226. */ 
  227. public function get_object( $id ) 
  228. return $this->_objects[ $id ] = new NF_Database_Models_Object( $this->_db, $id ); 
  229.  
  230. /** 
  231. * Returns an array of objects for the set object. 
  232. * @param array $where 
  233. * @param bool|FALSE $fresh 
  234. * @return array 
  235. */ 
  236. public function get_objects( $where = array(), $fresh = FALSE) 
  237. if( $where || $fresh || ! $this->_objects ) { 
  238.  
  239. $form_id = $this->_object->get_id(); 
  240.  
  241. $model_shell = new NF_Database_Models_Object( $this->_db, 0 ); 
  242.  
  243. $objects = $model_shell->find( $form_id, $where ); 
  244.  
  245. foreach( $objects as $object ) { 
  246. $this->_objects[ $object->get_id() ] = $object; 
  247.  
  248. return $this->_objects; 
  249.  
  250. /** 
  251. * SUBMISSIONS 
  252. */ 
  253.  
  254. /** 
  255. * Submission 
  256. * Returns a single submission by ID,  
  257. * or an empty submission. 
  258. * @param string $id 
  259. * @return $this 
  260. */ 
  261. public function sub( $id = '' ) 
  262. $form_id = $this->_object->get_id(); 
  263.  
  264. $this->_object = new NF_Database_Models_Submission( $id, $form_id ); 
  265.  
  266. return $this; 
  267.  
  268. /** 
  269. * Get Submission 
  270. * Returns a single submission by ID. 
  271. * @param $id 
  272. * @return NF_Database_Models_Submission 
  273. */ 
  274. public function get_sub( $id ) 
  275. $parent_id = $this->_object->get_id(); 
  276.  
  277. return $this->_objects[ $id ] = new NF_Database_Models_Submission( $id, $parent_id ); 
  278.  
  279. /** 
  280. * Get Submissions 
  281. * Returns an array of Submission Model Objects. 
  282. * @param array $where 
  283. * @param bool|FALSE $fresh 
  284. * @return array 
  285. */ 
  286. public function get_subs( $where = array(), $fresh = FALSE ) 
  287. if( $where || $fresh || ! $this->_objects ) { 
  288.  
  289. $form_id = $this->_object->get_id(); 
  290.  
  291. $model_shell = new NF_Database_Models_Submission( 0 ); 
  292.  
  293. $objects = $model_shell->find( $form_id, $where ); 
  294.  
  295. foreach( $objects as $object ) { 
  296. $this->_objects[ $object->get_id() ] = $object; 
  297.  
  298. return $this->_objects; 
  299.  
  300. /** 
  301. * Export Submissions 
  302. * A wrapper for the Submission Model export method. 
  303. * @param array $sub_ids 
  304. * @param bool|FALSE $return 
  305. * @return string 
  306. */ 
  307. public function export_subs( array $sub_ids = array(), $return = FALSE ) 
  308. $form_id = $this->_object->get_id(); 
  309.  
  310. return NF_Database_Models_Submission::export( $form_id, $sub_ids, $return ); 
  311.  
  312. /** 
  313. * GENERIC 
  314. */ 
  315.  
  316. /** 
  317. * Get Model 
  318. * A generic method call for any object model type. 
  319. * @param $id 
  320. * @param $type 
  321. * @return bool|NF_Database_Models_Action|NF_Database_Models_Field|NF_Database_Models_Form|NF_Database_Models_Object 
  322. */ 
  323. public function get_model( $id, $type ) 
  324. global $wpdb; 
  325.  
  326. switch( $type ) { 
  327. case 'form': 
  328. return new NF_Database_Models_Form( $wpdb, $id ); 
  329. break; 
  330. case 'field': 
  331. return new NF_Database_Models_Field( $wpdb, $id ); 
  332. break; 
  333. case 'action': 
  334. return new NF_Database_Models_Action( $wpdb, $id ); 
  335. break; 
  336. case 'object': 
  337. return new NF_Database_Models_Object( $wpdb, $id ); 
  338. break; 
  339. default: 
  340. return FALSE; 
  341.  
  342. } // End Class NF_Abstracts_ModelFactory