ModelFactory.php 13 KB

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