PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/gravityforms/forms_model.php

https://github.com/petergibbons/OpenCounterWP
PHP | 2738 lines | 2154 code | 484 blank | 100 comment | 392 complexity | 2f6009b54b6509716289be16ffd848b2 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. require_once(ABSPATH . "/wp-includes/post.php");
  3. define("GFORMS_MAX_FIELD_LENGTH", 200);
  4. class RGFormsModel{
  5. public static $uploaded_files = array();
  6. public static function get_form_table_name(){
  7. global $wpdb;
  8. return $wpdb->prefix . "rg_form";
  9. }
  10. public static function get_meta_table_name(){
  11. global $wpdb;
  12. return $wpdb->prefix . "rg_form_meta";
  13. }
  14. public static function get_form_view_table_name(){
  15. global $wpdb;
  16. return $wpdb->prefix . "rg_form_view";
  17. }
  18. public static function get_lead_table_name(){
  19. global $wpdb;
  20. return $wpdb->prefix . "rg_lead";
  21. }
  22. public static function get_lead_meta_table_name(){
  23. global $wpdb;
  24. return $wpdb->prefix . "rg_lead_meta";
  25. }
  26. public static function get_lead_notes_table_name(){
  27. global $wpdb;
  28. return $wpdb->prefix . "rg_lead_notes";
  29. }
  30. public static function get_lead_details_table_name(){
  31. global $wpdb;
  32. return $wpdb->prefix . "rg_lead_detail";
  33. }
  34. public static function get_lead_details_long_table_name(){
  35. global $wpdb;
  36. return $wpdb->prefix . "rg_lead_detail_long";
  37. }
  38. public static function get_lead_view_name(){
  39. global $wpdb;
  40. return $wpdb->prefix . "rg_lead_view";
  41. }
  42. public static function get_forms($is_active = null, $sort="title ASC"){
  43. global $wpdb;
  44. $form_table_name = self::get_form_table_name();
  45. $lead_table_name = self::get_lead_table_name();
  46. $view_table_name = self::get_form_view_table_name();
  47. $active_clause = $is_active !== null ? $wpdb->prepare("WHERE is_active=%d", $is_active) : "";
  48. $order_by = !empty($sort) ? "ORDER BY $sort" : "";
  49. $sql = "SELECT f.id, f.title, f.date_created, f.is_active, 0 as lead_count, 0 view_count
  50. FROM $form_table_name f
  51. $active_clause
  52. $order_by";
  53. //Getting all forms
  54. $forms = $wpdb->get_results($sql);
  55. //Getting entry count per form
  56. $sql = "SELECT form_id, count(id) as lead_count FROM $lead_table_name l WHERE status='active' GROUP BY form_id";
  57. $entry_count = $wpdb->get_results($sql);
  58. //Getting view count per form
  59. $sql = "SELECT form_id, sum(count) as view_count FROM $view_table_name GROUP BY form_id";
  60. $view_count = $wpdb->get_results($sql);
  61. //Adding entry counts and to form array
  62. foreach($forms as &$form){
  63. foreach($view_count as $count){
  64. if($count->form_id == $form->id){
  65. $form->view_count = $count->view_count;
  66. break;
  67. }
  68. }
  69. foreach($entry_count as $count){
  70. if($count->form_id == $form->id){
  71. $form->lead_count = $count->lead_count;
  72. break;
  73. }
  74. }
  75. }
  76. return $forms;
  77. }
  78. public static function get_forms_by_id($ids){
  79. global $wpdb;
  80. $form_table_name = self::get_form_table_name();
  81. $meta_table_name = self::get_meta_table_name();
  82. if(is_array($ids))
  83. $ids = implode(",", $ids);
  84. $results = $wpdb->get_results(" SELECT display_meta FROM {$form_table_name} f
  85. INNER JOIN {$meta_table_name} m ON f.id = m.form_id
  86. WHERE id in({$ids})", ARRAY_A);
  87. foreach ($results as &$result)
  88. $result = maybe_unserialize($result["display_meta"]);
  89. return $results;
  90. }
  91. public static function get_form_payment_totals($form_id){
  92. global $wpdb;
  93. $lead_table_name = self::get_lead_table_name();
  94. $sql = $wpdb->prepare(" SELECT sum(payment_amount) revenue, count(l.id) orders
  95. FROM $lead_table_name l
  96. WHERE form_id=%d AND payment_amount IS NOT null", $form_id);
  97. $totals = $wpdb->get_row($sql, ARRAY_A);
  98. $active = $wpdb->get_var($wpdb->prepare(" SELECT count(id) as active
  99. FROM $lead_table_name
  100. WHERE form_id=%d AND payment_status='Active'", $form_id));
  101. if(empty($active))
  102. $active = 0;
  103. $totals["active"] = $active;
  104. return $totals;
  105. }
  106. public static function get_form_counts($form_id){
  107. global $wpdb;
  108. $lead_table_name = self::get_lead_table_name();
  109. $sql = $wpdb->prepare(
  110. "SELECT
  111. (SELECT count(0) FROM $lead_table_name WHERE form_id=%d AND status='active') as total,
  112. (SELECT count(0) FROM $lead_table_name WHERE is_read=0 AND status='active' AND form_id=%d) as unread,
  113. (SELECT count(0) FROM $lead_table_name WHERE is_starred=1 AND status='active' AND form_id=%d) as starred,
  114. (SELECT count(0) FROM $lead_table_name WHERE status='spam' AND form_id=%d) as spam,
  115. (SELECT count(0) FROM $lead_table_name WHERE status='trash' AND form_id=%d) as trash",
  116. $form_id, $form_id, $form_id, $form_id, $form_id);
  117. $results = $wpdb->get_results($sql, ARRAY_A);
  118. return $results[0];
  119. }
  120. public static function get_form_summary(){
  121. global $wpdb;
  122. $form_table_name = self::get_form_table_name();
  123. $lead_table_name = self::get_lead_table_name();
  124. $sql = "SELECT l.form_id, count(l.id) as unread_count
  125. FROM $lead_table_name l
  126. WHERE is_read=0 AND status='active'
  127. GROUP BY form_id";
  128. //getting number of unread and total leads for all forms
  129. $unread_results = $wpdb->get_results($sql, ARRAY_A);
  130. $sql = "SELECT l.form_id, max(l.date_created) as last_lead_date, count(l.id) as total_leads
  131. FROM $lead_table_name l
  132. WHERE status='active'
  133. GROUP BY form_id";
  134. $lead_date_results = $wpdb->get_results($sql, ARRAY_A);
  135. $sql = "SELECT id, title, '' as last_lead_date, 0 as unread_count
  136. FROM $form_table_name
  137. WHERE is_active=1
  138. ORDER BY title";
  139. $forms = $wpdb->get_results($sql, ARRAY_A);
  140. for($i=0; $count = sizeof($forms), $i<$count; $i++){
  141. if(is_array($unread_results)){
  142. foreach($unread_results as $unread_result){
  143. if($unread_result["form_id"] == $forms[$i]["id"]){
  144. $forms[$i]["unread_count"] = $unread_result["unread_count"];
  145. break;
  146. }
  147. }
  148. }
  149. if(is_array($lead_date_results)){
  150. foreach($lead_date_results as $lead_date_result){
  151. if($lead_date_result["form_id"] == $forms[$i]["id"]){
  152. $forms[$i]["last_lead_date"] = $lead_date_result["last_lead_date"];
  153. $forms[$i]["total_leads"] = $lead_date_result["total_leads"];
  154. break;
  155. }
  156. }
  157. }
  158. }
  159. return $forms;
  160. }
  161. public static function get_form_count(){
  162. global $wpdb;
  163. $form_table_name = self::get_form_table_name();
  164. $results = $wpdb->get_results("SELECT count(0) as count FROM $form_table_name UNION ALL SELECT count(0) as count FROM $form_table_name WHERE is_active=1 ");
  165. return array( "total" => intval($results[0]->count),
  166. "active" => intval($results[1]->count),
  167. "inactive" => intval($results[0]->count) - intval($results[1]->count)
  168. );
  169. }
  170. public static function get_form_id($form_title){
  171. $forms = self::get_forms();
  172. foreach($forms as $form){
  173. $sanitized_name = str_replace("[", "", str_replace("]","", $form->title));
  174. if($form->title == $form_title || $sanitized_name == $form_title)
  175. return $form->id;
  176. }
  177. return 0;
  178. }
  179. public static function get_form($form_id){
  180. global $wpdb;
  181. $table_name = self::get_form_table_name();
  182. $results = $wpdb->get_results($wpdb->prepare("SELECT * FROM $table_name WHERE id=%d", $form_id));
  183. return $results[0];
  184. }
  185. public static function get_form_meta($form_id){
  186. global $wpdb;
  187. $table_name = self::get_meta_table_name();
  188. $form = maybe_unserialize($wpdb->get_var($wpdb->prepare("SELECT display_meta FROM $table_name WHERE form_id=%d", $form_id)));
  189. $page_number = 1;
  190. $description_placement = rgar($form, "descriptionPlacement") == "above" ? "above" : "below";
  191. if(is_array($form["fields"])){
  192. foreach($form["fields"] as &$field){
  193. $field["formId"] = $form["id"];
  194. $field["pageNumber"] = $page_number;
  195. $field["descriptionPlacement"] = $description_placement;
  196. if($field["type"] == "page"){
  197. $page_number++;
  198. $field["pageNumber"] = $page_number;
  199. }
  200. }
  201. }
  202. return $form;
  203. }
  204. public static function add_default_properties($form){
  205. if(is_array($form["fields"])){
  206. $all_fields = array("adminLabel"=>"","adminOnly"=>"","allowsPrepopulate"=>"","defaultValue"=>"","description"=>"","content"=>"","cssClass"=>"",
  207. "errorMessage"=>"","id"=>"","inputName"=>"","isRequired"=>"","label"=>"","noDuplicates"=>"",
  208. "size"=>"","type"=>"","postCustomFieldName"=>"","displayAllCategories"=>"","displayCaption"=>"","displayDescription"=>"",
  209. "displayTitle"=>"","inputType"=>"","rangeMin"=>"","rangeMax"=>"","calendarIconType"=>"",
  210. "calendarIconUrl"=>"", "dateType"=>"","dateFormat"=>"","phoneFormat"=>"","addressType"=>"","defaultCountry"=>"","defaultProvince"=>"",
  211. "defaultState"=>"","hideAddress2"=>"","hideCountry"=>"","hideState"=>"","inputs"=>"","nameFormat"=>"","allowedExtensions"=>"",
  212. "captchaType"=>"","page_number"=>"","captchaTheme"=>"","simpleCaptchaSize"=>"","simpleCaptchaFontColor"=>"","simpleCaptchaBackgroundColor"=>"",
  213. "failed_validation"=>"", "productField" => "", "enablePasswordInput" => "", "maxLength" => "", "enablePrice" => "", "basePrice" => "");
  214. foreach($form["fields"] as &$field)
  215. $field = wp_parse_args($field, $all_fields);
  216. }
  217. return $form;
  218. }
  219. public static function get_grid_column_meta($form_id){
  220. global $wpdb;
  221. $table_name = self::get_meta_table_name();
  222. return maybe_unserialize($wpdb->get_var($wpdb->prepare("SELECT entries_grid_meta FROM $table_name WHERE form_id=%d", $form_id)));
  223. }
  224. public static function update_grid_column_meta($form_id, $columns){
  225. global $wpdb;
  226. $table_name = self::get_meta_table_name();
  227. $meta = maybe_serialize(stripslashes_deep($columns) );
  228. $wpdb->query( $wpdb->prepare("UPDATE $table_name SET entries_grid_meta=%s WHERE form_id=%d", $meta, $form_id) );
  229. }
  230. public static function get_lead_detail_id($current_fields, $field_number){
  231. foreach($current_fields as $field)
  232. if($field->field_number == $field_number)
  233. return $field->id;
  234. return 0;
  235. }
  236. public static function update_form_active($form_id, $is_active){
  237. global $wpdb;
  238. $form_table = self::get_form_table_name();
  239. $sql = $wpdb->prepare("UPDATE $form_table SET is_active=%d WHERE id=%d", $is_active, $form_id);
  240. $wpdb->query($sql);
  241. }
  242. public static function update_forms_active($forms, $is_active){
  243. foreach($forms as $form_id)
  244. self::update_form_active($form_id, $is_active);
  245. }
  246. public static function update_leads_property($leads, $property_name, $property_value){
  247. foreach($leads as $lead)
  248. self::update_lead_property($lead, $property_name, $property_value);
  249. }
  250. public static function update_lead_property($lead_id, $property_name, $property_value, $update_akismet=true, $disable_hook=false){
  251. global $wpdb;
  252. $lead_table = self::get_lead_table_name();
  253. $lead = self::get_lead($lead_id);
  254. //marking entry as "spam" or "not spam" with Akismet if the plugin is installed
  255. if($update_akismet && GFCommon::akismet_enabled($lead["form_id"]) && $property_name == "status" && in_array($property_value, array("active", "spam"))){
  256. $current_status = $lead["status"];
  257. if($current_status == "spam" && $property_value == "active"){
  258. $form = self::get_form_meta($lead["form_id"]);
  259. GFCommon::mark_akismet_spam($form, $lead, false);
  260. }
  261. else if($current_status == "active" && $property_value == "spam"){
  262. $form = self::get_form_meta($lead["form_id"]);
  263. GFCommon::mark_akismet_spam($form, $lead, true);
  264. }
  265. }
  266. //updating lead
  267. $wpdb->update($lead_table, array($property_name => $property_value ), array("id" => $lead_id));
  268. if(!$disable_hook){
  269. $previous_value = rgar($lead, $property_name);
  270. if($previous_value != $property_value) {
  271. // if property is status, prev value is spam and new value is active
  272. if($property_name == 'status' && $previous_value == 'spam' && $property_value == 'active' && !rgar($lead, 'post_id')) {
  273. $lead[$property_name] = $property_value;
  274. $lead['post_id'] = GFCommon::create_post($form, $lead);
  275. }
  276. do_action("gform_update_{$property_name}", $lead_id, $property_value, $previous_value);
  277. }
  278. }
  279. }
  280. public static function update_lead($lead){
  281. global $wpdb;
  282. $lead_table = self::get_lead_table_name();
  283. $payment_date = strtotime(rgar($lead,"payment_date")) ? "'{$lead["payment_date"]}'" : "NULL";
  284. $payment_amount = !rgempty("payment_amount", $lead) ? $lead["payment_amount"] : "NULL";
  285. $transaction_type = !rgempty("transaction_type", $lead) ? $lead["transaction_type"] : "NULL";
  286. $status = !rgempty("status", $lead) ? $lead["status"] : "active";
  287. $sql = $wpdb->prepare("UPDATE $lead_table SET
  288. form_id=%d,
  289. post_id=%d,
  290. is_starred=%d,
  291. is_read=%d,
  292. ip=%s,
  293. source_url=%s,
  294. user_agent=%s,
  295. currency=%s,
  296. payment_status=%s,
  297. payment_date={$payment_date},
  298. payment_amount={$payment_amount},
  299. transaction_id=%s,
  300. is_fulfilled=%d,
  301. transaction_type={$transaction_type},
  302. status='{$status}'
  303. WHERE id=%d", rgar($lead,"form_id"), rgar($lead,"post_id"), rgar($lead,"is_starred"), rgar($lead,"is_read"), rgar($lead,"ip"), rgar($lead,"source_url"), rgar($lead,"user_agent"),
  304. rgar($lead,"currency"), rgar($lead,"payment_status"), rgar($lead,"transaction_id"), rgar($lead,"is_fulfilled"), rgar($lead,"id"));
  305. $wpdb->query($sql);
  306. }
  307. public static function delete_leads($leads){
  308. foreach($leads as $lead_id)
  309. self::delete_lead($lead_id);
  310. }
  311. public static function delete_forms($forms){
  312. foreach($forms as $form_id)
  313. self::delete_form($form_id);
  314. }
  315. public static function delete_leads_by_form($form_id, $status=""){
  316. global $wpdb;
  317. if(!GFCommon::current_user_can_any("gravityforms_delete_entries"))
  318. die(__("You don't have adequate permission to delete entries.", "gravityforms"));
  319. $lead_table = self::get_lead_table_name();
  320. $lead_notes_table = self::get_lead_notes_table_name();
  321. $lead_detail_table = self::get_lead_details_table_name();
  322. $lead_detail_long_table = self::get_lead_details_long_table_name();
  323. //deleting uploaded files
  324. self::delete_files_by_form($form_id, $status);
  325. $status_filter = empty($status) ? "" : $wpdb->prepare("AND status=%s", $status);
  326. //Delete from detail long
  327. $sql = $wpdb->prepare(" DELETE FROM $lead_detail_long_table
  328. WHERE lead_detail_id IN(
  329. SELECT ld.id FROM $lead_detail_table ld
  330. INNER JOIN $lead_table l ON l.id = ld.lead_id
  331. WHERE l.form_id=%d AND ld.form_id=%d {$status_filter}
  332. )", $form_id, $form_id);
  333. $wpdb->query($sql);
  334. //Delete from lead details
  335. $sql = $wpdb->prepare(" DELETE FROM $lead_detail_table
  336. WHERE lead_id IN (
  337. SELECT id FROM $lead_table WHERE form_id=%d {$status_filter}
  338. )", $form_id);
  339. $wpdb->query($sql);
  340. //Delete from lead notes
  341. $sql = $wpdb->prepare(" DELETE FROM $lead_notes_table
  342. WHERE lead_id IN (
  343. SELECT id FROM $lead_table WHERE form_id=%d {$status_filter}
  344. )", $form_id);
  345. $wpdb->query($sql);
  346. //Delete from lead
  347. $sql = $wpdb->prepare("DELETE FROM $lead_table WHERE form_id=%d {$status_filter}", $form_id);
  348. $wpdb->query($sql);
  349. }
  350. public static function delete_views($form_id){
  351. global $wpdb;
  352. $form_view_table = self::get_form_view_table_name();
  353. //Delete form view
  354. $sql = $wpdb->prepare("DELETE FROM $form_view_table WHERE form_id=%d", $form_id);
  355. $wpdb->query($sql);
  356. }
  357. public static function delete_form($form_id){
  358. global $wpdb;
  359. if(!GFCommon::current_user_can_any("gravityforms_delete_forms"))
  360. die(__("You don't have adequate permission to delete forms.", "gravityforms"));
  361. do_action("gform_before_delete_form", $form_id);
  362. $form_meta_table = self::get_meta_table_name();
  363. $form_table = self::get_form_table_name();
  364. //Deleting form Entries
  365. self::delete_leads_by_form($form_id);
  366. //Delete form meta
  367. $sql = $wpdb->prepare("DELETE FROM $form_meta_table WHERE form_id=%d", $form_id);
  368. $wpdb->query($sql);
  369. //Deleting form Views
  370. self::delete_views($form_id);
  371. //Delete form
  372. $sql = $wpdb->prepare("DELETE FROM $form_table WHERE id=%d", $form_id);
  373. $wpdb->query($sql);
  374. do_action("gform_after_delete_form", $form_id);
  375. }
  376. public static function duplicate_form($form_id){
  377. global $wpdb;
  378. if(!GFCommon::current_user_can_any("gravityforms_create_form"))
  379. die(__("You don't have adequate permission to create forms.", "gravityforms"));
  380. //finding unique title
  381. $form = self::get_form($form_id);
  382. $count = 2;
  383. $title = $form->title . " - Copy 1";
  384. while(!self::is_unique_title($title)){
  385. $title = $form->title . " - Copy $count";
  386. $count++;
  387. }
  388. //creating new form
  389. $new_id = self::insert_form($title);
  390. //copying form meta
  391. $meta = self::get_form_meta($form_id);
  392. $meta["title"] = $title;
  393. $meta["id"] = $new_id;
  394. self::update_form_meta($new_id, $meta);
  395. return $new_id;
  396. }
  397. public static function is_unique_title($title){
  398. $forms = self::get_forms();
  399. foreach($forms as $form){
  400. if(strtolower($form->title) == strtolower($title))
  401. return false;
  402. }
  403. return true;
  404. }
  405. public static function insert_form($form_title){
  406. global $wpdb;
  407. $form_table_name = $wpdb->prefix . "rg_form";
  408. //creating new form
  409. $wpdb->query($wpdb->prepare("INSERT INTO $form_table_name(title, date_created) VALUES(%s, utc_timestamp())", $form_title));
  410. //returning newly created form id
  411. return $wpdb->insert_id;
  412. }
  413. public static function update_form_meta($form_id, $form_meta){
  414. global $wpdb;
  415. $meta_table_name = self::get_meta_table_name();
  416. $form_meta = maybe_serialize($form_meta);
  417. if(intval($wpdb->get_var($wpdb->prepare("SELECT count(0) FROM $meta_table_name WHERE form_id=%d", $form_id))) > 0)
  418. $wpdb->query( $wpdb->prepare("UPDATE $meta_table_name SET display_meta=%s WHERE form_id=%d", $form_meta, $form_id) );
  419. else
  420. $wpdb->query( $wpdb->prepare("INSERT INTO $meta_table_name(form_id, display_meta) VALUES(%d, %s)", $form_id, $form_meta ) );
  421. }
  422. public static function delete_files($lead_id, $form=null){
  423. $lead = self::get_lead($lead_id);
  424. if($form == null)
  425. $form = self::get_form_meta($lead["form_id"]);
  426. $fields = GFCommon::get_fields_by_type($form, array("fileupload", "post_image"));
  427. if(is_array($fields)){
  428. foreach($fields as $field){
  429. $value = self::get_lead_field_value($lead, $field);
  430. self::delete_physical_file($value);
  431. }
  432. }
  433. }
  434. public static function delete_files_by_form($form_id, $status=""){
  435. global $wpdb;
  436. $form = self::get_form_meta($form_id);
  437. $fields = GFCommon::get_fields_by_type($form, array("fileupload", "post_image"));
  438. if(empty($fields))
  439. return;
  440. $status_filter = empty($status) ? "" : $wpdb->prepare("AND status=%s", $status);
  441. $results = $wpdb->get_results($wpdb->prepare("SELECT id FROM {$wpdb->prefix}rg_lead WHERE form_id=%d {$status_filter}", $form_id), ARRAY_A);
  442. foreach($results as $result){
  443. self::delete_files($result["id"], $form);
  444. }
  445. }
  446. public static function delete_file($lead_id, $field_id){
  447. global $wpdb;
  448. if($lead_id == 0 || $field_id == 0)
  449. return;
  450. $lead_detail_table = self::get_lead_details_table_name();
  451. //Deleting file
  452. $sql = $wpdb->prepare("SELECT value FROM $lead_detail_table WHERE lead_id=%d AND field_number BETWEEN %f AND %f", $lead_id, $field_id - 0.001, $field_id + 0.001);
  453. $file_path = $wpdb->get_var($sql);
  454. self::delete_physical_file($file_path);
  455. //Delete from lead details
  456. $sql = $wpdb->prepare("DELETE FROM $lead_detail_table WHERE lead_id=%d AND field_number BETWEEN %f AND %f", $lead_id, $field_id - 0.001, $field_id + 0.001);
  457. $wpdb->query($sql);
  458. }
  459. private static function delete_physical_file($file_url){
  460. $ary = explode("|:|", $file_url);
  461. $url = rgar($ary,0);
  462. if(empty($url))
  463. return;
  464. //Convert from url to physical path
  465. if (is_multisite()) {
  466. $file_path = preg_replace("|^(.*?)/files/gravity_forms/|", BLOGUPLOADDIR . "gravity_forms/", $url);
  467. }
  468. else {
  469. $file_path = str_replace(WP_CONTENT_URL, WP_CONTENT_DIR, $url);
  470. }
  471. if(file_exists($file_path)){
  472. unlink($file_path);
  473. }
  474. }
  475. public static function delete_field($form_id, $field_id){
  476. global $wpdb;
  477. if($form_id == 0)
  478. return;
  479. do_action("gform_before_delete_field", $form_id, $field_id);
  480. $lead_table = self::get_lead_table_name();
  481. $lead_detail_table = self::get_lead_details_table_name();
  482. $lead_detail_long_table = self::get_lead_details_long_table_name();
  483. $form = self::get_form_meta($form_id);
  484. $field_type = "";
  485. //Deleting field from form meta
  486. $count = sizeof($form["fields"]);
  487. for($i = $count-1; $i >= 0; $i--){
  488. $field = $form["fields"][$i];
  489. //Deleting associated conditional logic rules
  490. if(!empty($field["conditionalLogic"])){
  491. $rule_count = sizeof($field["conditionalLogic"]["rules"]);
  492. for($j = $rule_count-1; $j >= 0; $j--){
  493. if($field["conditionalLogic"]["rules"][$j]["fieldId"] == $field_id){
  494. unset($form["fields"][$i]["conditionalLogic"]["rules"][$j]);
  495. }
  496. }
  497. $form["fields"][$i]["conditionalLogic"]["rules"] = array_values($form["fields"][$i]["conditionalLogic"]["rules"]);
  498. //If there aren't any rules, remove the conditional logic
  499. if(sizeof($form["fields"][$i]["conditionalLogic"]["rules"]) == 0){
  500. $form["fields"][$i]["conditionalLogic"] = false;
  501. }
  502. }
  503. //Deleting field from form meta
  504. if($field["id"] == $field_id){
  505. $field_type = $field["type"];
  506. unset($form["fields"][$i]);
  507. }
  508. }
  509. //removing post content and title template if the field being deleted is a post content field or post title field
  510. if($field_type == "post_content"){
  511. $form["postContentTemplateEnabled"] = false;
  512. $form["postContentTemplate"] = "";
  513. }
  514. else if($field_type == "post_title"){
  515. $form["postTitleTemplateEnabled"] = false;
  516. $form["postTitleTemplate"] = "";
  517. }
  518. //Deleting associated routing rules
  519. if(!empty($form["notification"]["routing"])){
  520. $routing_count = sizeof($form["notification"]["routing"]);
  521. for($j = $routing_count-1; $j >= 0; $j--){
  522. if(intval($form["notification"]["routing"][$j]["fieldId"]) == $field_id){
  523. unset($form["notification"]["routing"][$j]);
  524. }
  525. }
  526. $form["notification"]["routing"] = array_values($form["notification"]["routing"]);
  527. //If there aren't any routing, remove it
  528. if(sizeof($form["notification"]["routing"]) == 0){
  529. $form["notification"]["routing"] = null;
  530. }
  531. }
  532. $form["fields"] = array_values($form["fields"]);
  533. self::update_form_meta($form_id, $form);
  534. //Delete from grid column meta
  535. $columns = self::get_grid_column_meta($form_id);
  536. $count = sizeof($columns);
  537. for($i = $count -1; $i >=0; $i--)
  538. {
  539. if(intval(rgar($columns,$i)) == intval($field_id)){
  540. unset($columns[$i]);
  541. }
  542. }
  543. self::update_grid_column_meta($form_id, $columns);
  544. //Delete from detail long
  545. $sql = $wpdb->prepare(" DELETE FROM $lead_detail_long_table
  546. WHERE lead_detail_id IN(
  547. SELECT id FROM $lead_detail_table WHERE form_id=%d AND field_number >= %d AND field_number < %d
  548. )", $form_id, $field_id, $field_id + 1);
  549. $wpdb->query($sql);
  550. //Delete from lead details
  551. $sql = $wpdb->prepare("DELETE FROM $lead_detail_table WHERE form_id=%d AND field_number >= %d AND field_number < %d", $form_id, $field_id, $field_id + 1);
  552. $wpdb->query($sql);
  553. //Delete leads with no details
  554. $sql = $wpdb->prepare(" DELETE FROM $lead_table
  555. WHERE form_id=%d
  556. AND id NOT IN(
  557. SELECT DISTINCT(lead_id) FROM $lead_detail_table WHERE form_id=%d
  558. )", $form_id, $form_id);
  559. $wpdb->query($sql);
  560. do_action("gform_after_delete_field", $form_id, $field_id);
  561. }
  562. public static function delete_lead($lead_id){
  563. global $wpdb;
  564. if(!GFCommon::current_user_can_any("gravityforms_delete_entries"))
  565. die(__("You don't have adequate permission to delete entries.", "gravityforms"));
  566. do_action("gform_delete_lead", $lead_id);
  567. $lead_table = self::get_lead_table_name();
  568. $lead_notes_table = self::get_lead_notes_table_name();
  569. $lead_detail_table = self::get_lead_details_table_name();
  570. $lead_detail_long_table = self::get_lead_details_long_table_name();
  571. //deleting uploaded files
  572. self::delete_files($lead_id);
  573. //Delete from detail long
  574. $sql = $wpdb->prepare(" DELETE FROM $lead_detail_long_table
  575. WHERE lead_detail_id IN(
  576. SELECT id FROM $lead_detail_table WHERE lead_id=%d
  577. )", $lead_id);
  578. $wpdb->query($sql);
  579. //Delete from lead details
  580. $sql = $wpdb->prepare("DELETE FROM $lead_detail_table WHERE lead_id=%d", $lead_id);
  581. $wpdb->query($sql);
  582. //Delete from lead notes
  583. $sql = $wpdb->prepare("DELETE FROM $lead_notes_table WHERE lead_id=%d", $lead_id);
  584. $wpdb->query($sql);
  585. //Delete from lead meta
  586. gform_delete_meta($lead_id);
  587. //Delete from lead
  588. $sql = $wpdb->prepare("DELETE FROM $lead_table WHERE id=%d", $lead_id);
  589. $wpdb->query($sql);
  590. }
  591. public static function add_note($lead_id, $user_id, $user_name, $note){
  592. global $wpdb;
  593. $table_name = self::get_lead_notes_table_name();
  594. $sql = $wpdb->prepare("INSERT INTO $table_name(lead_id, user_id, user_name, value, date_created) values(%d, %d, %s, %s, utc_timestamp())", $lead_id, $user_id, $user_name, $note);
  595. $wpdb->query($sql);
  596. }
  597. public static function delete_note($note_id){
  598. global $wpdb;
  599. if(!GFCommon::current_user_can_any("gravityforms_edit_entry_notes"))
  600. die(__("You don't have adequate permission to delete notes.", "gravityforms"));
  601. $table_name = self::get_lead_notes_table_name();
  602. $sql = $wpdb->prepare("DELETE FROM $table_name WHERE id=%d", $note_id);
  603. $wpdb->query($sql);
  604. }
  605. public static function delete_notes($notes){
  606. if(!is_array($notes))
  607. return;
  608. foreach($notes as $note_id){
  609. self::delete_note($note_id);
  610. }
  611. }
  612. public static function get_ip(){
  613. $ip = rgget("HTTP_X_FORWARDED_FOR", $_SERVER);
  614. if (!$ip)
  615. $ip = rgget("REMOTE_ADDR", $_SERVER);
  616. $ip_array = explode(",", $ip); //HTTP_X_FORWARDED_FOR can return a comma separated list of IPs. Using the first one.
  617. return $ip_array[0];
  618. }
  619. public static function save_lead($form, &$lead){
  620. global $wpdb;
  621. if(IS_ADMIN && !GFCommon::current_user_can_any("gravityforms_edit_entries"))
  622. die(__("You don't have adequate permission to edit entries.", "gravityforms"));
  623. $lead_detail_table = self::get_lead_details_table_name();
  624. //Inserting lead if null
  625. if($lead == null){
  626. global $current_user;
  627. $user_id = $current_user && $current_user->ID ? $current_user->ID : 'NULL';
  628. $lead_table = RGFormsModel::get_lead_table_name();
  629. $user_agent = strlen($_SERVER["HTTP_USER_AGENT"]) > 250 ? substr($_SERVER["HTTP_USER_AGENT"], 0, 250) : $_SERVER["HTTP_USER_AGENT"];
  630. $currency = GFCommon::get_currency();
  631. $wpdb->query($wpdb->prepare("INSERT INTO $lead_table(form_id, ip, source_url, date_created, user_agent, currency, created_by) VALUES(%d, %s, %s, utc_timestamp(), %s, %s, {$user_id})", $form["id"], self::get_ip(), self::get_current_page_url(), $user_agent, $currency));
  632. //reading newly created lead id
  633. $lead_id = $wpdb->insert_id;
  634. $lead = array("id" => $lead_id);
  635. }
  636. $current_fields = $wpdb->get_results($wpdb->prepare("SELECT id, field_number FROM $lead_detail_table WHERE lead_id=%d", $lead["id"]));
  637. $original_post_id = rgget("post_id", $lead);
  638. $total_field = null;
  639. $calculation_fields = array();
  640. $recalculate_total = false;
  641. foreach($form["fields"] as $field){
  642. //Ignore fields that are marked as display only
  643. if(rgget("displayOnly", $field) && $field["type"] != "password"){
  644. continue;
  645. }
  646. //ignore pricing fields in the entry detail
  647. if(RG_CURRENT_VIEW == "entry" && GFCommon::is_pricing_field($field["type"])){
  648. continue;
  649. }
  650. //process total field after all fields have been saved
  651. if($field["type"] == "total"){
  652. $total_field = $field;
  653. continue;
  654. }
  655. // process calculation fields after all fields have been saved
  656. if(GFCommon::has_field_calculation($field)) {
  657. $calculation_fields[] = $field;
  658. continue;
  659. }
  660. //only save fields that are not hidden (except on entry screen)
  661. if(RG_CURRENT_VIEW == "entry" || !RGFormsModel::is_field_hidden($form, $field, array()) ){
  662. if($field['type'] == 'post_category')
  663. $field = GFCommon::add_categories_as_choices($field, '');
  664. if(isset($field["inputs"]) && is_array($field["inputs"])){
  665. foreach($field["inputs"] as $input)
  666. self::save_input($form, $field, $lead, $current_fields, $input["id"]);
  667. }
  668. else{
  669. self::save_input($form, $field, $lead, $current_fields, $field["id"]);
  670. }
  671. }
  672. }
  673. if(!empty($calculation_fields)) {
  674. foreach($calculation_fields as $calculation_field) {
  675. if(isset($calculation_field["inputs"]) && is_array($calculation_field["inputs"])){
  676. foreach($calculation_field["inputs"] as $input) {
  677. self::save_input($form, $calculation_field, $lead, $current_fields, $input["id"]);
  678. }
  679. }
  680. else{
  681. self::save_input($form, $calculation_field, $lead, $current_fields, $calculation_field["id"]);
  682. }
  683. }
  684. self::refresh_product_cache($form, $lead = RGFormsModel::get_lead($lead['id']));
  685. }
  686. //saving total field as the last field of the form.
  687. if($total_field) {
  688. self::save_input($form, $total_field, $lead, $current_fields, $total_field["id"]);
  689. }
  690. }
  691. public static function refresh_product_cache($form, $lead, $use_choice_text = false, $use_admin_label = false) {
  692. $cache_options = array(
  693. array(false, false),
  694. array(false, true),
  695. array(true, false),
  696. array(true, true)
  697. );
  698. foreach($cache_options as $cache_option) {
  699. list($use_choice_text, $use_admin_label) = $cache_option;
  700. if( gform_get_meta( rgar($lead,'id'), "gform_product_info_{$use_choice_text}_{$use_admin_label}") ) {
  701. gform_delete_meta(rgar($lead,'id'), "gform_product_info_{$use_choice_text}_{$use_admin_label}");
  702. GFCommon::get_product_fields($form, $lead, $use_choice_text, $use_admin_label);
  703. }
  704. }
  705. }
  706. public static function is_field_hidden($form, $field, $field_values, $lead=null){
  707. $section = self::get_section($form, $field["id"]);
  708. $section_display = self::get_field_display($form, $section, $field_values, $lead);
  709. //if section is hidden, hide field no matter what. if section is visible, see if field is supposed to be visible
  710. if($section_display == "hide")
  711. return true;
  712. else if(self::is_page_hidden($form, rgar($field,"page_number"), $field_values, $lead)){
  713. return true;
  714. }
  715. else{
  716. $display = self::get_field_display($form, $field, $field_values, $lead);
  717. return $display == "hide";
  718. }
  719. }
  720. public static function is_page_hidden($form, $page_number, $field_values, $lead=null){
  721. $page = self::get_page_by_number($form, $page_number);
  722. if(!$page)
  723. return false;
  724. $display = self::get_field_display($form, $page, $field_values, $lead);
  725. return $display == "hide";
  726. }
  727. public static function get_page_by_number($form, $page_number){
  728. foreach($form["fields"] as $field){
  729. if($field["type"] == "page" && $field["pageNumber"] == $page_number)
  730. return $field;
  731. }
  732. return null;
  733. }
  734. public static function get_page_by_field($form, $field){
  735. return get_page_by_number($field["page_number"]);
  736. }
  737. //gets the section that the specified field belongs to, or null if none
  738. public static function get_section($form, $field_id){
  739. $current_section = null;
  740. foreach($form["fields"] as $field){
  741. if($field["type"] == "section")
  742. $current_section = $field;
  743. //stop section at a page break (sections don't go cross page)
  744. if($field["type"] == "page")
  745. $current_section = null;
  746. if($field["id"] == $field_id)
  747. return $current_section;
  748. }
  749. return null;
  750. }
  751. public static function is_value_match($field_value, $target_value, $operation="is", $source_field=null){
  752. if($source_field && $source_field["type"] == "post_category"){
  753. $field_value = GFCommon::prepare_post_category_value($field_value, $source_field, "conditional_logic");
  754. }
  755. if(is_array($field_value)){
  756. foreach($field_value as $val){
  757. if(self::matches_operation(GFCommon::get_selection_value($val), $target_value, $operation))
  758. return true;
  759. }
  760. }
  761. else if(self::matches_operation(GFCommon::get_selection_value($field_value), $target_value, $operation)){
  762. return true;
  763. }
  764. return false;
  765. }
  766. private static function try_convert_float($text){
  767. global $wp_locale;
  768. $number_format = $wp_locale->number_format['decimal_point'] == "," ? "decimal_comma" : "decimal_dot";
  769. if(GFCommon::is_numeric($text, $number_format))
  770. return GFCommon::clean_number($text, $number_format);
  771. return $text;
  772. }
  773. public static function matches_operation($val1, $val2, $operation){
  774. $val1 = !empty($val1) ? strtolower($val1) : "";
  775. $val2 = !empty($val2) ? strtolower($val2) : "";
  776. switch($operation){
  777. case "is" :
  778. return $val1 == $val2;
  779. break;
  780. case "isnot" :
  781. return $val1 != $val2;
  782. break;
  783. case "greater_than":
  784. case ">" :
  785. $val1 = self::try_convert_float($val1);
  786. $val2 = self::try_convert_float($val2);
  787. return $val1 > $val2;
  788. break;
  789. case "less_than":
  790. case "<" :
  791. $val1 = self::try_convert_float($val1);
  792. $val2 = self::try_convert_float($val2);
  793. return $val1 < $val2;
  794. break;
  795. case "contains" :
  796. return !empty($val2) && strpos($val1, $val2) !== false;
  797. break;
  798. case "starts_with" :
  799. return !empty($val2) && strpos($val1, $val2) === 0;
  800. break;
  801. case "ends_with" :
  802. $start = strlen($val1) - strlen($val2);
  803. if($start < 0)
  804. return false;
  805. $tail = substr($val1, $start);
  806. return $val2 == $tail;
  807. break;
  808. }
  809. return false;
  810. }
  811. private static function get_field_display($form, $field, $field_values, $lead=null){
  812. $logic = RGForms::get("conditionalLogic", $field);
  813. //if this field does not have any conditional logic associated with it, it won't be hidden
  814. if(empty($logic))
  815. return "show";
  816. $match_count = 0;
  817. foreach($logic["rules"] as $rule){
  818. $source_field = RGFormsModel::get_field($form, $rule["fieldId"]);
  819. $field_value = empty($lead) ? self::get_field_value($source_field, $field_values) : self::get_lead_field_value($lead, $source_field);
  820. $is_value_match = self::is_value_match($field_value, $rule["value"], $rule["operator"], $source_field);
  821. if($is_value_match)
  822. $match_count++;
  823. }
  824. $do_action = ($logic["logicType"] == "all" && $match_count == sizeof($logic["rules"]) ) || ($logic["logicType"] == "any" && $match_count > 0);
  825. $is_hidden = ($do_action && $logic["actionType"] == "hide") || (!$do_action && $logic["actionType"] == "show");
  826. return $is_hidden ? "hide" : "show";
  827. }
  828. public static function get_custom_choices(){
  829. $choices = get_option("gform_custom_choices");
  830. if(!$choices)
  831. $choices = array();
  832. return $choices;
  833. }
  834. public static function delete_custom_choice($name){
  835. $choices = self::get_custom_choices();
  836. if(array_key_exists($name, $choices))
  837. unset($choices[$name]);
  838. update_option("gform_custom_choices", $choices);
  839. }
  840. public static function save_custom_choice($previous_name, $new_name, $choices){
  841. $all_choices = self::get_custom_choices();
  842. if(array_key_exists($previous_name, $all_choices))
  843. unset($all_choices[$previous_name]);
  844. $all_choices[$new_name] = $choices;
  845. update_option("gform_custom_choices", $all_choices);
  846. }
  847. public static function get_field_value($field, $field_values = array(), $get_from_post=true){
  848. if($field['type'] == 'post_category')
  849. $field = GFCommon::add_categories_as_choices($field, '');
  850. $value = array();
  851. switch(RGFormsModel::get_input_type($field)){
  852. case "post_image" :
  853. $value[$field["id"] . ".1"] = self::get_input_value($field, "input_" . $field["id"] . "_1", $get_from_post);
  854. $value[$field["id"] . ".4"] = self::get_input_value($field, "input_" . $field["id"] . "_4", $get_from_post);
  855. $value[$field["id"] . ".7"] = self::get_input_value($field, "input_" . $field["id"] . "_7", $get_from_post);
  856. break;
  857. case "checkbox" :
  858. $parameter_values = self::get_parameter_value($field["inputName"], $field_values, $field);
  859. if(!empty($parameter_values) && !is_array($parameter_values)){
  860. $parameter_values = explode(",", $parameter_values);
  861. }
  862. if(!is_array($field["inputs"]))
  863. return "";
  864. $choice_index = 0;
  865. foreach($field["inputs"] as $input){
  866. if(!empty($_POST["is_submit_" . $field["formId"]]) && $get_from_post){
  867. $value[strval($input["id"])] = rgpost("input_" . str_replace('.', '_', strval($input["id"])));
  868. }
  869. else{
  870. if(is_array($parameter_values)){
  871. foreach($parameter_values as $item){
  872. $item = trim($item);
  873. if(self::choice_value_match($field, $field["choices"][$choice_index], $item))
  874. {
  875. $value[$input["id"] . ""] = $item;
  876. break;
  877. }
  878. }
  879. }
  880. }
  881. $choice_index++;
  882. }
  883. break;
  884. case "list" :
  885. $value = self::get_input_value($field, "input_" . $field["id"], rgar($field, "inputName"), $field_values, $get_from_post);
  886. $value = self::create_list_array($field, $value);
  887. break;
  888. default:
  889. if(isset($field["inputs"]) && is_array($field["inputs"])){
  890. foreach($field["inputs"] as $input){
  891. $value[strval($input["id"])] = self::get_input_value($field, "input_" . str_replace('.', '_', strval($input["id"])), RGForms::get("name", $input), $field_values, $get_from_post);
  892. }
  893. }
  894. else{
  895. $value = self::get_input_value($field, "input_" . $field["id"], rgar($field, "inputName"), $field_values, $get_from_post);
  896. }
  897. break;
  898. }
  899. return $value;
  900. }
  901. private static function get_input_value($field, $standard_name, $custom_name = "", $field_values=array(), $get_from_post=true){
  902. if(!empty($_POST["is_submit_" . rgar($field,"formId")]) && $get_from_post){
  903. return rgpost($standard_name);
  904. }
  905. else if(rgar($field, "allowsPrepopulate")){
  906. return self::get_parameter_value($custom_name, $field_values, $field);
  907. }
  908. }
  909. private static function get_parameter_value($name, $field_values, $field){
  910. $value = stripslashes(rgget($name));
  911. if(empty($value))
  912. $value = rgget($name, $field_values);
  913. //converting list format
  914. if(RGFormsModel::get_input_type($field) == "list"){
  915. //transforms this: col1|col2,col1b|col2b into this: col1,col2,col1b,col2b
  916. $column_count = count(rgar($field,"choices"));
  917. $rows = explode(",", $value);
  918. $ary_rows = array();
  919. if(!empty($rows)){
  920. foreach($rows as $row)
  921. $ary_rows = array_merge($ary_rows, rgexplode("|", $row, $column_count));
  922. $value = $ary_rows;
  923. }
  924. }
  925. return apply_filters("gform_field_value_$name", $value);
  926. }
  927. public static function get_default_value($field, $input_id){
  928. if(!is_array(rgar($field,"choices"))){
  929. if(is_array(rgar($field, "inputs"))){
  930. $input = RGFormsModel::get_input($field, $input_id);
  931. return rgar($input, "defaultValue");
  932. }
  933. else{
  934. return IS_ADMIN ? $field["defaultValue"] : GFCommon::replace_variables_prepopulate($field["defaultValue"]);
  935. }
  936. }
  937. else if($field["type"] == "checkbox"){
  938. for($i=0, $count=sizeof($field["inputs"]); $i<$count; $i++){
  939. $input = $field["inputs"][$i];
  940. $choice = $field["choices"][$i];
  941. if($input["id"] == $input_id && $choice["isSelected"]){
  942. return $choice["value"];
  943. }
  944. }
  945. return "";
  946. }
  947. else{
  948. foreach($field["choices"] as $choice){
  949. if($choice["isSelected"] || $field["type"] == "post_category")
  950. return $choice["value"];
  951. }
  952. return "";
  953. }
  954. }
  955. public static function get_input_type($field){
  956. return empty($field["inputType"]) ? rgar($field,"type") : $field["inputType"];
  957. }
  958. private static function get_post_field_value($field, $lead){
  959. if(is_array($field["inputs"])){
  960. $value = array();
  961. foreach($field["inputs"] as $input){
  962. $val = isset($lead[strval($input["id"])]) ? $lead[strval($input["id"])] : "";
  963. if(!empty($val))
  964. $value[] = $val;
  965. }
  966. $value = implode(",", $value);
  967. }
  968. else{
  969. $value = isset($lead[$field["id"]]) ? $lead[$field["id"]] : "";
  970. }
  971. return $value;
  972. }
  973. private static function get_post_fields($form, $lead) {
  974. $post_data = array();
  975. $post_data["post_custom_fields"] = array();
  976. $post_data["tags_input"] = array();
  977. $categories = array();
  978. $images = array();
  979. foreach($form["fields"] as $field){
  980. if($field['type'] == 'post_category')
  981. $field = GFCommon::add_categories_as_choices($field, '');
  982. $value = self::get_post_field_value($field, $lead);
  983. switch($field["type"]){
  984. case "post_title" :
  985. case "post_excerpt" :
  986. case "post_content" :
  987. $post_data[$field["type"]] = $value;
  988. break;
  989. case "post_tags" :
  990. $tags = explode(",", $value);
  991. if(is_array($tags) && sizeof($tags) > 0)
  992. $post_data["tags_input"] = array_merge($post_data["tags_input"], $tags) ;
  993. break;
  994. case "post_custom_field" :
  995. $meta_name = $field["postCustomFieldName"];
  996. if(!isset($post_data["post_custom_fields"][$meta_name])){
  997. $post_data["post_custom_fields"][$meta_name] = $value;
  998. }
  999. else if(!is_array($post_data["post_custom_fields"][$meta_name])){
  1000. $post_data["post_custom_fields"][$meta_name] = array($post_data["post_custom_fields"][$meta_name], $value);
  1001. }
  1002. else{
  1003. $post_data["post_custom_fields"][$meta_name][] = $value;
  1004. }
  1005. break;
  1006. case "post_category" :
  1007. foreach(explode(',', $value) as $cat_string) {
  1008. list($cat_name, $cat_id) = rgexplode(":", $cat_string, 2);
  1009. array_push($categories, $cat_id);
  1010. }
  1011. break;
  1012. case "post_image" :
  1013. $ar

Large files files are truncated, but you can click here to view the full file