PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/system/expressionengine/third_party/freeform/libraries/Freeform_forms.php

https://bitbucket.org/studiobreakfast/sync
PHP | 1019 lines | 564 code | 194 blank | 261 comment | 75 complexity | 297cbb4cd5898b9fbef50e7e361c9a75 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Solspace - Freeform
  4. *
  5. * @package Solspace:Freeform
  6. * @author Solspace DevTeam
  7. * @copyright Copyright (c) 2008-2012, Solspace, Inc.
  8. * @link http://solspace.com/docs/addon/c/Freeform/
  9. * @filesource ./system/expressionengine/third_party/freeform/libraries/
  10. */
  11. /**
  12. * Freeform - Forms Library
  13. *
  14. * @package Solspace:Freeform
  15. * @author Solspace DevTeam
  16. * @filesource ./system/expressionengine/third_party/freeform/libraries/Freeform_forms.php
  17. */
  18. // EE 2.0's Wizard doesn't set this constant
  19. if ( ! defined('APP_VER')) define('APP_VER', '2.0');
  20. $__parent_folder = rtrim(realpath(rtrim(dirname(__FILE__), "/") . '/../'), '/') . '/';
  21. if ( ! class_exists('Addon_builder_freeform'))
  22. {
  23. require_once $__parent_folder . 'addon_builder/addon_builder.php';
  24. }
  25. unset($__parent_folder);
  26. class Freeform_forms extends Addon_builder_freeform
  27. {
  28. private $data_cache = array();
  29. private $default_path = '';
  30. private $hash_expire;
  31. private $cookie_expire;
  32. public $hash_cookie_name = 'freeform_multipage_hash';
  33. // --------------------------------------------------------------------
  34. /**
  35. * Constructor
  36. *
  37. * @access public
  38. * @return null
  39. */
  40. public function __construct()
  41. {
  42. parent::__construct('freeform');
  43. ee()->load->model('freeform_field_model');
  44. ee()->load->model('freeform_form_model');
  45. ee()->load->library('freeform_fields');
  46. }
  47. //END __construct
  48. // --------------------------------------------------------------------
  49. /**
  50. * check_duplicate - checks to see if a form has a dupe for logged
  51. * in user or by current ID
  52. *
  53. * @access public
  54. * @param int ID of form to check
  55. * @param string what field to prevent on
  56. * @param string prevention data to check
  57. * @param bool check site_id as well
  58. * @return bool is duplicate
  59. */
  60. public function check_duplicate ($form_id = 0, $prevent_on = '',
  61. $check = '', $site_id = FALSE)
  62. {
  63. //admins or form 0, goodbye
  64. if ( ! $this->is_positive_intlike($form_id))
  65. {
  66. return FALSE;
  67. }
  68. $form_name = ee()->freeform_form_model->table_name($form_id);
  69. //custom dupe check?
  70. if ( is_string($prevent_on) AND
  71. ! in_array(
  72. $prevent_on,
  73. array('member_id', 'author_id', 'ip_address', ''),
  74. TRUE
  75. )
  76. )
  77. {
  78. $prevent_field = ee()->freeform_field_model->get_column_name(
  79. $prevent_on,
  80. 'name'
  81. );
  82. //this is sep because we dont want to fall back on
  83. //something else if this fails. That would confuse
  84. if ($prevent_field AND
  85. ee()->db->field_exists($prevent_field, $form_name))
  86. {
  87. ee()->db->where($prevent_field, $check);
  88. }
  89. else
  90. {
  91. return FALSE;
  92. }
  93. }
  94. //user logged in?
  95. else if ($prevent_on !== 'ip_address' AND
  96. ee()->session->userdata['member_id'] != '0' )
  97. {
  98. ee()->db->where('author_id', ee()->session->userdata['member_id']);
  99. }
  100. //anonymous users
  101. else if ( ee()->input->ip_address() != '0.0.0.0')
  102. {
  103. ee()->db->where('ip_address', ee()->input->ip_address());
  104. }
  105. else
  106. {
  107. return FALSE;
  108. }
  109. if ($site_id)
  110. {
  111. ee()->db->where('site_id', ee()->config->item('site_id'));
  112. }
  113. ee()->db->where('complete', 'y');
  114. ee()->db->from($form_name);
  115. return ( ee()->db->count_all_results() > 0 );
  116. }
  117. //END check_duplicate
  118. // --------------------------------------------------------------------
  119. /**
  120. * entry
  121. *
  122. * inserts/updates data into form
  123. *
  124. * @access private
  125. * @param int ID of form to check
  126. * @param array field input data to validate
  127. * @param int entry_id for updating?
  128. * @param bool store this as a complete entry?
  129. * @return entry_id id of insert
  130. */
  131. private function entry ($form_id, $field_input_data, $entry_id = 0, $complete = TRUE)
  132. {
  133. if ( ! $this->data->is_valid_form_id($form_id))
  134. {
  135. return $this->actions()->full_stop(lang('invalid_form_id'));
  136. }
  137. $form_data = $this->data->get_form_info($form_id);
  138. $edit = FALSE;
  139. $field_save_data = array(
  140. 'author_id' => isset($field_input_data['author_id']) ?
  141. $field_input_data['author_id'] :
  142. ee()->session->userdata('member_id'),
  143. 'ip_address' => isset($field_input_data['ip_address']) ?
  144. $field_input_data['ip_address'] :
  145. ee()->input->ip_address(),
  146. 'entry_date' => isset($field_input_data['entry_date']) ?
  147. $field_input_data['entry_date'] :
  148. ee()->localize->now,
  149. 'edit_date' => isset($field_input_data['edit_date']) ?
  150. $field_input_data['edit_date'] :
  151. 0,
  152. 'status' => isset($field_input_data['status']) ?
  153. $field_input_data['status'] :
  154. $form_data['default_status'],
  155. 'site_id' => ee()->config->item('site_id')
  156. );
  157. if ((int) $entry_id !== 0)
  158. {
  159. if ($this->data->is_valid_entry_id($entry_id, $form_id))
  160. {
  161. $edit = TRUE;
  162. //we don't want to undo the previous data
  163. $field_save_data = array(
  164. 'edit_date' => ee()->localize->now
  165. );
  166. if (isset($field_input_data['status']))
  167. {
  168. $field_save_data['status'] = $field_input_data['status'];
  169. }
  170. }
  171. else
  172. {
  173. $this->actions()->full_stop(
  174. lang('invalid_entry_id') . ' - ' . $entry_id
  175. );
  176. }
  177. }
  178. //complete? we need this no matter what
  179. $field_save_data['complete'] = ((bool) $complete) ? 'y' : 'n';
  180. foreach ($form_data['fields'] as $field_id => $field_data)
  181. {
  182. if ( ! isset($field_input_data[$field_data['field_name']]))
  183. {
  184. continue;
  185. }
  186. $fid = ee()->freeform_form_model->form_field_prefix . $field_id;
  187. //get class instance of field
  188. $instance =& ee()->freeform_fields->get_field_instance(array(
  189. 'field_id' => $field_id,
  190. 'form_id' => $form_id,
  191. 'edit' => $edit,
  192. 'entry_id' => $entry_id,
  193. 'edit' => $edit,
  194. 'extra_settings' => array('entry_id' => $entry_id)
  195. ));
  196. $field_save_data[$fid] = $instance->save(
  197. $field_input_data[$field_data['field_name']]
  198. );
  199. }
  200. ee()->load->model('freeform_entry_model');
  201. ee()->freeform_entry_model->id($form_id);
  202. if ($edit)
  203. {
  204. ee()->freeform_entry_model->update(
  205. $field_save_data,
  206. array('entry_id' => $entry_id)
  207. );
  208. }
  209. else
  210. {
  211. $entry_id = ee()->freeform_entry_model->insert(
  212. $field_save_data
  213. );
  214. }
  215. // -------------------------------------
  216. // post save
  217. // -------------------------------------
  218. foreach ($form_data['fields'] as $field_id => $field_data)
  219. {
  220. if ( ! isset($field_input_data[$field_data['field_name']]))
  221. {
  222. continue;
  223. }
  224. //get class instance of field
  225. $instance =& ee()->freeform_fields->get_field_instance(array(
  226. 'field_id' => $field_id,
  227. 'form_id' => $form_id,
  228. 'entry_id' => $entry_id,
  229. 'edit' => $edit,
  230. 'extra_settings' => array('entry_id' => $entry_id)
  231. ));
  232. $fid = ee()->freeform_form_model->form_field_prefix . $field_id;
  233. $post_save_data = isset($field_input_data[$field_data['field_name']]) ?
  234. $field_input_data[$field_data['field_name']] :
  235. '';
  236. $instance->post_save($field_input_data[$field_data['field_name']]);
  237. }
  238. return $entry_id;
  239. }
  240. //END entry
  241. // --------------------------------------------------------------------
  242. /**
  243. * insert_new_entry
  244. *
  245. * inserts data into form
  246. *
  247. * @access public
  248. * @param int ID of form to check
  249. * @param array field input data to validate
  250. * @return entry_id id of insert
  251. */
  252. public function insert_new_entry ($form_id, $field_input_data)
  253. {
  254. return $this->entry($form_id, $field_input_data);
  255. }
  256. //END insert_new_entry
  257. // --------------------------------------------------------------------
  258. /**
  259. * update_entry
  260. *
  261. * updates data entry
  262. *
  263. * @access public
  264. * @param int ID of form to check
  265. * @param int entry_id to update
  266. * @param array form_input_data to insert
  267. * @return bool update successful
  268. */
  269. public function update_entry ($form_id, $entry_id, $form_input_data)
  270. {
  271. return $this->entry($form_id, $form_input_data, $entry_id);
  272. }
  273. //END update_entry
  274. // --------------------------------------------------------------------
  275. /**
  276. * Multipage hash expire time
  277. *
  278. * @access public
  279. * @return bool
  280. */
  281. public function hash_expire_time ()
  282. {
  283. if ( ! isset($this->hash_expire))
  284. {
  285. $pref = $this->preference('multi_form_timeout');
  286. $num = $this->is_positive_intlike($pref) ? $pref : 7200;
  287. $this->hash_expire = ee()->localize->now - $num;
  288. }
  289. return $this->hash_expire;
  290. }
  291. //end hash_expire_time
  292. // --------------------------------------------------------------------
  293. /**
  294. * hash clean
  295. * cleans out old hashes and entries
  296. *
  297. * delete unfinished entries? Default to yes
  298. * (the pref is a default no, but no means yes!)
  299. *
  300. * @access public
  301. * @return null
  302. */
  303. public function hash_clean_up ()
  304. {
  305. //delete entries?
  306. $delete_entries = ! $this->check_yes(
  307. $this->preference('keep_unfinished_multi_form')
  308. );
  309. //all sites or just this one?
  310. if ( ! $this->data->show_all_sites())
  311. {
  312. ee()->db->where('site_id', ee()->config->item('site_id'));
  313. }
  314. //could be faster if we have a lot
  315. if ( ! $delete_entries)
  316. {
  317. ee()->db->select('hash');
  318. }
  319. $hashes_q = ee()->db->get_where(
  320. 'freeform_multipage_hashes',
  321. array('date <' => $this->hash_expire_time())
  322. );
  323. if ($hashes_q->num_rows() > 0)
  324. {
  325. $hashes = array();
  326. $entry_ids = array();
  327. foreach ($hashes_q->result_array() as $row)
  328. {
  329. if ($row['form_id'] != 0 AND $delete_entries)
  330. {
  331. if ( ! isset($entry_ids[$row['form_id']]))
  332. {
  333. $entry_ids[$row['form_id']] = array();
  334. }
  335. $entry_ids[$row['form_id']][] = $row['entry_id'];
  336. }
  337. $hashes[] = $row['hash'];
  338. }
  339. if ($delete_entries)
  340. {
  341. ee()->load->model('freeform_form_model');
  342. ee()->load->model('freeform_entry_model');
  343. ee()->load->library('freeform_fields');
  344. //delete all entries in each form
  345. //but ONLY if they are incomplete
  346. foreach ($entry_ids as $form_id => $f_entry_ids)
  347. {
  348. if ( ! ee()->db->table_exists(
  349. ee()->freeform_form_model->table_name($form_id)
  350. )
  351. )
  352. {
  353. continue;
  354. }
  355. //lets make sure they are not complete
  356. $delete_q = ee()->freeform_entry_model
  357. ->id($form_id)
  358. ->where_in('entry_id', $f_entry_ids)
  359. ->where('complete', 'n')
  360. ->select('entry_id')
  361. ->get();
  362. if ($delete_q !== FALSE)
  363. {
  364. $delete_ids = array();
  365. foreach ($delete_q as $row)
  366. {
  367. $delete_ids[] = $row['entry_id'];
  368. }
  369. ee()->freeform_fields->apply_field_method(array(
  370. 'method' => 'delete',
  371. 'form_id' => $form_id,
  372. 'entry_id' => $delete_ids
  373. ));
  374. ee()->freeform_entry_model
  375. ->id($form_id)
  376. ->where_in('entry_id', $delete_ids)
  377. ->delete();
  378. }
  379. }
  380. }
  381. if ( ! empty($hashes))
  382. {
  383. ee()->db->where_in('hash', $hashes);
  384. ee()->db->delete('freeform_multipage_hashes');
  385. }
  386. }
  387. }
  388. //end hash_clean_up
  389. // --------------------------------------------------------------------
  390. /**
  391. * Add field to form
  392. *
  393. * @access public
  394. * @param int $form_id id of form to add field to
  395. * @param mixed $new_field_ids field id or array of fields to add to form
  396. * @return void
  397. */
  398. public function add_field_to_form ($form_id, $new_field_ids)
  399. {
  400. if ( ! $this->is_positive_intlike($form_id))
  401. {
  402. return FALSE;
  403. }
  404. $form_data = $this->data->get_form_info($form_id);
  405. if (is_array($form_data['field_ids']))
  406. {
  407. $field_ids = $form_data['field_ids'];
  408. }
  409. else if ( is_string($form_data['field_ids']))
  410. {
  411. $field_ids = $this->actions()->pipe_split($form_data['field_ids']);
  412. }
  413. else
  414. {
  415. $field_ids = array();
  416. }
  417. sort($field_ids);
  418. // -------------------------------------
  419. // make sure field ids are an array and
  420. // clean them
  421. // -------------------------------------
  422. if ( is_string($new_field_ids))
  423. {
  424. $new_field_ids = $this->actions()->pipe_split($new_field_ids);
  425. }
  426. if ( ! is_array($new_field_ids))
  427. {
  428. $new_field_ids = array($new_field_ids);
  429. }
  430. $new_field_ids = array_filter($new_field_ids, array($this, 'is_positive_intlike'));
  431. if (empty($new_field_ids))
  432. {
  433. return FALSE;
  434. }
  435. sort($new_field_ids);
  436. // -------------------------------------
  437. // check combined
  438. // -------------------------------------
  439. $our_powers_combined = array_unique(array_merge($field_ids, $new_field_ids));
  440. sort($our_powers_combined);
  441. //if there are no changes we can move on
  442. if ($field_ids == $our_powers_combined)
  443. {
  444. return FALSE;
  445. }
  446. // -------------------------------------
  447. // update
  448. // -------------------------------------
  449. return $this->update_form(
  450. $form_id,
  451. array(
  452. 'field_ids' => implode('|', $our_powers_combined)
  453. )
  454. );
  455. }
  456. //END add_field_to_form
  457. // --------------------------------------------------------------------
  458. /**
  459. * Remove field from form
  460. *
  461. * @access public
  462. * @param int $form_id id of form to remove field from
  463. * @param int $field_id field id to remove
  464. * @return null
  465. */
  466. public function remove_field_from_form ($form_id, $field_id)
  467. {
  468. $form_data = ee()->freeform_form_model->get_row($form_id);
  469. $field_ids = $form_data['field_ids'];
  470. $order_ids = $this->actions()->pipe_split($form_data['field_order']);
  471. // -------------------------------------
  472. // if it isn't present, nothing to remove
  473. // -------------------------------------
  474. if ( ! in_array($field_id, $field_ids))
  475. {
  476. return;
  477. }
  478. // -------------------------------------
  479. // remove from field ids
  480. // -------------------------------------
  481. //removing the field from field ids is simple because
  482. //sort order isn't important
  483. unset($field_ids[array_search($field_id, $field_ids)]);
  484. sort($field_ids);
  485. $new_order_ids = array();
  486. if ( ! empty($order_ids))
  487. {
  488. foreach ($order_ids as $id)
  489. {
  490. if ($id != $field_id)
  491. {
  492. $new_order_ids[] = $id;
  493. }
  494. }
  495. }
  496. // -------------------------------------
  497. // call remove
  498. // -------------------------------------
  499. ee()->load->library('freeform_fields');
  500. $instance =& ee()->freeform_fields->get_field_instance($field_id);
  501. if (is_callable(array($instance, 'remove_from_form')))
  502. {
  503. $instance->remove_from_form($form_id);
  504. }
  505. // -------------------------------------
  506. // remove field from form
  507. // -------------------------------------
  508. $this->update_form(
  509. $form_id,
  510. array(
  511. 'field_ids' => implode('|', $field_ids),
  512. 'field_order' => implode('|', $new_order_ids)
  513. )
  514. );
  515. }
  516. //END remove_field_from_form
  517. // --------------------------------------------------------------------
  518. /**
  519. * Create Form
  520. *
  521. * Creates a form and its corosponding tables
  522. *
  523. * @access public
  524. * @param array $data data to insert
  525. * @return int form id
  526. */
  527. public function create_form ($data)
  528. {
  529. ee()->load->model('freeform_form_model');
  530. $form_id = ee()->freeform_form_model->insert($data);
  531. //add fields
  532. if ( isset($data['field_ids']) AND
  533. trim($data['field_ids']) !== '')
  534. {
  535. $this->update_form_fields($form_id, $data['field_ids']);
  536. }
  537. return $form_id;
  538. }
  539. //end create_form
  540. // --------------------------------------------------------------------
  541. /**
  542. * Update Form
  543. *
  544. * Updates an existing form with new data and an edit date
  545. *
  546. * @access public
  547. * @param int $form_id the id desired form to be updated
  548. * @param array $data data to insert
  549. * @return null
  550. */
  551. public function update_form ($form_id, $data)
  552. {
  553. ee()->load->model('freeform_form_model');
  554. ee()->freeform_form_model->update($data, array('form_id' => $form_id));
  555. //add fields
  556. if ( isset($data['field_ids']))
  557. {
  558. $this->update_form_fields($form_id, $data['field_ids']);
  559. }
  560. }
  561. //end update_form
  562. // --------------------------------------------------------------------
  563. /**
  564. * update_form_fields
  565. *
  566. * @access public
  567. * @param int form_id number
  568. * @param mixed pipe delimited list, single id, or array of fields to add/remove
  569. * @return bool success
  570. */
  571. public function update_form_fields ($form_id, $field_ids)
  572. {
  573. if ( ! $this->is_positive_intlike($form_id))
  574. {
  575. return FALSE;
  576. }
  577. ee()->load->dbforge();
  578. ee()->load->model('freeform_form_model');
  579. ee()->load->model('freeform_entry_model');
  580. // -------------------------------------
  581. // form data? form data
  582. // -------------------------------------
  583. $table_name = ee()->freeform_form_model->table_name($form_id);
  584. $p_table_name = (
  585. substr($table_name, 0,strlen(ee()->db->dbprefix)
  586. ) !== ee()->db->dbprefix) ?
  587. ee()->db->dbprefix . $table_name :
  588. $table_name;
  589. // -------------------------------------
  590. // get a list of current fields so we don't overwrite
  591. // -------------------------------------
  592. $current_fields = ee()->freeform_entry_model
  593. ->id($form_id)
  594. ->list_table_fields(FALSE);
  595. //we only want the fields aside from the defaults to test against
  596. $current_field_names = array_diff(
  597. array_keys(
  598. ee()->freeform_form_model->default_form_table_columns
  599. ),
  600. $current_fields
  601. );
  602. $old_fields = array();
  603. //make current fields $custom_field_id => form_field_name array
  604. foreach ($current_fields as $field_name)
  605. {
  606. if (preg_match(
  607. '/^' . ee()->freeform_form_model->form_field_prefix . '/',
  608. $field_name
  609. )
  610. )
  611. {
  612. $old_fields[(int) str_replace(
  613. ee()->freeform_form_model->form_field_prefix,
  614. '',
  615. $field_name
  616. )] = $field_name;
  617. }
  618. }
  619. $old_field_ids = array_keys($old_fields);
  620. //we need to make sure our input is an array
  621. if ( ! is_array($field_ids))
  622. {
  623. //number sets (most common)
  624. if (stristr($field_ids, '|'))
  625. {
  626. $field_ids = $this->actions()->pipe_split($field_ids);
  627. }
  628. //just a number
  629. else if ( is_numeric($field_ids))
  630. {
  631. $field_ids = array($field_ids);
  632. }
  633. //blank
  634. else if ( is_string($field_ids) AND trim($field_ids) === '')
  635. {
  636. $field_ids = array();
  637. }
  638. //at this point its rogue data and we need to stop
  639. //to avoid errors
  640. else
  641. {
  642. return FALSE;
  643. }
  644. }
  645. // -------------------------------------
  646. // add/remove fields
  647. // -------------------------------------
  648. $remove = array_unique(array_diff($old_field_ids, $field_ids));
  649. $add = array_unique(array_diff($field_ids, $old_field_ids));
  650. sort($remove);
  651. sort($add);
  652. //removing?
  653. if ( ! empty($remove))
  654. {
  655. ee()->load->library('freeform_fields');
  656. foreach ($remove as $remove_id)
  657. {
  658. //lets not do anything hokey
  659. if (array_key_exists($remove_id, $old_fields))
  660. {
  661. // -------------------------------------
  662. // call remove before actually removing
  663. // -------------------------------------
  664. $instance =& ee()->freeform_fields->get_field_instance($remove_id);
  665. if (is_callable(array($instance, 'remove_from_form')))
  666. {
  667. $instance->remove_from_form($form_id);
  668. }
  669. //because db->list_fields is a stupid POS
  670. if ($this->column_exists($old_fields[$remove_id], $p_table_name, FALSE))
  671. {
  672. ee()->dbforge->drop_column(
  673. $table_name,
  674. $old_fields[$remove_id]
  675. );
  676. }
  677. }
  678. }
  679. }
  680. //adding?
  681. if ( ! empty($add))
  682. {
  683. $add_fields = array();
  684. foreach ($add as $add_id)
  685. {
  686. //lets not do anything hokey
  687. if ( ! array_key_exists($add_id, $old_fields))
  688. {
  689. $fid = ee()->freeform_form_model->form_field_prefix . $add_id;
  690. $add_fields[$fid] = ee()->freeform_form_model->custom_field_info;
  691. }
  692. }
  693. if ( ! empty($add_fields))
  694. {
  695. ee()->dbforge->add_column($table_name, $add_fields);
  696. }
  697. }
  698. }
  699. //END update_form_fields
  700. // --------------------------------------------------------------------
  701. /**
  702. * Delete Form
  703. *
  704. * Removes a form and its corosponding entries table
  705. *
  706. * @access public
  707. * @param form_id id of form to delete
  708. */
  709. public function delete_form ($form_id)
  710. {
  711. if ( ! $this->is_positive_intlike($form_id))
  712. {
  713. return FALSE;
  714. }
  715. $form_data = $this->data->get_form_info($form_id);
  716. if ($form_data == FALSE)
  717. {
  718. return FALSE;
  719. }
  720. ee()->load->library('freeform_fields');
  721. if ( ! empty($form_data['field_ids']))
  722. {
  723. // -------------------------------------
  724. // call remove before actually removing
  725. // -------------------------------------
  726. foreach ($form_data['field_ids'] as $field_id)
  727. {
  728. $instance =& ee()->freeform_fields->get_field_instance($field_id);
  729. if (is_callable(array($instance, 'remove_from_form')))
  730. {
  731. $instance->remove_from_form($form_id);
  732. }
  733. }
  734. }
  735. // -------------------------------------
  736. // clean any left over hashes
  737. // -------------------------------------
  738. $this->hash_clean_up();
  739. // -------------------------------------
  740. // check composer
  741. // -------------------------------------
  742. if ($this->is_positive_intlike($form_data['composer_id']))
  743. {
  744. //only want to remove it if its just used here.
  745. $total_used = ee()->freeform_form_model
  746. ->where(
  747. 'composer_id',
  748. $form_data['composer_id']
  749. )
  750. ->count();
  751. if ($total_used <= 1)
  752. {
  753. ee()->load->model('freeform_composer_model');
  754. ee()->freeform_composer_model
  755. ->where('composer_id', $form_data['composer_id'])
  756. ->delete();
  757. }
  758. }
  759. return ee()->freeform_form_model->delete($form_id);
  760. }
  761. //END delete_form
  762. // --------------------------------------------------------------------
  763. /**
  764. * Delete entries from a form (one form)
  765. *
  766. * @access public
  767. * @param int $form_id form_id to delete entries from
  768. * @param mixed $entry_ids array or int of entries to be deleted
  769. * @return mixed return value of db delete
  770. */
  771. public function delete_entries ($form_id, $entry_ids)
  772. {
  773. // -------------------------------------
  774. // lots of validation
  775. // -------------------------------------
  776. if ( ! $this->is_positive_intlike($form_id) OR
  777. ! $this->data->is_valid_form_id($form_id))
  778. {
  779. return FALSE;
  780. }
  781. if ( ! is_array($entry_ids) AND
  782. ! $this->is_positive_intlike($entry_ids))
  783. {
  784. return FALSE;
  785. }
  786. if ( ! is_array($entry_ids))
  787. {
  788. $entry_ids = array($entry_ids);
  789. }
  790. $entry_ids = array_filter($entry_ids, array($this, 'is_positive_intlike'));
  791. if (empty($entry_ids))
  792. {
  793. return FALSE;
  794. }
  795. // -------------------------------------
  796. // pre hook
  797. // -------------------------------------
  798. if (ee()->extensions->active_hook('freeform_module_entry_delete') === TRUE)
  799. {
  800. ee()->extensions->universal_call(
  801. 'freeform_module_entry_delete',
  802. $form_id,
  803. $entry_ids,
  804. $this
  805. );
  806. if (ee()->extensions->end_script === TRUE) return;
  807. }
  808. // -------------------------------------
  809. // MUSH!
  810. // -------------------------------------
  811. ee()->load->library('freeform_fields');
  812. ee()->freeform_fields->apply_field_method(array(
  813. 'method' => 'delete',
  814. 'form_id' => $form_id,
  815. 'entry_id' => $entry_ids
  816. ));
  817. ee()->load->model('freeform_entry_model');
  818. return ee()->freeform_entry_model
  819. ->id($form_id)
  820. ->where_in('entry_id', $entry_ids)
  821. ->delete();
  822. }
  823. //END delete_entries
  824. }
  825. //END Freeform_forms