/gravity-forms-addons.php

  1. <?php 
  2. /** 
  3. Plugin Name: Gravity Forms Directory & Addons 
  4. Plugin URI: http://katz.co/gravity-forms-addons/ 
  5. Description: Turn <a href="http://katz.si/gravityforms">Gravity Forms</a> into a great WordPress directory...and more! 
  6. Author: Katz Web Services, Inc. 
  7. Version: 3.7 
  8. Author URI: http://www.katzwebservices.com 
  9. Text Domain: gravity-forms-addons 
  10. License: GPLv2 or later 
  11. License URI: http://www.gnu.org/licenses/gpl-2.0.html 
  12. Domain Path: /languages 
  13.   
  14. Copyright 2014 Katz Web Services, Inc. (email: info@katzwebservices.com) 
  15.   
  16. This program is free software; you can redistribute it and/or modify 
  17. it under the terms of the GNU General Public License as published by 
  18. the Free Software Foundation; either version 3 of the License, or 
  19. (at your option) any later version. 
  20.   
  21. This program is distributed in the hope that it will be useful,  
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of 
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  24. GNU General Public License for more details. 
  25.   
  26. You should have received a copy of the GNU General Public License 
  27. along with this program. If not, see <http://www.gnu.org/licenses/>. 
  28.   
  29. */ 
  30.  
  31. register_activation_hook( __FILE__, array('GFDirectory', 'activation') ); 
  32. add_action('plugins_loaded', array('GFDirectory', 'plugins_loaded')); 
  33. add_action('plugins_loaded', 'kws_gf_load_functions'); 
  34.  
  35. class GFDirectory { 
  36.  
  37. private static $path = "gravity-forms-addons/gravity-forms-addons.php"; 
  38. private static $slug = "gravity-forms-addons"; 
  39. private static $version = "3.7"; 
  40. private static $min_gravityforms_version = "1.5"; 
  41.  
  42. public static function directory_defaults($args = array()) { 
  43. $defaults = array( 
  44. 'form' => 1, // Gravity Forms form ID 
  45. 'approved' => false, // Show only entries that have been Approved (have a field in the form that is an Admin-only checkbox with a value of 'Approved' 
  46. 'smartapproval' => true, // Auto-convert form into Approved-only when an Approved field is detected. 
  47. 'directoryview' => 'table', // Table, list or DL 
  48. 'entryview' => 'table', // Table, list or DL 
  49. 'hovertitle' => true, // Show column name as user hovers over cell 
  50. 'tableclass' => 'gf_directory widefat', // Class for the <table> 
  51. 'tablestyle' => '', // inline CSS for the <table> 
  52. 'rowclass' => '', // Class for the <table> 
  53. 'rowstyle' => '', // inline CSS for all <tbody><tr>'s 
  54. 'valign' => '',  
  55. 'sort' => 'date_created', // Use the input ID ( example: 1.3 or 7 or ip ) 
  56. 'dir' => 'DESC',  
  57.  
  58. 'useredit' => false,  
  59. 'limituser' => false,  
  60. 'adminedit' => false,  
  61.  
  62. 'status' => 'active', // Added in 2.0 
  63. 'start_date' => '', // Added in 2.0 
  64. 'end_date' => '', // Added in 2.0 
  65.  
  66. //'wpautop' => true, // Convert bulk paragraph text to...paragraphs. Deprecated 3.6.3 
  67. 'page_size' => 20, // Number of entries to show at once 
  68. 'startpage' => 1, // If you want to show page 8 instead of 1 
  69.  
  70. 'lightboxstyle' => 3,  
  71. 'lightboxsettings' => array('images' => true, 'entry' => null, 'websites' => null),  
  72.  
  73. 'showcount' => true, // Do you want to show "Displaying 1-19 of 19"? 
  74. 'pagelinksshowall' => true, // Whether to show each page number, or just 7 
  75. 'next_text' => '»',  
  76. 'prev_text' => '«',  
  77. 'pagelinkstype' => 'plain', // 'plain' is just a string with the links separated by a newline character. The other possible values are either 'array' or 'list'. 
  78. //'fulltext' => true, // If there's a textarea or post content field, show the full content or a summary? Deprecated 3.6.3 
  79. 'linkemail' => true, // Convert email fields to email mailto: links 
  80. 'linkwebsite' => true, // Convert URLs to links 
  81. 'linknewwindow' => false, // Open links in new window? (uses target="_blank") 
  82. 'nofollowlinks' => false, // Add nofollow to all links, including emails 
  83. 'titleshow' => true, // Show a form title? By default, the title will be the form title. 
  84. 'titleprefix' => 'Entries for ', // Default GF behavior is 'Entries : ' 
  85. 'tablewidth' => '100%', // 'width' attribute for the table 
  86. 'searchtabindex' => false, // adds tabindex="" to the search field 
  87. 'search' => true, // show the search field 
  88. 'tfoot' => true, // show the <tfoot> 
  89. 'thead' => true, // show the <thead> 
  90. 'showadminonly' => false, // Admin only columns aren't shown by default, but can be (added 2.0.1) 
  91. 'datecreatedformat' => get_option('date_format').' \a\t '.get_option('time_format'), // Use standard PHP date formats (http://php.net/manual/en/function.date.php) 
  92. 'credit' => true, // Credit link 
  93. 'dateformat' => false, // Override the options from Gravity Forms, and use standard PHP date formats (http://php.net/manual/en/function.date.php) 
  94. 'postimage' => 'icon', // Whether to show icon, thumbnail, or large image 
  95. 'getimagesize' => false,  
  96. 'entry' => true, // If there's an Entry ID column, link to the full entry 
  97. 'entrylink' => 'View entry details',  
  98. 'entryth' => 'More Info',  
  99. 'entryback' => '← Back to directory',  
  100. 'entryonly' => true,  
  101. 'entrytitle' => 'Entry Detail',  
  102. 'entrydetailtitle' => '%%formtitle%% : Entry # %%leadid%%',  
  103. 'entryanchor' => true,  
  104. 'truncatelink' => false,  
  105. 'appendaddress' => false,  
  106. 'hideaddresspieces' => false,  
  107. 'jssearch' => true,  
  108. 'jstable' => false,  
  109. 'lightbox' => null, // depreciated - Combining with lightboxsettings 
  110. 'entrylightbox' => null, // depreciated - Combining with lightboxsettings 
  111. ); 
  112.  
  113. $settings = get_option("gf_addons_settings"); 
  114. if(isset($settings['directory_defaults'])) { 
  115. $defaults = wp_parse_args($settings['directory_defaults'], $defaults); 
  116.  
  117. $options = wp_parse_args($args, $defaults); 
  118.  
  119. // Backward Compatibility 
  120. if(!empty($args['lightbox'])) { $options['lightboxsettings']['images'] = 1; } 
  121. if(!empty($args['entrylightbox'])) { $options['lightboxsettings']['entry'] = 1; } 
  122. unset($options['lightbox'], $options['entrylightbox']); // Depreciated for lightboxsettings 
  123.  
  124. return apply_filters('kws_gf_directory_defaults', $options); 
  125.  
  126. public static function plugins_loaded() { 
  127.  
  128. if(!self::is_gravityforms_installed()) { return false; } 
  129.  
  130. include_once(plugin_dir_path( __FILE__ ) .'/edit-form.php'); 
  131. include_once(plugin_dir_path( __FILE__ ) .'/admin.php'); 
  132. include_once(plugin_dir_path( __FILE__ ) .'/gravity-forms-lead-creator.php'); 
  133.  
  134. if(in_array(RG_CURRENT_PAGE, array("gf_entries", "admin.php", "admin-ajax.php"))) { 
  135. self::globals_get_approved_column(); 
  136. if(self::is_gravity_page()) { 
  137. self::load_functionality(); 
  138.  
  139. add_action('init', array('GFDirectory', 'init')); 
  140. add_shortcode('directory', array('GFDirectory', 'make_directory')); 
  141.  
  142.  
  143. //Plugin starting point. Will load appropriate files 
  144. public static function init() { 
  145. global $current_user; 
  146.  
  147. self::add_rewrite(); 
  148.  
  149. if(!self::is_gravityforms_supported()) { 
  150. return; 
  151.  
  152. if(!is_admin()) { 
  153.  
  154. add_action('template_redirect', array('GFDirectory', 'enqueue_files')); 
  155. if(apply_filters('kws_gf_directory_canonical_add', true)) { 
  156. add_filter('post_link', array('GFDirectory', 'directory_canonical'), 1, 3); 
  157. add_filter('page_link', array('GFDirectory', 'directory_canonical'), 1, 3); 
  158. if(apply_filters('kws_gf_directory_shortlink', true)) { 
  159. add_filter('get_shortlink', array('GFDirectory', 'shortlink')); 
  160. add_filter('kws_gf_directory_lead_filter', array('GFDirectory', 'show_only_user_entries'), 10, 2); 
  161. add_filter('kws_gf_directory_anchor_text', array('GFDirectory', 'directory_anchor_text')); 
  162.  
  163. //integrating with Members plugin 
  164. if(function_exists('members_get_capabilities')) { 
  165. add_filter('members_get_capabilities', array("GFDirectory", "members_get_capabilities")); 
  166.  
  167. add_filter('kws_gf_directory_td_address', array('GFDirectory', 'format_address'), 1, 2); // Add this filter so it can be removed or overridden by users 
  168.  
  169. if(self::is_directory_page()) { 
  170.  
  171. //enqueueing sack for AJAX requests 
  172. wp_enqueue_script(array("sack", 'datepicker')); 
  173. wp_enqueue_style('gravityforms-admin', GFCommon::get_base_url().'/css/admin.css'); 
  174.  
  175. else if(self::is_gravity_page('gf_entries')) { 
  176. wp_enqueue_script( 'thickbox', array('jquery')); 
  177. add_filter("gform_get_field_value", array('GFDirectory', 'add_lead_approved_hidden_input'), 1, 3); 
  178. else if(in_array(RG_CURRENT_PAGE, array("admin-ajax.php"))) { 
  179. add_action('wp_ajax_rg_update_feed_active', array('GFDirectory', 'update_feed_active')); 
  180. add_action('wp_ajax_gf_select_directory_form', array('GFDirectory', 'select_directory_form')); 
  181. add_action('wp_ajax_rg_update_approved', array('GFDirectory', 'directory_update_approved_hook')); 
  182. add_action('wp_ajax_change_directory_columns', array('GFDirectory', 'change_directory_columns')); 
  183. } else if(in_array(RG_CURRENT_PAGE, array("plugins.php"))) { 
  184.  
  185. add_filter('plugin_action_links', array('GFDirectory', 'settings_link'), 10, 2 ); 
  186.  
  187.  
  188.  
  189. //Target of Member plugin filter. Provides the plugin with Gravity Forms lists of capabilities 
  190. public static function members_get_capabilities( $caps ) { 
  191. return array_merge($caps, array("gravityforms_directory", "gravityforms_directory_uninstall")); 
  192.  
  193. static public function activation() { 
  194. self::add_permissions(); 
  195. self::flush_rules(); 
  196.  
  197. public static function is_gravityforms_installed() { 
  198. return class_exists("RGForms"); 
  199.  
  200. public static function add_permissions() { 
  201. global $wp_roles; 
  202. $wp_roles->add_cap("administrator", "gravityforms_directory"); 
  203. $wp_roles->add_cap("administrator", "gravityforms_directory_uninstall"); 
  204.  
  205. static public function flush_rules() { 
  206. global $wp_rewrite; 
  207. self::add_rewrite(); 
  208. $wp_rewrite->flush_rules(); 
  209. return; 
  210.  
  211.  
  212.  
  213. static private function load_functionality() { 
  214.  
  215. register_deactivation_hook( __FILE__, array('GFDirectory', 'uninstall') ); 
  216.  
  217. $settings = self::get_settings(); 
  218. extract($settings); 
  219.  
  220. if($referrer) { 
  221. // Load Joost's referrer tracker 
  222. @include_once('gravity-forms-referrer.php'); 
  223.  
  224.  
  225. static public function shortlink($link = '') { 
  226. global $post; 
  227. if(empty($post)) { return; } 
  228. if(empty($link) && isset($post->guid)) { 
  229. $link = $post->guid; 
  230. return $link; 
  231.  
  232. $url = add_query_arg(array()); 
  233. if(preg_match('/'.sanitize_title(apply_filters('kws_gf_directory_endpoint', 'entry')).'\/([0-9]+)(?:\/|-)([0-9]+)\/?/ism', $url, $matches)) { 
  234. $link = add_query_arg(array('form'=>(int)$matches[1], 'leadid'=>(int)$matches[2]), $link); 
  235. } elseif(isset($_REQUEST['leadid']) && isset($_REQUEST['form'])) { 
  236. $link = wp_nonce_url(add_query_arg(array('leadid'=>(int)$_REQUEST['leadid'], 'form'=>(int)$_REQUEST['form']), $link), sprintf('view-%d-%d', $_REQUEST['leadid'], $_REQUEST['form']), 'view'); 
  237. return $link; 
  238.  
  239. static public function directory_canonical($permalink, $sentPost = '', $leavename = '') { 
  240.  
  241. // This was messing up the wp menu links 
  242. if(did_action('wp_head')) { return $permalink; } 
  243.  
  244. global $post; 
  245.  
  246. if( is_object( $post ) ) { 
  247. $post->permalink = $permalink; 
  248.  
  249. $url = add_query_arg(array()); 
  250.  
  251. $sentPostID = is_object($sentPost) ? $sentPost->ID : $sentPost; 
  252. // $post->ID === $sentPostID is so that add_query_arg match doesn't apply to prev/next posts; just current 
  253. preg_match('/('.sanitize_title(apply_filters('kws_gf_directory_endpoint', 'entry')).'\/([0-9]+)(?:\/|-)([0-9]+)\/?)/ism', $url, $matches); 
  254. if(isset($post->ID) && $post->ID === $sentPostID && !empty($matches)) { 
  255. return trailingslashit($permalink).$matches[0]; 
  256. } elseif(isset($post->ID) && $post->ID === $sentPostID && (isset($_REQUEST['leadid']) && isset($_REQUEST['form'])) || !empty($matches)) { 
  257. if($matches) { $leadid = $matches[2]; $form = $matches[1]; } 
  258. else { $leadid = $_REQUEST['leadid']; $form = $_REQUEST['form']; } 
  259.  
  260. return wp_nonce_url(add_query_arg(array('leadid' =>$leadid, 'form'=>$form), trailingslashit($permalink)), sprintf('view-%d-%d', $leadid, $form), 'view'); 
  261. return $permalink; 
  262.  
  263. static public function enqueue_files() { 
  264. global $post, $kws_gf_styles, $kws_gf_scripts, $kws_gf_directory_options; 
  265.  
  266. $kws_gf_styles = isset($kws_gf_styles) ? $kws_gf_styles : array(); 
  267. $kws_gf_scripts = isset($kws_gf_scripts) ? $kws_gf_scripts : array(); 
  268.  
  269. if( !empty($post) && 
  270. !empty($post->post_content) && 
  271. preg_match('/(.?)\[(directory)\b(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?(.?)/', $post->post_content, $matches) 
  272. ) { 
  273.  
  274. $options = self::directory_defaults(shortcode_parse_atts($matches[3])); 
  275. if(!is_array($options['lightboxsettings'])) { $options['lightboxsettings'] = explode(', ', $options['lightboxsettings']); } 
  276.  
  277. $kws_gf_directory_options = $options; 
  278. do_action('kws_gf_directory_enqueue', $options, $post); 
  279.  
  280. extract($options); 
  281.  
  282. if($jstable) { 
  283. $theme = apply_filters('kws_gf_tablesorter_theme', 'blue', $form); 
  284. wp_enqueue_style('tablesorter-'.$theme, plugins_url( "/bower_components/jquery.tablesorter/css/theme.{$theme}.css", __FILE__)); 
  285. wp_enqueue_script('tablesorter-min', plugins_url( "/bower_components/jquery.tablesorter/js/jquery.tablesorter.min.js", __FILE__), array('jquery')); 
  286. $kws_gf_styles[] = 'tablesorter-'.$theme; 
  287. $kws_gf_scripts[] = 'tablesorter-min'; 
  288.  
  289. if(!empty($lightboxsettings)) { 
  290. wp_enqueue_script('colorbox', plugins_url( "/bower_components/colorbox/jquery.colorbox-min.js", __FILE__), array('jquery')); 
  291. wp_enqueue_style('colorbox', plugins_url( "/bower_components/colorbox/example{$lightboxstyle}/colorbox.css", __FILE__), array()); 
  292. $kws_gf_scripts[] = $kws_gf_styles[] = 'colorbox'; 
  293. add_action(apply_filters('kws_gf_directory_colorbox_action', 'wp_footer'), array('GFDirectory', 'load_colorbox'), 1000); 
  294.  
  295. list($urlformid, $urlleadid) = self::get_form_and_lead_ids(); 
  296. if(isset($_GET['edit']) && !empty($urlformid) && isset($urlleadid)) { 
  297.  
  298. $edit_scripts = array( 'jquery', 'gform_json', 'gform_placeholder', 'sack', 'plupload-all' ); 
  299. wp_enqueue_script('gform_gravityforms', $edit_scripts ); 
  300.  
  301. $kws_gf_scripts[] = array_merge( $kws_gf_scripts, $edit_scripts ); 
  302.  
  303. static function format_colorbox_settings($colorboxSettings = array()) { 
  304. $settings = array(); 
  305. if(!empty($colorboxSettings) && is_array($colorboxSettings)) { 
  306. foreach($colorboxSettings as $key => $value) { 
  307. if($value === null) { continue; } 
  308. if($value === true) { 
  309. $value = 'true'; 
  310. } elseif(empty($value) && $value !== 0) { 
  311. $value = 'false'; 
  312. } else { 
  313. $value = '"'.$value.'"'; 
  314. $settings["{$key}"] = $key.':'.$value.''; 
  315. return $settings; 
  316.  
  317. static public function load_colorbox() { 
  318. global $kws_gf_directory_options; 
  319. extract($kws_gf_directory_options); 
  320.  
  321. $lightboxsettings = apply_filters('kws_gf_directory_lightbox_settings', $lightboxsettings); 
  322. $colorboxSettings = apply_filters('kws_gf_directory_colorbox_settings', array( 
  323. 'width' => apply_filters('kws_gf_directory_lightbox_width', '70%'),  
  324. 'height' => apply_filters('kws_gf_directory_lightbox_height', '70%'),  
  325. 'iframe' => true,  
  326. 'maxWidth' => '95%',  
  327. 'maxHeight' => '95%',  
  328. 'current' => '{current} of {total}',  
  329. 'rel' => apply_filters('kws_gf_directory_lightbox_settings_rel', null) 
  330. )); 
  331.  
  332. ?> 
  333. <script> 
  334. jQuery(document).ready(function($) { 
  335. <?php 
  336. $output = ''; 
  337. foreach($lightboxsettings as $key => $value) { 
  338. $settings = $colorboxSettings; 
  339. if(is_numeric($key)) { $key = $value; } 
  340. switch($key) { 
  341. case "images": 
  342. $settings['width'] = $settings['height'] = $settings['iframe'] = null; 
  343. break; 
  344. case "urls": 
  345. $settings['height'] = '80%'; 
  346. break; 
  347. $output .= "\t\t".'$(".colorbox[rel~=\'directory_'.$key.'\']").colorbox('; 
  348. if(!empty($settings)) { 
  349. $output .= "{\n\t\t\t".implode(", \n\t\t\t", self::format_colorbox_settings(apply_filters("kws_gf_directory_lightbox_{$key}_settings", $settings)))."\n\t\t}"; 
  350. $output .= ");\n\n"; 
  351. echo $output; 
  352. do_action('kws_gf_directory_jquery', $kws_gf_directory_options); 
  353. ?> 
  354. }); 
  355. </script> 
  356. <?php 
  357.  
  358. static public function add_rewrite() { 
  359. global $wp_rewrite, $wp; 
  360.  
  361. if(!$wp_rewrite->using_permalinks()) { return; } 
  362. $endpoint = sanitize_title(apply_filters('kws_gf_directory_endpoint', 'entry')); 
  363.  
  364. # @TODO: Make sure this works in MU 
  365. $wp_rewrite->add_permastruct("{$endpoint}", $endpoint.'/%'.$endpoint.'%/?', true); 
  366. $wp_rewrite->add_endpoint("{$endpoint}", EP_ALL); 
  367.  
  368. //Returns true if the current page is one of Gravity Forms pages. Returns false if not 
  369. public static function is_gravity_page($page = array()) { 
  370. $current_page = trim(strtolower(RGForms::get("page"))); 
  371. if(empty($page)) { 
  372. $gf_pages = array("gf_edit_forms", "gf_new_form", "gf_entries", "gf_settings", "gf_export", "gf_help"); 
  373. } else { 
  374. $gf_pages = is_array($page) ? $page : array($page); 
  375. return in_array($current_page, $gf_pages); 
  376.  
  377. static function directory_update_approved($lead_id = 0, $approved = 0, $form_id = 0, $approvedcolumn = 0) { 
  378. global $wpdb, $_gform_directory_approvedcolumn, $current_user; 
  379. $current_user = wp_get_current_user(); 
  380. $user_data = get_userdata($current_user->ID); 
  381.  
  382. if(!empty($approvedcolumn)) { $_gform_directory_approvedcolumn = $approvedcolumn; } 
  383.  
  384. if(empty($_gform_directory_approvedcolumn)) { return false; } 
  385.  
  386. $lead_detail_table = RGFormsModel::get_lead_details_table_name(); 
  387.  
  388. // This will be faster in the 1.6+ future. 
  389. if(function_exists('gform_update_meta')) { gform_update_meta($lead_id, 'is_approved', $approved); } 
  390.  
  391. if(empty($approved)) { 
  392. //Deleting details for this field 
  393. $sql = $wpdb->prepare("DELETE FROM $lead_detail_table WHERE lead_id=%d AND field_number BETWEEN %f AND %f ", $lead_id, $_gform_directory_approvedcolumn - 0.001, $_gform_directory_approvedcolumn + 0.001); 
  394. $wpdb->query($sql); 
  395.  
  396. RGFormsModel::add_note($lead_id, $current_user->ID, $user_data->display_name, stripslashes(__('Disapproved the lead', 'gravity-forms-addons'))); 
  397.  
  398. } else { 
  399.  
  400. // Get the fields for the lead 
  401. $current_fields = $wpdb->get_results($wpdb->prepare("SELECT id, field_number FROM $lead_detail_table WHERE lead_id=%d", $lead_id)); 
  402.  
  403. $lead_detail_id = RGFormsModel::get_lead_detail_id($current_fields, $_gform_directory_approvedcolumn); 
  404.  
  405. // If there's already a field for the approved column, then we update it. 
  406. if($lead_detail_id > 0) { 
  407. $update = $wpdb->update($lead_detail_table, array("value" => $approved), array("lead_id" => $lead_id, 'form_id' => $form_id, 'field_number' => $_gform_directory_approvedcolumn), array("%s"), array("%d", "%d", "%f")); 
  408. // Otherwise, we create it. 
  409. else { 
  410. $update = $wpdb->insert($lead_detail_table, array("lead_id" => $lead_id, "form_id" => $form_id, "field_number" => $_gform_directory_approvedcolumn, "value" => $approved), array("%d", "%d", "%f", "%s")); 
  411.  
  412. RGFormsModel::add_note($lead_id, $current_user->ID, $user_data->display_name, stripslashes(__('Approved the lead', 'gravity-forms-addons'))); 
  413.  
  414. static public function edit_lead_detail($Form, $lead, $options) { 
  415. global $current_user, $_gform_directory_approvedcolumn; 
  416. require_once(GFCommon::get_base_path() . "/form_display.php"); 
  417. if(empty($_gform_directory_approvedcolumn)) { $_gform_directory_approvedcolumn = self::get_approved_column($Form); } 
  418.  
  419. // We fetch this again, since it may have had some admin-only columns taken out. 
  420. #$lead = RGFormsModel::get_lead($lead["id"]); 
  421.  
  422. // If you want to allow users to edit their own approval (?) add a filter and return true. 
  423. if(apply_filters('kws_gf_directory_allow_user_edit_approved', false) === false) { 
  424. $Form['fields'] = self::remove_approved_column('form', $Form['fields'], $_gform_directory_approvedcolumn); 
  425.  
  426. // If this is not the form that should be edited 
  427. list($urlformid, $urlleadid) = self::get_form_and_lead_ids(); 
  428. if(intval($Form['id']) !== intval($urlformid) || intval($lead['id']) !== intval($urlleadid)) { return; } 
  429.  
  430. // If either of these two things are false (creator of lead, or admin) 
  431. if(!( 
  432.  
  433. // Users can edit their own listings, they are logged in, the current user is the creator of the lead 
  434. (!empty($options['useredit']) && is_user_logged_in() && intval($current_user->ID) === intval($lead['created_by'])) === true || // OR 
  435.  
  436. // Administrators can edit every listing, and this person has administrator access 
  437. (!empty($options['adminedit']) && self::has_access("gravityforms_directory")) === true) 
  438. ) { 
  439. // Kick them out. 
  440. printf( esc_html_e( '%sYou do not have permission to edit this form.%s', 'gravity-forms-addons') , '<div class="error">', '</div>'); 
  441. return; 
  442.  
  443. $validation_message = ''; 
  444.  
  445. // If the form is submitted 
  446. if(RGForms::post("action") === "update") { 
  447. check_admin_referer('gforms_save_entry', 'gforms_save_entry'); 
  448.  
  449. $lead = apply_filters('kws_gf_directory_lead_being_updated', $lead, $Form); 
  450.  
  451. // We don't DO passwords. 
  452. foreach($Form['fields'] as $key => $field) { 
  453. if($field['type'] === 'password') { unset($Form['fields'][$key]); } 
  454.  
  455. $is_valid = GFFormDisplay::validate($Form, $lead); 
  456.  
  457. $validation_message = ''; 
  458. foreach($Form['fields'] as $field) { 
  459. if(!GFCommon::is_product_field($field["type"])) { 
  460. $validation_message .= (rgget("failed_validation", $field) && !empty($field["validation_message"])) ? sprintf("<li class='gfield_description validation_message'><strong>%s</strong>: %s</li>", $field["label"], $field["validation_message"]) : "";; 
  461. if(!empty($validation_message)) { 
  462. $validation_message = '<ul>'.$validation_message.'</ul>'; 
  463. echo esc_html( apply_filters('kws_gf_directory_lead_error_message', sprintf( __("%sThere were errors with the edit you made.%s%s", 'gravity-forms-addons'), "<div class='error' id='message' style='padding:.5em .75em; background-color:#ffffcc; border:1px solid #ccc;'><p>", "</p>", $validation_message.'</div>'), $lead, $Form) ); 
  464.  
  465. // So the form submission always throws an error even though there's no problem. 
  466. // Product fields can't be edited, so that doesn't really matter. 
  467. if(!empty($is_valid) || (empty($is_valid) && empty($validation_message))) { 
  468. do_action('kws_gf_directory_pre_update_lead', $lead, $Form); 
  469. // since @3.6.1 to enable conditional fields' updates. 
  470. self::save_lead($Form, $lead); 
  471. $lead = RGFormsModel::get_lead($lead["id"]); 
  472.  
  473. do_action('kws_gf_directory_post_update_lead', $lead, $Form); 
  474. echo apply_filters('kws_gf_directory_lead_updated_message', sprintf( esc_html__("%sThe entry was successfully updated.%s", 'gravity-forms-addons'), "<p class='updated' id='message' style='padding:.5em .75em; background-color:#ffffcc; border:1px solid #ccc;'>", "</p>"), $lead, $Form); 
  475. return $lead; 
  476.  
  477. if((isset($_GET['edit']) && wp_verify_nonce($_GET['edit'], 'edit'.$lead['id'].$Form["id"])) || !empty($validation_message)) { 
  478.  
  479. // The ID of the form needs to be `gform_{form_id}` for the pluploader 
  480. ?> 
  481. <form method="post" id="gform_<?php echo esc_attr( $Form['id'] ); ?>" enctype="multipart/form-data" action="<?php echo remove_query_arg(array('gf_search', 'sort', 'dir', 'pagenum', 'edit'), add_query_arg(array()));?>"> 
  482. <?php 
  483. wp_nonce_field('gforms_save_entry', 'gforms_save_entry'); 
  484. ?> 
  485. <input type="hidden" name="action" id="action" value="update"/> 
  486. <input type="hidden" name="screen_mode" id="screen_mode" value="edit" /> 
  487. <?php 
  488.  
  489. $form_without_products = $Form; 
  490. $post_message_shown = false; 
  491. $product_fields = array(); 
  492. foreach($Form['fields'] as $key => $field) { 
  493. if( 
  494. GFCommon::is_product_field($field["type"]) || 
  495. is_numeric($lead["post_id"]) && GFCommon::is_post_field($field) 
  496. ) { 
  497. if(is_numeric($lead["post_id"]) && GFCommon::is_post_field($field) && !$message_shown ) { 
  498. echo apply_filters('kws_gf_directory_edit_post_details_text', sprintf( esc_html__('You can edit post details from the %1$spost page%2$s.', 'gravity-forms-addons'), '<a href="'.admin_url('post.php?action=edit&post='.$lead["post_id"]).'">', '</a>'), $field, $lead, $lead['post_id']); 
  499. $message_shown = true; 
  500.  
  501. unset($form_without_products['fields'][$key]); 
  502. $product_fields[] = $field['id']; 
  503. if(!empty($field['inputs'])) { 
  504. foreach($field['inputs'] as $input) { 
  505. $product_fields[] = $input['id']; 
  506.  
  507. $lead_without_products = &$lead; 
  508. foreach($product_fields as $product_field) { 
  509. $value = RGFormsModel::get_lead_field_value($lead, $field); 
  510. unset($lead_without_products[$product_field]); 
  511.  
  512. require_once(GFCommon::get_base_path() . "/entry_detail.php"); 
  513. GFEntryDetail::lead_detail_edit(apply_filters( 'kws_gf_directory_form_being_edited', $form_without_products, $lead), apply_filters( 'kws_gf_directory_lead_being_edited', $lead_without_products, $form_without_products)); 
  514. echo '<input class="button-primary" type="submit" tabindex="4" value="'.esc_attr( apply_filters('kws_gf_directory_update_lead_button_text', __('Update Entry', 'gravity-forms-addons') ) ).'" name="save" />'; 
  515. ?> 
  516. </form> 
  517. <?php 
  518. do_action('kws_gf_directory_post_after_edit_lead_form', $lead, $Form); 
  519. return false; 
  520. } elseif((isset($_GET['edit']) && !wp_verify_nonce($_GET['edit'], 'edit'))) { 
  521. echo apply_filters('kws_gf_directory_edit_access_error_message', sprintf( esc_html__("%sThe link to edit this entry is not valid; it may have expired.%s", 'gravity-forms-addons'), "<p class='error' id='message' style='padding:.5em .75em; background-color:#ffffcc; border:1px solid #ccc;'>", "</p>"), $lead, $Form); 
  522.  
  523. return $lead; 
  524.  
  525.  
  526. static public function lead_detail($Form, $lead, $allow_display_empty_fields=false, $inline = true, $options = array()) { 
  527.  
  528. if( !class_exists('GFEntryList')) { require_once(GFCommon::get_base_path() . "/entry_list.php"); } 
  529.  
  530. global $current_user, $_gform_directory_approvedcolumn; 
  531. get_currentuserinfo(); 
  532.  
  533. $display_empty_fields = ''; $allow_display_empty_fields = true; 
  534. if($allow_display_empty_fields) { 
  535. $display_empty_fields = @rgget("gf_display_empty_fields", $_COOKIE); 
  536. if(empty($options)) { 
  537. $options = self::directory_defaults(); 
  538.  
  539. // There is no edit link 
  540. if(isset($_GET['edit']) || RGForms::post("action") === "update") { 
  541. // Process editing leads 
  542. $lead = self::edit_lead_detail($Form, $lead, $options); 
  543. if(RGForms::post("action") !== "update") { return; } 
  544.  
  545. extract($options); 
  546.  
  547. ?> 
  548. <table cellspacing="0" class="widefat fixed entry-detail-view"> 
  549. <?php 
  550. $title = str_replace('%%formtitle%%', $Form["title"], str_replace('%%leadid%%', $lead['id'], $entrydetailtitle)); 
  551. if(!empty($title) && $inline) { ?> 
  552. <thead> 
  553. <tr> 
  554. <th id="details" colspan="2" scope="col"> 
  555. <?php 
  556. $title = apply_filters('kws_gf_directory_detail_title', apply_filters('kws_gf_directory_detail_title_'.(int)$lead['id'], array($title, $lead), true), true); 
  557. if(is_array($title)) { 
  558. echo $title[0]; 
  559. } else { 
  560. echo $title; 
  561. ?> 
  562. </th> 
  563. </tr> 
  564. </thead> 
  565. <?php 
  566. ?> 
  567. <tbody> 
  568. <?php 
  569. $count = 0; 
  570. $has_product_fields = false; 
  571. $field_count = sizeof($Form["fields"]); 
  572. $display_value = ''; 
  573. foreach($Form["fields"] as $field) { 
  574.  
  575. // Don't show fields defined as hide in single. 
  576. if(!empty($field['hideInSingle'])) { 
  577. if(self::has_access("gravityforms_directory")) { 
  578. echo "\n\t\t\t\t\t\t\t\t\t".'<!-- '.sprintf(esc_html__('(Admin-only notice) Field #%d not shown: "Hide This Field in Single Entry View" was selected.', 'gravity-forms-addons'), $field['id']).' -->'."\n\n"; 
  579. continue; 
  580.  
  581. $count++; 
  582. $is_last = $count >= $field_count ? true : false; 
  583.  
  584. switch(RGFormsModel::get_input_type($field)) { 
  585. case "section" : 
  586. if(!GFCommon::is_section_empty($field, $Form, $lead) || $display_empty_fields) { 
  587. $count++; 
  588. $is_last = $count >= $field_count ? true : false; 
  589. ?> 
  590. <tr> 
  591. <td colspan="2" class="entry-view-section-break<?php echo $is_last ? " lastrow" : ""?>"><?php echo esc_html(GFCommon::get_label($field))?></td> 
  592. </tr> 
  593. <?php 
  594. break; 
  595.  
  596. case "captcha": 
  597. case "html": 
  598. case "password": 
  599. case "page": 
  600. //ignore captcha, html, password, page field 
  601. break; 
  602.  
  603. case "post_image" : 
  604. $value = RGFormsModel::get_lead_field_value($lead, $field); 
  605. $valueArray = explode("|:|", $value); 
  606.  
  607. @list($url, $title, $caption, $description) = $valueArray; 
  608.  
  609. if(!empty($url)) { 
  610. $value = $display_value = self::render_image_link( $url, $lead, $options, $title, $caption, $description ); 
  611. break; 
  612.  
  613. default : 
  614. //ignore product fields as they will be grouped together at the end of the grid 
  615. if(GFCommon::is_product_field($field["type"])) { 
  616. $has_product_fields = true; 
  617. continue; 
  618.  
  619. $value = RGFormsModel::get_lead_field_value($lead, $field); 
  620. $display_value = GFCommon::get_lead_field_display($field, $value, $lead["currency"]); 
  621. break; 
  622.  
  623. } // end switch 
  624.  
  625. $display_value = apply_filters("gform_entry_field_value", $display_value, $field, $lead, $Form); 
  626. if($display_empty_fields || !empty($display_value) || $display_value === "0") { 
  627. $count++; 
  628. $is_last = $count >= $field_count && !$has_product_fields ? true : false; 
  629. $last_row = $is_last ? " lastrow" : ""; 
  630.  
  631. $display_value = empty($display_value) && $display_value !== "0" ? " " : $display_value; 
  632.  
  633. $content = ' 
  634. <tr> 
  635. <th colspan="2" class="entry-view-field-name">' . esc_html(GFCommon::get_label($field)) . '</th> 
  636. </tr> 
  637. <tr> 
  638. <td colspan="2" class="entry-view-field-value' . $last_row . '">' . $display_value . '</td> 
  639. </tr>'; 
  640.  
  641. $content = apply_filters("gform_field_content", $content, $field, $value, $lead["id"], $Form["id"]); 
  642.  
  643. echo $content; 
  644.  
  645.  
  646. } // End foreach 
  647.  
  648. $products = array(); 
  649. if($has_product_fields) { 
  650. $products = GFCommon::get_product_fields($Form, $lead); 
  651. if(!empty($products["products"])) { 
  652. ?> 
  653. <tr> 
  654. <td colspan="2" class="entry-view-field-name"><?php echo apply_filters("gform_order_label_{$Form["id"]}", apply_filters("gform_order_label", __("Order", "gravityforms"), $Form["id"]), $Form["id"]) ?></td> 
  655. </tr> 
  656. <tr> 
  657. <td colspan="2" class="entry-view-field-value lastrow"> 
  658. <table class="entry-products" cellspacing="0" width="97%"> 
  659. <colgroup> 
  660. <col class="entry-products-col1"> 
  661. <col class="entry-products-col2"> 
  662. <col class="entry-products-col3"> 
  663. <col class="entry-products-col4"> 
  664. </colgroup> 
  665. <thead> 
  666. <th scope="col"><?php echo apply_filters("gform_product_{$Form['id']}", apply_filters("gform_product", __("Product", "gravityforms"), $Form['id']), $Form['id']) ?></th> 
  667. <th scope="col" class="textcenter"><?php echo apply_filters("gform_product_qty_{$Form['id']}", apply_filters("gform_product_qty", __("Qty", "gravityforms"), $Form['id']), $Form['id']) ?></th> 
  668. <th scope="col"><?php echo apply_filters("gform_product_unitprice_{$Form['id']}", apply_filters("gform_product_unitprice", __("Unit Price", "gravityforms"), $Form['id']), $Form['id']) ?></th> 
  669. <th scope="col"><?php echo apply_filters("gform_product_price_{$Form['id']}", apply_filters("gform_product_price", __("Price", "gravityforms"), $Form['id']), $Form['id']) ?></th> 
  670. </thead> 
  671. <tbody> 
  672. <?php 
  673.  
  674. $total = 0; 
  675. foreach($products["products"] as $product) { 
  676. ?> 
  677. <tr> 
  678. <td> 
  679. <div class="product_name"><?php echo esc_html($product["name"])?></div> 
  680. <ul class="product_options"> 
  681. <?php 
  682. $price = GFCommon::to_number($product["price"]); 
  683. if(is_array(rgar($product, "options"))) { 
  684. $count = sizeof($product["options"]); 
  685. $index = 1; 
  686. foreach($product["options"] as $option) { 
  687. $price += GFCommon::to_number($option["price"]); 
  688. $class = $index == $count ? " class='lastitem'" : ""; 
  689. $index++; 
  690. ?> 
  691. <li<?php echo $class?>><?php echo $option["option_label"]?></li> 
  692. <?php 
  693. $subtotal = floatval($product["quantity"]) * $price; 
  694. $total += $subtotal; 
  695. ?> 
  696. </ul> 
  697. </td> 
  698. <td class="textcenter"><?php echo $product["quantity"] ?></td> 
  699. <td><?php echo GFCommon::to_money($price, $lead["currency"]) ?></td> 
  700. <td><?php echo GFCommon::to_money($subtotal, $lead["currency"]) ?></td> 
  701. </tr> 
  702. <?php 
  703. $total += floatval($products["shipping"]["price"]); 
  704. ?> 
  705. </tbody> 
  706. <tfoot> 
  707. <?php 
  708. if(!empty($products["shipping"]["name"])) { 
  709. ?> 
  710. <tr> 
  711. <td colspan="2" rowspan="2" class="emptycell"> </td> 
  712. <td class="textright shipping"><?php echo $products["shipping"]["name"] ?></td> 
  713. <td class="shipping_amount"><?php echo GFCommon::to_money($products["shipping"]["price"], $lead["currency"])?> </td> 
  714. </tr> 
  715. <?php 
  716. ?> 
  717. <tr> 
  718. <?php 
  719. if(empty($products["shipping"]["name"])) { 
  720. ?> 
  721. <td colspan="2" class="emptycell"> </td> 
  722. <?php 
  723. ?> 
  724. <td class="textright grandtotal"><?php esc_html_e("Total", "gravityforms") ?></td> 
  725. <td class="grandtotal_amount"><?php echo GFCommon::to_money($total, $lead["currency"])?></td> 
  726. </tr> 
  727. </tfoot> 
  728. </table> 
  729. </td> 
  730. </tr> 
  731.  
  732. <?php 
  733.  
  734. // Edit link 
  735. if( 
  736. !empty($options['useredit']) && is_user_logged_in() && intval( $current_user->ID ) === intval( $lead['created_by'] ) || // Is user who created the entry 
  737. !empty($options['adminedit']) && self::has_access("gravityforms_directory") // Or is an administrator 
  738. ) { 
  739.  
  740. if(!empty($options['adminedit']) && self::has_access("gravityforms_directory")) { 
  741. $editbuttontext = apply_filters('kws_gf_directory_edit_entry_text_admin', __("Edit Entry", 'gravity-forms-addons')); 
  742. } else { 
  743. $editbuttontext = apply_filters('kws_gf_directory_edit_entry_text_user', __("Edit Your Entry", 'gravity-forms-addons')); 
  744.  
  745. ?> 
  746. <tr> 
  747. <th scope="row" class="entry-view-field-name"><?php echo esc_html( apply_filters('kws_gf_directory_edit_entry_th', __( "Edit", "gravity-forms-addons" ) ) ); ?></th> 
  748. <td class="entry-view-field-value useredit"><a href="<?php echo add_query_arg(array('edit' => wp_create_nonce('edit'.$lead['id'].$Form["id"]))); ?>"><?php echo $editbuttontext; ?></a></td> 
  749. </tr> 
  750. <?php 
  751.  
  752. ?> 
  753. </tbody> 
  754. </table> 
  755. <?php 
  756.  
  757. static public function get_admin_only($form, $adminOnly = array()) { 
  758. if(!is_array($form)) { return false; } 
  759.  
  760. foreach($form['fields'] as $key=>$col) { 
  761. // Only the Go to Entry button adds disableMargins. 
  762.  
  763. if($col['type'] === 'hidden' && !empty($col['useAsEntryLink']) && !empty($col['disableMargins'])) { 
  764. continue; 
  765. if(!empty($col['adminOnly'])) { 
  766. $adminOnly[] = $col['id']; 
  767. if(isset($col['inputs']) && is_array($col['inputs'])) { 
  768. foreach($col['inputs'] as $key2=>$input) { 
  769. if(!empty($col['adminOnly'])) { 
  770. $adminOnly[] = $input['id']; 
  771. return $adminOnly; 
  772.  
  773. /** 
  774. * Get the form and lead IDs from the URL or from $_REQUEST 
  775. * @return array|null $formid, $leadid if found. Null if not. 
  776. */ 
  777. static private function get_form_and_lead_ids() { 
  778. global $wp, $wp_rewrite; 
  779.  
  780. $formid = $leadid = null; 
  781.  
  782. $url = isset($wp->request) ? $wp->request : add_query_arg(array()); 
  783.  
  784. if( 
  785. // If permalinks is turned on 
  786. $wp_rewrite->using_permalinks() && 
  787. // And 
  788. preg_match('/\/?'.sanitize_title(apply_filters('kws_gf_directory_endpoint', 'entry')).'\/([0-9]+)(?:\/|-)([0-9]+)/ism', $url, $matches)) { 
  789. $formid = $matches[1]; 
  790. $leadid = $matches[2]; 
  791. } else { 
  792. $formid = isset($_REQUEST['form']) ? (int)$_REQUEST['form'] : $formid; 
  793. $leadid = isset($_REQUEST['leadid']) ? (int)$_REQUEST['leadid'] : $leadid; 
  794.  
  795. return array($formid, $leadid); 
  796.  
  797.  
  798.  
  799. /** 
  800. * get_back_link function. 
  801. * 
  802. * @access public 
  803. * @param string $entryback (default: '') The text of the back-link anchor 
  804. * @return string The HTML link for the backlink 
  805. */ 
  806. static public function get_back_link($passed_entryback = '') { 
  807. global $pagenow, $wp_rewrite; 
  808.  
  809. $options = self::directory_defaults(); 
  810.  
  811. if(isset($_GET['edit'])) { 
  812. return '<p class="entryback"><a href="'.add_query_arg(array(), remove_query_arg(array('edit'))).'">'.esc_html(__(apply_filters('kws_gf_directory_edit_entry_cancel', "← Cancel Editing"), "gravity-forms-addons")).'</a></p>'; 
  813.  
  814. list($formid, $leadid) = self::get_form_and_lead_ids(); 
  815. extract($options); 
  816.  
  817. // Use passed value, if available. Otherwise, use default 
  818. $entryback = !empty($passed_entryback) ? $passed_entryback : $entryback; 
  819.  
  820. if($pagenow === 'entry-details.php') { 
  821.  
  822. // If possible, link back to the original post. 
  823. if(isset($_GET['post'])) { 
  824. $href = get_permalink((int)$_GET['post']); 
  825. } else { 
  826. // Otherwise we rely on Javascript below. 
  827. $href = '#'; 
  828.  
  829. $onclick = ' onclick="parent.jQuery.fn.colorbox.close();"'; 
  830. } else { 
  831. $onclick = ''; 
  832. $href = remove_query_arg(array('row', 'leadid', 'form', 'edit')); 
  833. if($wp_rewrite->using_permalinks()) { 
  834. $href = preg_replace('/('.sanitize_title(apply_filters('kws_gf_directory_endpoint', 'entry')).'\/(?:[0-9]+)(?:\/|-)(?:[0-9]+)\/?)/ism', '', $href); 
  835.  
  836. $url = parse_url(add_query_arg(array(), $href)); 
  837. if(!empty($url['query']) && !empty($permalink)) { $href .= '?'.$url['query']; } 
  838. if(!empty($options['entryanchor'])) { $href .= '#lead_row_'.$leadid; } 
  839.  
  840. // If there's a back link, format it 
  841. if(!empty($entryback) && !empty($entryonly)) { 
  842. $link = apply_filters('kws_gf_directory_backlink', '<p class="entryback"><a href="'.$href.'"'.$onclick.'>'.esc_html($entryback).'</a></p>', $href, $entryback); 
  843. } else { 
  844. $link = ''; 
  845.  
  846. return $link; 
  847.  
  848. static public function process_lead_detail($inline = true, $entryback = '', $showadminonly = false, $adminonlycolumns = array(), $approvedcolumn = null, $options = array(), $entryonly = true) { 
  849. global $wp, $post, $wp_rewrite, $wpdb; 
  850. $formid = $leadid = false; 
  851.  
  852. list($formid, $leadid) = self::get_form_and_lead_ids(); 
  853.  
  854. if(!is_null($leadid) && !is_null($formid)) { 
  855.  
  856. $form = apply_filters('kws_gf_directory_lead_detail_form', RGFormsModel::get_form_meta((int)$formid)); 
  857. $lead = apply_filters('kws_gf_directory_lead_detail', RGFormsModel::get_lead((int)$leadid)); 
  858.  
  859. if(empty($approvedcolumn)) { $approvedcolumn = self::get_approved_column($form); } 
  860. if(empty($adminonlycolumns) && !$showadminonly) { $adminonlycolumns = self::get_admin_only($form); } 
  861.  
  862. //since 3.5 
  863. $lead = self::remove_hidden_fields( array( $lead ), $adminonlycolumns, $approvedcolumn, true, true, $showadminonly , $form ); 
  864. $lead = $lead[0]; 
  865.  
  866. ob_start(); // Using ob_start() allows us to filter output 
  867. self::lead_detail($form, $lead, false, $inline, $options); 
  868. $content = ob_get_contents(); // Get the output 
  869. ob_end_clean(); // Clear the buffer 
  870.  
  871. // Get the back link if this is a single entry. 
  872. $link = !empty($entryonly) ? self::get_back_link($entryback) : ''; 
  873.  
  874. $content = $link . $content; 
  875. $content = apply_filters('kws_gf_directory_detail', apply_filters('kws_gf_directory_detail_'.(int)$leadid, $content, (int)$leadid), (int)$leadid); 
  876.  
  877.  
  878. if(isset($options['entryview'])) { 
  879. $content = self::html_display_type_filter($content, $options['entryview'], true); 
  880.  
  881. return $content; 
  882. } else { 
  883. return false; 
  884.  
  885. static public function change_directory_columns() { 
  886. check_ajax_referer('gforms_directory_columns', 'gforms_directory_columns'); 
  887. $columns = GFCommon::json_decode(stripslashes($_POST["directory_columns"]), true); 
  888. self::update_grid_column_meta((int)$_POST['form_id'], $columns); 
  889.  
  890. public static function update_grid_column_meta($form_id, $columns) { 
  891. global $wpdb; 
  892.  
  893. $meta = maybe_serialize(stripslashes_deep($columns) ); 
  894.  
  895. update_option('gf_directory_form_'.$form_id.'_grid', $meta); 
  896.  
  897. public static function get_grid_column_meta($form_id) { 
  898. global $wpdb; 
  899.  
  900. $grid = get_option('gf_directory_form_'.$form_id.'_grid'); 
  901. if(!$grid) { 
  902. $grid = GFFormsModel::get_grid_column_meta($form_id); 
  903. self::update_grid_column_meta($form_id, $grid); 
  904. return maybe_unserialize($grid); 
  905.  
  906. public static function get_grid_columns($form_id, $input_label_only=false) { 
  907. $form = GFFormsModel::get_form_meta($form_id); 
  908. $field_ids = self::get_grid_column_meta($form_id); 
  909.  
  910. if(!is_array($field_ids)) { 
  911. $field_ids = array(); 
  912. for($i=0, $count=sizeof($form["fields"]); $i<$count && $i<5; $i++) { 
  913. $field = $form["fields"][$i]; 
  914.  
  915. if(RGForms::get("displayOnly", $field)) 
  916. continue; 
  917.  
  918.  
  919. if(isset($field["inputs"]) && is_array($field["inputs"])) { 
  920. $field_ids[] = $field["id"]; 
  921. if($field["type"] == "name") { 
  922. $field_ids[] = $field["id"] . '.3'; //adding first name 
  923. $field_ids[] = $field["id"] . '.6'; //adding last name 
  924. else if(isset($field["inputs"][0])) { 
  925. $field_ids[] = $field["inputs"][0]["id"]; //getting first input 
  926. else{ 
  927. $field_ids[] = $field["id"]; 
  928. //adding default entry meta columns 
  929. $entry_metas = GFFormsModel::get_entry_meta($form_id); 
  930. foreach ($entry_metas as $key => $entry_meta) { 
  931. if (rgar($entry_meta, "is_default_column")) 
  932. $field_ids[] = $key; 
  933.  
  934. $columns = array(); 
  935. $entry_meta = GFFormsModel::get_entry_meta($form_id); 
  936. foreach($field_ids as $field_id) { 
  937.  
  938. switch($field_id) { 
  939. case "id" : 
  940. $columns[$field_id] = array("label" => "Entry Id", "type" => "id"); 
  941. break; 
  942. case "ip" : 
  943. $columns[$field_id] = array("label" => "User IP", "type" => "ip"); 
  944. break; 
  945. case "date_created" : 
  946. $columns[$field_id] = array("label" => "Entry Date", "type" => "date_created"); 
  947. break; 
  948. case "source_url" : 
  949. $columns[$field_id] = array("label" => "Source Url", "type" => "source_url"); 
  950. break; 
  951. case "payment_status" : 
  952. $columns[$field_id] = array("label" => "Payment Status", "type" => "payment_status"); 
  953. break; 
  954. case "transaction_id" : 
  955. $columns[$field_id] = array("label" => "Transaction Id", "type" => "transaction_id"); 
  956. break; 
  957. case "payment_date" : 
  958. $columns[$field_id] = array("label" => "Payment Date", "type" => "payment_date"); 
  959. break; 
  960. case "payment_amount" : 
  961. $columns[$field_id] = array("label" => "Payment Amount", "type" => "payment_amount"); 
  962. break; 
  963. case "created_by" : 
  964. $columns[$field_id] = array("label" => "User", "type" => "created_by"); 
  965. break; 
  966. case ((is_string($field_id) || is_int($field_id)) && array_key_exists($field_id, $entry_meta)) : 
  967. $columns[$field_id] = array("label" => $entry_meta[$field_id]["label"], "type" => $field_id); 
  968. break; 
  969. default : 
  970. $field = GFFormsModel::get_field($form, $field_id); 
  971. if($field) { 
  972. $columns[strval($field_id)] = array("label" => self::get_label($field, $field_id, $input_label_only), "type" => rgget("type", $field), "inputType" => rgget("inputType", $field)); 
  973. return $columns; 
  974.  
  975. /** 
  976. * Get the label for the input field. This is necessary to prevent Admin Labels from being used instead of normal labels. 
  977. */ 
  978. public static function get_label($field, $input_id = 0, $input_only = false) { 
  979. $field_label = rgar($field, "label"); 
  980. $input = GFFormsModel::get_input($field, $input_id); 
  981. if(rgget("type", $field) == "checkbox" && $input != null) 
  982. return $input["label"]; 
  983. else if($input != null) 
  984. return $input_only ? $input["label"] : $field_label . ' (' . $input["label"] . ')'; 
  985. else 
  986. return $field_label; 
  987.  
  988. public static function make_directory($atts) { 
  989. global $wpdb, $wp_rewrite, $post, $wpdb, $directory_shown, $kws_gf_scripts, $kws_gf_styles; 
  990.  
  991. if(!class_exists('GFEntryDetail')) { @require_once(GFCommon::get_base_path() . "/entry_detail.php"); } 
  992. if(!class_exists('GFCommon')) { @require_once(GFCommon::get_base_path() . "/common.php"); } 
  993. if(!class_exists('RGFormsModel')) { @require_once(GFCommon::get_base_path() . "/forms_model.php"); } 
  994. if(!class_exists('GFEntryList')) { require_once(GFCommon::get_base_path() . "/entry_list.php"); } 
  995.  
  996. //quit if version of wp is not supported 
  997. if(!class_exists('GFCommon') || !GFCommon::ensure_wp_version()) 
  998. return; 
  999.  
  1000. // Already showed edit directory form and there are more than one forms on the page. 
  1001. if(did_action('kws_gf_directory_post_after_edit_lead_form')) { return; } 
  1002.  
  1003. ob_start(); // Using ob_start() allows us to use echo instead of $output .= 
  1004.  
  1005. foreach($atts as $key => $att) { 
  1006. if(strtolower($att) == 'false') { $atts[$key] = false; } 
  1007. if(strtolower($att) == 'true') { $atts[$key] = true; } 
  1008.  
  1009. $atts['approved'] = isset($atts['approved']) ? $atts['approved'] : -1; 
  1010.  
  1011. if(!empty($atts['lightboxsettings']) && is_string($atts['lightboxsettings'])) { 
  1012. $atts['lightboxsettings'] = explode(', ', $atts['lightboxsettings']); 
  1013.  
  1014. $options = self::directory_defaults($atts); 
  1015.  
  1016. // Make sure everything is on the same page. 
  1017. if(is_array($options['lightboxsettings'])) { 
  1018. foreach($options['lightboxsettings'] as $key => $value) { 
  1019. if(is_numeric($key)) { 
  1020. $options['lightboxsettings']["{$value}"] = $value; 
  1021. unset($options['lightboxsettings']["{$key}"]); 
  1022.  
  1023.  
  1024. extract( $options ); 
  1025.  
  1026. $form_id = $form; 
  1027.  
  1028. $form = RGFormsModel::get_form_meta($form_id); 
  1029.  
  1030. if(empty($form)) { return;} 
  1031.  
  1032. $sort_field = empty($_GET["sort"]) ? $sort : $_GET["sort"]; 
  1033. $sort_direction = empty($_GET["dir"]) ? $dir : $_GET["dir"]; 
  1034. $search_query = !empty($_GET["gf_search"]) ? $_GET["gf_search"] : null; 
  1035.  
  1036.  
  1037. $start_date = !empty($_GET["start_date"]) ? $_GET["start_date"] : $start_date; 
  1038. $end_date = !empty($_GET["end_date"]) ? $_GET["end_date"] : $end_date; 
  1039.  
  1040. $page_index = empty($_GET["pagenum"]) ? $startpage -1 : intval($_GET["pagenum"]) - 1; 
  1041. $star = (isset($_GET["star"]) && is_numeric($_GET["star"])) ? intval($_GET["star"]) : null; 
  1042. $read = (isset($_GET["read"]) && is_numeric($_GET["read"])) ? intval($_GET["read"]) : null; 
  1043. $first_item_index = $page_index * $page_size; 
  1044. $link_params = array(); 
  1045. if(!empty($page_index)) { $link_params['pagenum'] = $page_index; } 
  1046. $formaction = remove_query_arg(array('gf_search', 'sort', 'dir', 'pagenum', 'edit'), add_query_arg($link_params)); 
  1047. $tableclass .= !empty($jstable) ? sprintf(' tablesorter tablesorter-%s', apply_filters('kws_gf_tablesorter_theme', 'blue', $form)) : ''; 
  1048. $title = $form["title"]; 
  1049. $sort_field_meta = RGFormsModel::get_field($form, $sort_field); 
  1050. $is_numeric = $sort_field_meta["type"] == "number"; 
  1051.  
  1052. $columns = self::get_grid_columns($form_id, true); 
  1053.  
  1054. $approvedcolumn = null; 
  1055.  
  1056. if((!$approved && $approved !== -1) || (!empty($smartapproval) && $approved === -1)) { 
  1057. $approvedcolumn = self::get_approved_column($form); 
  1058.  
  1059. if(!empty($smartapproval) && $approved === -1 && !empty($approvedcolumn)) { 
  1060. $approved = true; // If there is an approved column, turn on approval 
  1061. } else { 
  1062. $approved = false; // Otherwise, show entries as normal. 
  1063.  
  1064. $entrylinkcolumns = self::get_entrylink_column($form, $entry); 
  1065. $adminonlycolumns = self::get_admin_only($form); 
  1066.  
  1067. // 
  1068. // Show only a single entry 
  1069. // 
  1070. $detail = self::process_lead_detail(true, $entryback, $showadminonly, $adminonlycolumns, $approvedcolumn, $options, $entryonly); 
  1071.  
  1072. if(!empty($entry) && !empty($detail)) { 
  1073.  
  1074. // Once again, checking to make sure this hasn't been shown already with multiple shortcodes on one page. 
  1075. if(!did_action('kws_gf_after_directory')) { 
  1076. echo $detail; 
  1077.  
  1078. if(!empty($entryonly)) { 
  1079. do_action('kws_gf_after_directory', do_action('kws_gf_after_directory_form_'.$form_id, $form, compact("approved", "sort_field", "sort_direction", "search_query", "first_item_index", "page_size", "star", "read", "is_numeric", "start_date", "end_date"))); 
  1080.  
  1081. $content = ob_get_clean(); // Get the output and clear the buffer 
  1082.  
  1083. // If the form is form #2, two filters are applied: `kws_gf_directory_output_2` and `kws_gf_directory_output` 
  1084. $content = apply_filters('kws_gf_directory_output', apply_filters('kws_gf_directory_output_'.$form_id, self::html_display_type_filter($content, $directoryview))); 
  1085. return $content; 
  1086.  
  1087.  
  1088. // since 3.5 - remove columns of the fields not allowed to be shown 
  1089. $columns = self::remove_hidden_fields( $columns, $adminonlycolumns, $approvedcolumn, false, false, $showadminonly , $form ); 
  1090.  
  1091. // hook for external selection of columns 
  1092. $columns = apply_filters( 'kws_gf_directory_filter_columns', $columns ); 
  1093.  
  1094.  
  1095. //since 3.5 search criteria 
  1096. $show_search_filters = self::get_search_filters( $form ); 
  1097. $show_search_filters = apply_filters( 'kws_gf_directory_search_filters', $show_search_filters, $form ); 
  1098. $search_criteria = array(); 
  1099. foreach( $show_search_filters as $key ) { 
  1100. if( !empty( $_GET['filter_'. $key ] ) ) { 
  1101. $search_criteria[ $key ] = $_GET['filter_'. $key ]; 
  1102.  
  1103.  
  1104. // 
  1105. // Or start to generate the directory 
  1106. // 
  1107. $leads = GFDirectory::get_leads( $form_id, $sort_field, $sort_direction, $search_query, $first_item_index, $page_size, $star, $read, $is_numeric, $start_date, $end_date, 'active', $approvedcolumn, $limituser, $search_criteria ); 
  1108.  
  1109.  
  1110. // Allow lightbox to determine whether showadminonly is valid without passing a query string in URL 
  1111. if($entry === true && !empty($lightboxsettings['entry'])) { 
  1112. if(get_site_transient('gf_form_'.$form_id.'_post_'.$post->ID.'_showadminonly') != $showadminonly) { 
  1113. set_site_transient('gf_form_'.$form_id.'_post_'.$post->ID.'_showadminonly', $showadminonly, HOUR_IN_SECONDS); 
  1114. } else { 
  1115. delete_site_transient('gf_form_'.$form_id.'_post_'.$post->ID.'_showadminonly'); 
  1116.  
  1117.  
  1118. // Get a list of query args for the pagination links 
  1119. if(!empty($search_query)) { $args["gf_search"] = urlencode($search_query); } 
  1120. if(!empty($sort_field)) { $args["sort"] = $sort_field; } 
  1121. if(!empty($sort_direction)) { $args["dir"] = $sort_direction; } 
  1122. if(!empty($star)) { $args["star"] = $star; } 
  1123.  
  1124. if($page_size > 0) { 
  1125.  
  1126. // $leads contains all the entries according to request, since 3.5, to allow multisort. 
  1127. if( apply_filters( 'kws_gf_directory_want_multisort', false ) ) { 
  1128. $lead_count = count( $leads ); 
  1129. $leads = array_slice( $leads, $first_item_index, $page_size ); 
  1130. } else { 
  1131. $lead_count = self::get_lead_count($form_id, $search_query, $star, $read, $approvedcolumn, $approved, $leads, $start_date, $end_date, $limituser, $search_criteria); 
  1132.  
  1133.  
  1134.  
  1135. $page_links = array( 
  1136. 'base' => @add_query_arg('pagenum', '%#%'), // get_permalink().'%_%',  
  1137. 'format' => '&pagenum=%#%',  
  1138. 'add_args' => $args,  
  1139. 'prev_text' => $prev_text,  
  1140. 'next_text' => $next_text,  
  1141. 'total' => ceil($lead_count / $page_size),  
  1142. 'current' => $page_index + 1,  
  1143. 'show_all' => $pagelinksshowall,  
  1144. ); 
  1145.  
  1146. $page_links = apply_filters('kws_gf_results_pagination', $page_links); 
  1147.  
  1148. $page_links = paginate_links($page_links); 
  1149. } else { 
  1150. // Showing all results 
  1151. $page_links = false; 
  1152. $lead_count = sizeof($leads); 
  1153.  
  1154.  
  1155. if(!isset($directory_shown)) { 
  1156. $directory_shown = true; 
  1157.  
  1158.  
  1159. ?> 
  1160.  
  1161. <script> 
  1162. <?php if(!empty($lightboxsettings['images']) || !empty($lightboxsettings['entry'])) { ?> 
  1163.  
  1164. var tb_pathToImage = "<?php echo site_url('/wp-includes/js/thickbox/loadingAnimation.gif'); ?>"; 
  1165. var tb_closeImage = "<?php echo site_url('/wp-includes/js/thickbox/tb-close.png'); ?>"; 
  1166. var tb_height = 600; 
  1167. <?php } ?> 
  1168. function not_empty(variable) { 
  1169. if(variable == '' || variable == null || variable == 'undefined' || typeof(variable) == 'undefined') { 
  1170. return false; 
  1171. } else { 
  1172. return true; 
  1173.  
  1174. <?php if(!empty($jstable)) { ?> 
  1175. jQuery(document).ready(function($) { 
  1176. $('.tablesorter').each(function() { 
  1177. $(this).tablesorter(<?php echo apply_filters('kws_gf_directory_tablesorter_options', '') ?>); 
  1178. }); 
  1179. }); 
  1180. <?php } else if(isset($jssearch) && $jssearch) { ?> 
  1181. function Search(search, sort_field_id, sort_direction, search_criteria ) { 
  1182. if(not_empty(search)) { var search = "&gf_search=" + encodeURIComponent(search); } else { var search = ''; } 
  1183.  
  1184. var search_filters = ''; 
  1185. if( not_empty( search_criteria ) ) { 
  1186. $.each( search_criteria, function( index, value ) { 
  1187. search_filters += "&filter_" + index + "=" + encodeURIComponent(value); 
  1188. } ); 
  1189.  
  1190. if(not_empty(sort_field_id)) { var sort = "&sort=" + sort_field_id; } else { var sort = ''; } 
  1191. if(not_empty(sort_direction)) { var dir = "&dir=" + sort_direction; } else { var dir = ''; } 
  1192. var page = '<?php if($wp_rewrite->using_permalinks()) { echo '?'; } else { echo '&'; } ?>page='+<?php echo isset($_GET['pagenum']) ? intval($_GET['pagenum']) : '"1"'; ?>; 
  1193. var location = "<?php echo get_permalink($post->ID); ?>"+page+search+sort+dir+search_filters; 
  1194. document.location = location; 
  1195. <?php } ?> 
  1196. </script> 
  1197. <?php } ?> 
  1198.  
  1199. <div class="wrap"> 
  1200. <?php if( $titleshow ) : ?> 
  1201. <h2><?php echo $titleprefix.$title; ?></h2> 
  1202. <?php endif; ?> 
  1203.  
  1204. <?php // --- Render Search Box --- 
  1205.  
  1206. if( $search || !empty( $show_search_filters ) ) : ?> 
  1207.  
  1208. <form id="lead_form" method="get" action="<?php echo $formaction; ?>"> 
  1209. <?php 
  1210. //New logic for search criterias (since 3.5) 
  1211.  
  1212. if( !empty( $show_search_filters ) ) { 
  1213.  
  1214. foreach( $show_search_filters as $key ) { 
  1215. $properties = self::get_field_properties( $form, $key ); 
  1216. if( in_array( $properties['type'] , array( 'select', 'checkbox', 'radio', 'post_category' ) ) ) { 
  1217. echo self::render_search_dropdown( $properties['label'], 'filter_'.$properties['id'], $properties['choices'] ); //Label, name attr, choices 
  1218. } else { 
  1219. echo self::render_search_input( $properties['label'], 'filter_'.$properties['id'] ); //label, attr name 
  1220.  
  1221.  
  1222.  
  1223. ?> 
  1224. <p class="search-box"> 
  1225. <?php if( $search ) : ?> 
  1226. <label class="hidden" for="lead_search"><?php esc_html_e("Search Entries:", "gravity-forms-addons"); ?></label> 
  1227. <input type="text" name="gf_search" id="lead_search" value="<?php echo $search_query ?>"<?php if( $searchtabindex ) { echo ' tabindex="'.intval( $searchtabindex ).'"'; } ?> /> 
  1228. <?php endif; ?> 
  1229. <?php 
  1230. // If not using permalinks, let's make the form work! 
  1231. echo !empty($_GET['p']) ? '<input name="p" type="hidden" value="'.esc_html( $_GET['p'] ).'" />' : ''; 
  1232. echo !empty($_GET['page_id']) ? '<input name="page_id" type="hidden" value="'.esc_html($_GET['page_id']).'" />' : ''; 
  1233. ?> 
  1234. <input type="submit" class="button" id="lead_search_button" value="<?php esc_attr_e("Search", "gravity-forms-addons") ?>"<?php if($searchtabindex) { echo ' tabindex="'.intval($searchtabindex++).'"'; } ?> /> 
  1235. </p> 
  1236. </form> 
  1237.  
  1238. <?php endif; 
  1239.  
  1240.  
  1241. //Displaying paging links if appropriate 
  1242.  
  1243. if($lead_count > 0 && $showcount || $page_links) { 
  1244. if($lead_count == 0) { $first_item_index--; } 
  1245. ?> 
  1246. <div class="tablenav"> 
  1247. <div class="tablenav-pages"> 
  1248. <?php if($showcount) { 
  1249. if(($first_item_index + $page_size) > $lead_count || $page_size <= 0) { 
  1250. $second_part = $lead_count; 
  1251. } else { 
  1252. $second_part = $first_item_index + $page_size; 
  1253. ?> 
  1254. <span class="displaying-num"><?php printf(__("Displaying %d - %d of %d", "gravity-forms-addons"), $first_item_index + 1, $second_part, $lead_count) ?></span> 
  1255. <?php } if($page_links) { echo $page_links; } ?> 
  1256. </div> 
  1257. <div class="clear"></div> 
  1258. </div> 
  1259. <?php 
  1260.  
  1261. do_action('kws_gf_before_directory_after_nav', do_action('kws_gf_before_directory_after_nav_form_'.$form_id, $form, $leads, compact("approved", "sort_field", "sort_direction", "search_query", "first_item_index", "page_size", "star", "read", "is_numeric", "start_date", "end_date"))); 
  1262. ?> 
  1263.  
  1264. <table class="<?php echo $tableclass; ?>" cellspacing="0"<?php if(!empty($tablewidth)) { echo ' width="'.$tablewidth.'"'; } echo $tablestyle ? ' style="'.$tablestyle.'"' : ''; ?>> 
  1265. <?php if($thead) {?> 
  1266. <thead> 
  1267. <tr> 
  1268. <?php 
  1269.  
  1270. $addressesExist = false; 
  1271. foreach($columns as $field_id => $field_info) { 
  1272. $dir = $field_id == 0 ? "DESC" : "ASC"; //default every field so ascending sorting except date_created (id=0) 
  1273. if($field_id == $sort_field) { //reverting direction if clicking on the currently sorted field 
  1274. $dir = $sort_direction == "ASC" ? "DESC" : "ASC"; 
  1275. if(is_array($adminonlycolumns) && !in_array($field_id, $adminonlycolumns) || (is_array($adminonlycolumns) && in_array($field_id, $adminonlycolumns) && $showadminonly) || !$showadminonly) { 
  1276. if($field_info['type'] == 'address' && $appendaddress && $hideaddresspieces) { $addressesExist = true; continue; } 
  1277. ?> 
  1278. <?php 
  1279. $_showlink = false; 
  1280. if(isset($jssearch) && $jssearch && !isset($jstable)) { ?> 
  1281. <th scope="col" id="gf-col-<?php echo $form_id.'-'.$field_id ?>" class="manage-column" onclick="Search('<?php echo $search_query ?>', '<?php echo $field_id ?>', '<?php echo $dir ?>', '' );" style="cursor:pointer;"><?php 
  1282. } elseif(isset($jstable) && $jstable || $field_info['type'] === 'id') {?> 
  1283. <th scope="col" id="gf-col-<?php echo $form_id.'-'.$field_id ?>" class="manage-column"> 
  1284. <?php } else { 
  1285. $_showlink = true; 
  1286. ?> 
  1287. <th scope="col" id="gf-col-<?php echo $form_id.'-'.$field_id ?>" class="manage-column"> 
  1288. <a href="<?php 
  1289. $searchpage = isset($_GET['pagenum']) ? intval($_GET['pagenum']) : 1; 
  1290. $new_query_args = array('gf_search' => $search_query, 'sort' => $field_id, 'dir' => $dir, 'pagenum' => $searchpage ); 
  1291. foreach( $search_criteria as $key => $value ) { 
  1292. $new_query_args[ 'filter_'.$key ] = $value; 
  1293. echo add_query_arg( $new_query_args, get_permalink($post->ID)); 
  1294. ?>"><?php 
  1295. if($field_info['type'] == 'id' && $entry) { $label = $entryth; } 
  1296. else { $label = $field_info["label"]; } 
  1297.  
  1298. $label = apply_filters('kws_gf_directory_th', apply_filters('kws_gf_directory_th_'.$field_id, apply_filters('kws_gf_directory_th_'.sanitize_title($label), $label))); 
  1299. echo esc_html($label); 
  1300.  
  1301. if($_showlink) { ?></a><?php } ?> 
  1302. </th> 
  1303. <?php 
  1304.  
  1305. if($appendaddress && $addressesExist) { 
  1306. ?> 
  1307. <th scope="col" id="gf-col-<?php echo $form_id.'-'.$field_id ?>" class="manage-column" onclick="Search('<?php echo $search_query ?>', '<?php echo $field_id ?>', '<?php echo $dir ?>');" style="cursor:pointer;"><?php 
  1308. $label = apply_filters('kws_gf_directory_th', apply_filters('kws_gf_directory_th_address', 'Address')); 
  1309. echo esc_html($label) 
  1310.  
  1311. ?></th> 
  1312. <?php 
  1313. ?> 
  1314. </tr> 
  1315. </thead> 
  1316. <?php } ?> 
  1317. <tbody class="list:user user-list"> 
  1318. <?php 
  1319. include(plugin_dir_path( __FILE__ ) . "/template-row.php"); 
  1320. ?> 
  1321. </tbody> 
  1322. <?php if($tfoot) { 
  1323. if(isset($jssearch) && $jssearch && !isset($jstable)) { 
  1324. $th = '<th scope="col" id="gf-col-'.$form_id.'-'.$field_id.'" class="manage-column" onclick="Search(\''.$search_query.'\', \''.$field_id.'\', \''.$dir.'\');" style="cursor:pointer;">'; 
  1325. } else { 
  1326. $th = '<th scope="col" id="gf-col-'.$form_id.'-'.$field_id.'" class="manage-column">'; 
  1327. ?> 
  1328. <tfoot> 
  1329. <tr> 
  1330. <?php 
  1331. $addressesExist = false; 
  1332. foreach($columns as $field_id => $field_info) { 
  1333. $dir = $field_id == 0 ? "DESC" : "ASC"; //default every field so ascending sorting except date_created (id=0) 
  1334. if($field_id == $sort_field) { //reverting direction if clicking on the currently sorted field 
  1335. $dir = $sort_direction == "ASC" ? "DESC" : "ASC"; 
  1336. if(is_array($adminonlycolumns) && !in_array($field_id, $adminonlycolumns) || (is_array($adminonlycolumns) && in_array($field_id, $adminonlycolumns) && $showadminonly) || !$showadminonly) { 
  1337. if($field_info['type'] == 'address' && $appendaddress && $hideaddresspieces) { $addressesExist = true; continue; } 
  1338.  
  1339. echo $th; 
  1340.  
  1341. if($field_info['type'] == 'id' && $entry) { $label = $entryth; } 
  1342. else { $label = $field_info["label"]; } 
  1343.  
  1344. $label = apply_filters('kws_gf_directory_th', apply_filters('kws_gf_directory_th_'.$field_id, apply_filters('kws_gf_directory_th_'.sanitize_title($label), $label))); 
  1345. echo esc_html($label) 
  1346.  
  1347. ?></th> 
  1348. <?php 
  1349. if($appendaddress && $addressesExist) { 
  1350. ?> 
  1351. <th scope="col" id="gf-col-<?php echo $form_id.'-'.$field_id ?>" class="manage-column" onclick="Search('<?php echo $search_query ?>', '<?php echo $field_id ?>', '<?php echo $dir ?>');" style="cursor:pointer;"><?php 
  1352. $label = apply_filters('kws_gf_directory_th', apply_filters('kws_gf_directory_th_address', 'Address')); 
  1353. echo esc_html($label) 
  1354.  
  1355. ?></th> 
  1356. <?php 
  1357. ?> 
  1358. </tr> 
  1359. <?php if(!empty($credit)) { self::get_credit_link(sizeof($columns), $options); } ?> 
  1360. </tfoot> 
  1361. <?php } ?> 
  1362. </table> 
  1363. <?php 
  1364.  
  1365. do_action('kws_gf_after_directory_before_nav', do_action('kws_gf_after_directory_before_nav_form_'.$form_id, $form, $leads, compact("approved", "sort_field", "sort_direction", "search_query", "first_item_index", "page_size", "star", "read", "is_numeric", "start_date", "end_date"))); 
  1366.  
  1367.  
  1368. //Displaying paging links if appropriate 
  1369.  
  1370. if($lead_count > 0 && $showcount || $page_links) { 
  1371. if($lead_count == 0) { $first_item_index--; } 
  1372. ?> 
  1373. <div class="tablenav"> 
  1374. <div class="tablenav-pages"> 
  1375. <?php if($showcount) { 
  1376. if(($first_item_index + $page_size) > $lead_count || $page_size <= 0) { 
  1377. $second_part = $lead_count; 
  1378. } else { 
  1379. $second_part = $first_item_index + $page_size; 
  1380. ?> 
  1381. <span class="displaying-num"><?php printf(__("Displaying %d - %d of %d", "gravity-forms-addons"), $first_item_index + 1, $second_part, $lead_count) ?></span> 
  1382. <?php } if($page_links) { echo $page_links; } ?> 
  1383. </div> 
  1384. <div class="clear"></div> 
  1385. </div> 
  1386. <?php 
  1387.  
  1388. ?> 
  1389. </div> 
  1390. <?php 
  1391. if(empty($credit)) { 
  1392. echo "\n".'<!-- Directory generated by Gravity Forms Directory & Addons : http://wordpress.org/extend/plugins/gravity-forms-addons/ -->'."\n"; 
  1393.  
  1394. do_action('kws_gf_after_directory', do_action('kws_gf_after_directory_form_'.$form_id, $form, $leads, compact("approved", "sort_field", "sort_direction", "search_query", "first_item_index", "page_size", "star", "read", "is_numeric", "start_date", "end_date"))); 
  1395.  
  1396. $content = ob_get_contents(); // Get the output 
  1397. ob_end_clean(); // Clear the cache 
  1398.  
  1399. // If the form is form #2, two filters are applied: `kws_gf_directory_output_2` and `kws_gf_directory_output` 
  1400. $content = apply_filters('kws_gf_directory_output', apply_filters('kws_gf_directory_output_'.$form_id, self::html_display_type_filter($content, $directoryview))); 
  1401.  
  1402. return $content; // Return it! 
  1403.  
  1404. /** 
  1405. * Render image link HTML 
  1406. * 
  1407. * @since 3.7 
  1408. * @param [type] $url [description] 
  1409. * @param string $title [description] 
  1410. * @param string $caption [description] 
  1411. * @param string $description [description] 
  1412. * @return [type] [description] 
  1413. */ 
  1414. static private function render_image_link( $url, $lead, $options, $title = '', $caption = '', $description = '' ) { 
  1415.  
  1416. extract($options); 
  1417.  
  1418. $target = ($linknewwindow && empty($lightboxsettings['images'])) ? ' target="_blank"' : ''; 
  1419.  
  1420. $size = false; 
  1421. if( !empty( $options['getimagesize'] )) { 
  1422. $size = @getimagesize( $url ); 
  1423.  
  1424. //displaying thumbnail (if file is an image) or an icon based on the extension 
  1425. $icon = GFEntryList::get_icon_url($url); 
  1426. if(!preg_match('/icon\_image\.gif/ism', $icon)) { 
  1427. $src = $icon; 
  1428. if(!empty($size)) { 
  1429. $img = "<img src='$src' {$size[3]}/>"; 
  1430. } else { 
  1431. $img = "<img src='$src' />"; 
  1432. } else { // No thickbox for non-images please 
  1433. switch( strtolower( trim( $options['postimage'] ) ) ) { 
  1434. case 'image': 
  1435. $src = $url; 
  1436. break; 
  1437. case 'icon': 
  1438. default: 
  1439. $src = $icon; 
  1440. break; 
  1441. $img = array( 
  1442. 'src' => $src,  
  1443. 'size' => $size,  
  1444. 'title' => $title,  
  1445. 'caption' => $caption,  
  1446. 'description' => $description,  
  1447. 'url' => esc_url_raw( $url ),  
  1448. 'code' => isset($size[3]) ? "<img src='$src' {$size[3]} />" : "<img src='$src' />" 
  1449. ); 
  1450. $img = apply_filters('kws_gf_directory_lead_image', apply_filters('kws_gf_directory_lead_image_'.$options['postimage'], apply_filters('kws_gf_directory_lead_image_'.$lead['id'], $img))); 
  1451.  
  1452. $lightboxclass = ''; 
  1453.  
  1454. if(!empty($lightboxsettings['images']) && self::is_image_file( $url ) ) { 
  1455. if(wp_script_is('colorbox', 'registered')) { 
  1456. $lightboxclass = ' class="colorbox lightbox"'; 
  1457. } else if(wp_script_is('thickbox', 'registered')) { 
  1458. $lightboxclass = ' class="thickbox lightbox"'; 
  1459.  
  1460. if(in_array('images', $lightboxsettings) || !empty($lightboxsettings['images'])) { 
  1461. $lightboxclass .= ' rel="directory_all directory_images"'; 
  1462.  
  1463. $value = "<a href='{$url}'{$target}{$lightboxclass}>{$img['code']}</a>"; 
  1464.  
  1465. $value = apply_filters( 'kws_gf_directory_render_image_link', $value, $url, $lead, $options, $title, $caption, $description ); 
  1466.  
  1467. return $value; 
  1468.  
  1469. /** 
  1470. * Verify that the src URL matches image patterns. 
  1471. * 
  1472. * 
  1473. * @return boolean True: matches pattern; False: does not match pattern. 
  1474. */ 
  1475. public static function is_image_file( $src ) { 
  1476.  
  1477. $info = pathinfo( $src ); 
  1478.  
  1479. $image_exts = apply_filters('kws_gf_directory_image_extensions', array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp', 'tif', 'tiff', 'ico' )); 
  1480.  
  1481. return isset( $info['extension'] ) && in_array(strtolower( $info['extension'] ), $image_exts); 
  1482.  
  1483. /** 
  1484. * render_search_dropdown function. 
  1485. * 
  1486. * @since 3.5 
  1487. * @access private 
  1488. * @static 
  1489. * @param string $label (default: '') search field label 
  1490. * @param string $name (default: '') input name attribute 
  1491. * @param array $choices 
  1492. * @return field dropdown html 
  1493. */ 
  1494. static private function render_search_dropdown( $label = '', $name = '', $choices ) { 
  1495.  
  1496. if( empty( $choices ) || !is_array( $choices ) || empty( $name ) ) { 
  1497. return ''; 
  1498.  
  1499. $current_value = isset( $_GET[ $name ] ) ? $_GET[ $name ] : ''; 
  1500.  
  1501. $output = '<div class="search-box">'; 
  1502. $output .= '<label for=search-box-'.$name.'>' . $label . '</label>'; 
  1503. $output .= '<select name="'.$name.'" id="search-box-'.$name.'">'; 
  1504. $output .= '<option value="" '. selected( '', $current_value, false ) .'>---</option>'; 
  1505. foreach( $choices as $choice ) { 
  1506. $output .= '<option value="'. $choice['value'] .'" '. selected( $choice['value'], $current_value, false ) .'>'. $choice['text'] .'</option>'; 
  1507. $output .= '</select>'; 
  1508. $output .= '</div>'; 
  1509.  
  1510. return $output; 
  1511.  
  1512.  
  1513.  
  1514. /** 
  1515. * render_search_input function. 
  1516. * 
  1517. * @since 3.5 
  1518. * @access private 
  1519. * @static 
  1520. * @param string $label (default: '') search field label 
  1521. * @param string $name (default: '') input name attribute 
  1522. * @return field input html 
  1523. */ 
  1524. static private function render_search_input( $label = '', $name = '' ) { 
  1525.  
  1526. if( empty( $name ) ) { 
  1527. return ''; 
  1528.  
  1529. $current_value = isset( $_GET[ $name ] ) ? $_GET[ $name ] : ''; 
  1530.  
  1531. $output = '<div class="search-box">'; 
  1532. $output .= '<label for=search-box-'.$name.'>' . $label . '</label>'; 
  1533. $output .= '<input type="text" name="'.$name.'" id="search-box-'.$name.'" value="'.$current_value.'">'; 
  1534. $output .= '</div>'; 
  1535.  
  1536. return $output; 
  1537.  
  1538.  
  1539.  
  1540. static public function get_credit_link( $columns = 1, $options = array() ) { 
  1541. global $post;// prevents calling before <HTML> 
  1542. if(empty($post) || is_admin()) { return; } 
  1543.  
  1544. $settings = self::get_settings(); 
  1545.  
  1546. // Only show credit link if the user has saved settings; 
  1547. // this prevents existing directories adding a link without user action. 
  1548. if(isset($settings['version'])) { 
  1549. echo "<tr><th colspan='{$columns}'>". self::attr($options) ."</th></tr>"; 
  1550.  
  1551. static public function get_version() { 
  1552. return self::$version; 
  1553.  
  1554. static public function return_7776000() { 
  1555. return 7776000; // extend the cache to 90 days 
  1556.  
  1557. static public function attr($options, $default = '<span class="kws_gf_credit" style="font-weight:normal; text-align:center; display:block; margin:0 auto;">Powered by <a href="http://katz.co/gravity-forms-addons/">Gravity Forms Directory</a></span>') { 
  1558. include_once(ABSPATH . WPINC . '/feed.php'); 
  1559. add_filter( 'wp_feed_cache_transient_lifetime' , array('GFDirectory', 'return_7776000')); 
  1560. $rss = fetch_feed(add_query_arg(array('site' => htmlentities(substr(get_bloginfo('url'), is_ssl() ? 8 : 7)), 'from' => 'kws_gf_addons', 'version' => self::$version, 'credit' => !empty($options['credit'])), 'http://www.katzwebservices.com/development/attribution.php')); 
  1561. remove_filter( 'wp_feed_cache_transient_lifetime' , array('GFDirectory', 'return_7776000')); 
  1562. if($rss && !is_wp_error($rss)) { 
  1563. // We want to strip all tags except for 'style', 'id', and 'class' so that the return value is always safe for the site. 
  1564. $strip = array('bgsound', 'expr', 'onclick', 'onerror', 'onfinish', 'onmouseover', 'onmouseout', 'onfocus', 'onblur', 'lowsrc', 'dynsrc'); 
  1565. $rss->strip_attributes($strip); $rss_items = $rss->get_items(0, 1); 
  1566. foreach ( $rss_items as $item ) { 
  1567. return str_replace(array("\n", "\r"), ' ', $item->get_description()); 
  1568.  
  1569. return $default; 
  1570.  
  1571.  
  1572. static public function add_lead_approved_hidden_input($value, $lead, $field = '') { 
  1573. global $_gform_directory_processed_meta, $_gform_directory_approvedcolumn; 
  1574.  
  1575. if(!in_array($lead['id'], $_gform_directory_processed_meta)) { 
  1576. $_gform_directory_processed_meta[] = $lead['id']; 
  1577. if(empty($_gform_directory_approvedcolumn)) { 
  1578. $forms = RGFormsModel::get_forms(null, "title"); 
  1579. $_gform_directory_approvedcolumn = self::globals_get_approved_column($forms[0]->id); 
  1580. if(self::check_approval($lead, $_gform_directory_approvedcolumn)) { 
  1581. echo '<td style="display:none;"><input type="hidden" class="lead_approved" id="lead_approved_'.$lead['id'].'" value="true" /></td>'; 
  1582.  
  1583. return $value; 
  1584.  
  1585.  
  1586. static public function globals_get_approved_column($formID = 0) { 
  1587. global $_gform_directory_processed_meta, $_gform_directory_approvedcolumn, $_gform_directory_activeform; 
  1588.  
  1589. $_gform_directory_processed_meta = array(); 
  1590.  
  1591. if(empty($formID)) { 
  1592. $formID = RGForms::get("id"); 
  1593.  
  1594. if(empty($formID)) { 
  1595. $forms = RGFormsModel::get_forms(null, "title"); 
  1596. $formID = $forms[0]->id; 
  1597.  
  1598. if(!empty($formID)) { 
  1599. $_gform_directory_activeform = RGFormsModel::get_form_meta($formID); 
  1600. } else if(isset($_GET['id'])) { 
  1601. $_gform_directory_activeform = RGFormsModel::get_form_meta($_GET['id']); 
  1602.  
  1603. $_gform_directory_approvedcolumn = self::get_approved_column($_gform_directory_activeform); 
  1604.  
  1605. return $_gform_directory_approvedcolumn; 
  1606.  
  1607. static public function get_approved_column($form) { 
  1608. if(!is_array($form)) { return false; } 
  1609.  
  1610. foreach(@$form['fields'] as $key=>$col) { 
  1611. if(isset($col['inputs']) && is_array($col['inputs'])) { 
  1612. foreach($col['inputs'] as $key2=>$input) { 
  1613. if(strtolower($input['label']) == 'approved' && $col['type'] == 'checkbox' && !empty($col['adminOnly'])) { 
  1614. return $input['id']; 
  1615.  
  1616. foreach(@$form['fields'] as $key=>$col) { 
  1617. if(isset($col['label']) && strtolower($col['label']) == 'approved' && $col['type'] == 'checkbox') { 
  1618. if(isset($col['inputs'][0]['id'])) 
  1619. return $key; 
  1620.  
  1621. return null; 
  1622.  
  1623.  
  1624. static public function directory_update_approved_hook() { 
  1625. global $_gform_directory_approvedcolumn; 
  1626. check_ajax_referer('rg_update_approved', 'rg_update_approved'); 
  1627. if(!empty($_POST["lead_id"])) { 
  1628. $_gform_directory_approvedcolumn = empty($_gform_directory_approvedcolumn) ? self::globals_get_approved_column($_POST['form_id']) : $_gform_directory_approvedcolumn; 
  1629. self::directory_update_approved((int)$_POST["lead_id"], $_POST["approved"], (int)$_POST['form_id'], $_gform_directory_approvedcolumn); 
  1630.  
  1631. static public function settings_link( $links, $file ) { 
  1632. static $this_plugin; 
  1633. if( ! $this_plugin ) $this_plugin = plugin_basename(__FILE__); 
  1634. if ( $file == $this_plugin ) { 
  1635. $settings_link = '<a href="' . admin_url( 'admin.php?page=gf_settings&addon=Directory+%26+Addons' ) . '">' . __('Settings', 'gravity-forms-addons') . '</a>'; 
  1636. array_unshift( $links, $settings_link ); // before other links 
  1637. return $links; 
  1638.  
  1639. //Returns true if the current page is an Feed pages. Returns false if not 
  1640. private static function is_directory_page() { 
  1641. if(empty($_GET["pagenum"])) { return false; } 
  1642. $current_page = trim(strtolower($_GET["pagenum"])); 
  1643. $directory_pages = array("gf_directory"); 
  1644.  
  1645. return in_array($current_page, $directory_pages); 
  1646.  
  1647. static public function get_settings() { 
  1648. return get_option("gf_addons_settings", array( 
  1649. "directory" => true,  
  1650. "directory_defaults" => array(),  
  1651. "referrer" => false,  
  1652. "modify_admin" => array( 
  1653. 'expand' => true,  
  1654. 'toggle' => true,  
  1655. 'edit' => true,  
  1656. 'ids' => true 
  1657. ),  
  1658. "saved" => false,  
  1659. "version" => self::$version 
  1660. ); 
  1661.  
  1662. public static function disable_directory() { 
  1663. delete_option("gf_directory_oid"); 
  1664.  
  1665. public static function uninstall() { 
  1666.  
  1667. if(!GFDirectory::has_access("gravityforms_directory_uninstall")) 
  1668. (__("You don't have adequate permission to uninstall Directory Add-On.", "gravity-forms-addons")); 
  1669.  
  1670. //removing options 
  1671. delete_option("gf_addons_settings"); 
  1672.  
  1673. //Deactivating plugin 
  1674. $plugin = "gravity-forms-addons/gravity-forms-addons.php"; 
  1675. deactivate_plugins($plugin); 
  1676. update_option('recently_activated', array($plugin => time()) + (array)get_option('recently_activated')); 
  1677.  
  1678. private static function is_gravityforms_supported() { 
  1679. if(class_exists("GFCommon")) { 
  1680. $is_correct_version = version_compare(GFCommon::$version, self::$min_gravityforms_version, ">="); 
  1681. return $is_correct_version; 
  1682. else{ 
  1683. return false; 
  1684.  
  1685. protected static function get_has_access($required_permission) { 
  1686. $has_members_plugin = function_exists('members_get_capabilities'); 
  1687. $has_access = $has_members_plugin ? current_user_can($required_permission) : current_user_can("level_7"); 
  1688. if($has_access) 
  1689. return $has_members_plugin ? $required_permission : "level_7"; 
  1690. else 
  1691. return false; 
  1692.  
  1693. public static function has_access($required_permission) { 
  1694. return self::get_has_access($required_permission); 
  1695.  
  1696. //Returns the url of the plugin's root folder 
  1697. static public function get_base_url() { 
  1698. return plugins_url(null, __FILE__); 
  1699.  
  1700.  
  1701. /** 
  1702. * get_search_filters function. 
  1703. * 
  1704. * @since 3.5 
  1705. * @access public 
  1706. * @static 
  1707. * @param mixed $form 
  1708. * @return array search fields ids 
  1709. */ 
  1710. public static function get_search_filters( $form ) { 
  1711. if( empty($form['fields']) ) { 
  1712. return array(); 
  1713.  
  1714. $search_fields = array(); 
  1715.  
  1716. foreach( $form['fields'] as $field ) { 
  1717. if( !empty( $field['isSearchFilter'] ) ) { 
  1718. $search_fields[] = $field['id']; 
  1719.  
  1720. return $search_fields; 
  1721.  
  1722. /** 
  1723. * get_leads function. 
  1724. * 
  1725. * @access public 
  1726. * @static 
  1727. * @param int $form_id 
  1728. * @param int $sort_field_number (default: 0) 
  1729. * @param string $sort_direction (default: 'DESC') 
  1730. * @param string $search (default: '') 
  1731. * @param int $offset (default: 0) 
  1732. * @param int $page_size (default: 30) 
  1733. * @param mixed $star (default: null) 
  1734. * @param mixed $read (default: null) 
  1735. * @param bool $is_numeric_sort (default: false) 
  1736. * @param mixed $start_date (default: null) 
  1737. * @param mixed $end_date (default: null) 
  1738. * @param string $status (default: 'active') 
  1739. * @param mixed $approvedcolumn (default: null) 
  1740. * @param bool $limituser (default: false) 
  1741. * @param array $search_criterias, since 3.5 
  1742. * @return array Leads results 
  1743. */ 
  1744. public static function get_leads($form_id, $sort_field_number=0, $sort_direction='DESC', $search='', $offset=0, $page_size=30, $star=null, $read=null, $is_numeric_sort = false, $start_date=null, $end_date=null, $status='active', $approvedcolumn = null, $limituser = false, $search_criterias ) { 
  1745.  
  1746. global $wpdb; 
  1747.  
  1748. if($sort_field_number == 0) 
  1749. $sort_field_number = "date_created"; 
  1750.  
  1751. //since 3.5 
  1752. if( empty( $search_criterias ) ) { 
  1753. $search_criterias = array(); 
  1754.  
  1755. // Retrieve the leads based on whether it's sorted or not. 
  1756. if( is_numeric( $sort_field_number ) ) { 
  1757. $sql = self::sort_by_custom_field_query($form_id, $sort_field_number, $sort_direction, $search, $search_criterias, $offset, $page_size, $star, $read, $is_numeric_sort, $status, $approvedcolumn, $limituser ); 
  1758. } else { 
  1759. $sql = self::sort_by_default_field_query($form_id, $sort_field_number, $sort_direction, $search, $search_criterias, $offset, $page_size, $star, $read, $is_numeric_sort, $start_date, $end_date, $status, $approvedcolumn, $limituser ); 
  1760.  
  1761. //initializing rownum 
  1762. $wpdb->query("select @rownum:=0"); 
  1763.  
  1764. //getting results 
  1765.  
  1766. $results = $wpdb->get_results($sql); 
  1767.  
  1768.  
  1769. $return = ''; 
  1770. if(function_exists('gform_get_meta')) { 
  1771. $return = RGFormsModel::build_lead_array($results); // This is a private function until 1.6 
  1772.  
  1773. // Used by at least the show_only_user_entries() method 
  1774. $return = apply_filters( 'kws_gf_directory_lead_filter', $return, compact("approved", "sort_field_number", "sort_direction", "search_query", "search_criterias", "first_item_index", "page_size", "star", "read", "is_numeric", "start_date", "end_date", "status", "approvedcolumn", "limituser") ); 
  1775.  
  1776. return $return; 
  1777.  
  1778. static function is_current_user( $lead = array()) { 
  1779. global $current_user; 
  1780. get_currentuserinfo(); 
  1781. return ( (int)$current_user->ID === (int)$lead["created_by"]) ; 
  1782.  
  1783. static function show_only_user_entries($leads = array(), $settings = array()) { 
  1784. if(empty($settings['limituser'])) { return $leads; } 
  1785. return array_filter($leads, array('GFDirectory', 'is_current_user')); 
  1786.  
  1787.  
  1788. /** 
  1789. * sort_by_custom_field_query function. 
  1790. * 
  1791. * A copy of the Gravity Forms method, but adding $approvedcolumns and $limituser args 
  1792. * 
  1793. * @access private 
  1794. * @static 
  1795. * @param mixed $form_id 
  1796. * @param int $sort_field_number (default: 0) 
  1797. * @param string $sort_direction (default: 'DESC') 
  1798. * @param string $search (default: '') 
  1799. * @param array $search_criterias, since 3.5 
  1800. * @param int $offset (default: 0) 
  1801. * @param int $page_size (default: 30) 
  1802. * @param mixed $star (default: null) 
  1803. * @param mixed $read (default: null) 
  1804. * @param bool $is_numeric_sort (default: false) 
  1805. * @param string $status (default: 'active') 
  1806. * @param mixed $approvedcolumn (default: null) 
  1807. * @param bool $limituser (default: false) 
  1808. * @return void 
  1809. */ 
  1810. private static function sort_by_custom_field_query($form_id, $sort_field_number=0, $sort_direction='DESC', $search='', $search_criterias, $offset=0, $page_size=30, $star=null, $read=null, $is_numeric_sort = false, $status='active', $approvedcolumn = null, $limituser = false ) { 
  1811. global $wpdb, $current_user; 
  1812. if(!is_numeric($form_id) || !is_numeric($sort_field_number)|| !is_numeric($offset)|| !is_numeric($page_size)) 
  1813. return ""; 
  1814.  
  1815. $lead_detail_table_name = RGFormsModel::get_lead_details_table_name(); 
  1816. $lead_table_name = RGFormsModel::get_lead_table_name(); 
  1817.  
  1818. $orderby = $is_numeric_sort ? "ORDER BY query, (value+0) $sort_direction" : "ORDER BY query, value $sort_direction"; 
  1819.  
  1820. //$search = empty($search) ? "" : "WHERE d.value LIKE '%$search%' "; 
  1821. $search_term = "%$search%"; 
  1822. $search_filter = empty($search) ? "" : $wpdb->prepare("WHERE d.value LIKE %s", $search_term); 
  1823.  
  1824. //starred clause 
  1825. $where = empty($search) ? "WHERE" : "AND"; 
  1826. $search_filter .= $star !== null && $status == 'active' ? $wpdb->prepare("$where is_starred=%d AND status='active' ", $star) : ""; 
  1827.  
  1828. //read clause 
  1829. $where = empty($search) ? "WHERE" : "AND"; 
  1830. $search_filter .= $read !== null && $status == 'active' ? $wpdb->prepare("$where is_read=%d AND status='active' ", $read) : ""; 
  1831.  
  1832. //status clause 
  1833. if(function_exists('gform_get_meta')) { 
  1834. $where = empty($search) ? "WHERE" : "AND"; 
  1835. $search_filter .= $wpdb->prepare("$where status=%s ", $status); 
  1836.  
  1837. // new search criterias since 3.5 
  1838. $in_search_criteria = ''; 
  1839. if( !empty( $search_criterias ) ) { 
  1840. foreach( $search_criterias as $field_id => $value ) { 
  1841. $value = "%$value%"; 
  1842. $in_search_criteria .= $wpdb->prepare(" l.id IN (SELECT lead_id from $lead_detail_table_name WHERE field_number = %s AND value LIKE %s) AND ", $field_id, $value ); 
  1843. $where = empty($search_filter) ? "WHERE " : "AND "; 
  1844. $in_search_criteria = ( !empty($in_search_criteria) ) ? $where . substr( $in_search_criteria, 0, -4 ) : ''; // to add where/and and remove the last AND 
  1845.  
  1846.  
  1847. if($limituser) { 
  1848. get_currentuserinfo(); 
  1849. if((int)$current_user->ID !== 0 || ($current_user->ID === 0 && apply_filters('kws_gf_show_entries_if_not_logged_in', apply_filters('kws_gf_treat_not_logged_in_as_user', true)))) { 
  1850. $where = empty($search_filter) ? "WHERE" : "AND"; 
  1851. if((int)$current_user->ID === 0) { 
  1852. $search_filter .= $wpdb->prepare("$where (created_by IS NULL OR created_by=%d)", $current_user->ID); 
  1853. } else { 
  1854. $search_filter .= $wpdb->prepare("$where l.created_by=%d ", $current_user->ID); 
  1855. } else { 
  1856. return false; 
  1857.  
  1858. $field_number_min = $sort_field_number - 0.001; 
  1859. $field_number_max = $sort_field_number + 0.001; 
  1860.  
  1861. $in_filter = ""; 
  1862. if(!empty($approvedcolumn)) { 
  1863. $in_filter = $wpdb->prepare("WHERE l.id IN (SELECT lead_id from $lead_detail_table_name WHERE field_number BETWEEN %f AND %f)", $approvedcolumn - 0.001, $approvedcolumn + 0.001); 
  1864. // This will work once all the fields are converted to the meta_key after 1.6 
  1865. #$search_filter .= $wpdb->prepare(" AND m.meta_key = 'is_approved' AND m.meta_value = %s", 1); 
  1866.  
  1867. $limit_filter = ''; //paging is done later since 3.5 to allow multisort 
  1868. if( !apply_filters( 'kws_gf_directory_want_multisort', false ) ) { 
  1869. if($page_size > 0) { $limit_filter = "LIMIT $offset, $page_size"; } 
  1870.  
  1871. $sql = " 
  1872. SELECT filtered.sort, l.*, d.field_number, d.value 
  1873. FROM $lead_table_name l 
  1874. INNER JOIN $lead_detail_table_name d ON d.lead_id = l.id 
  1875. INNER JOIN ( 
  1876. SELECT distinct sorted.sort, l.id 
  1877. FROM $lead_table_name l 
  1878. INNER JOIN $lead_detail_table_name d ON d.lead_id = l.id 
  1879. INNER JOIN ( 
  1880. SELECT @rownum:=@rownum+1 as sort, id FROM ( 
  1881. SELECT 0 as query, lead_id as id, value 
  1882. FROM $lead_detail_table_name 
  1883. WHERE form_id=$form_id 
  1884. AND field_number between $field_number_min AND $field_number_max 
  1885.  
  1886. UNION ALL 
  1887.  
  1888. SELECT 1 as query, l.id, d.value 
  1889. FROM $lead_table_name l 
  1890. LEFT OUTER JOIN $lead_detail_table_name d ON d.lead_id = l.id AND field_number between $field_number_min AND $field_number_max 
  1891. WHERE l.form_id=$form_id 
  1892. AND d.lead_id IS NULL 
  1893.  
  1894. ) sorted1 
  1895. $orderby 
  1896. ) sorted ON d.lead_id = sorted.id 
  1897. $search_filter 
  1898. $in_search_criteria 
  1899. $limit_filter 
  1900. ) filtered ON filtered.id = l.id 
  1901. $in_filter 
  1902. ORDER BY filtered.sort"; 
  1903.  
  1904. return $sql; 
  1905.  
  1906.  
  1907. /** 
  1908. * sort_by_default_field_query function. 
  1909. * 
  1910. * A copy of the Gravity Forms method, but adding $approvedcolumns and $limituser args 
  1911. * 
  1912. * @access private 
  1913. * @static 
  1914. * @param mixed $form_id 
  1915. * @param mixed $sort_field 
  1916. * @param string $sort_direction (default: 'DESC') 
  1917. * @param string $search (default: '') 
  1918. * @param array $search_criterias - since 3.5 
  1919. * @param int $offset (default: 0) 
  1920. * @param int $page_size (default: 30) 
  1921. * @param mixed $star (default: null) 
  1922. * @param mixed $read (default: null) 
  1923. * @param bool $is_numeric_sort (default: false) 
  1924. * @param mixed $start_date (default: null) 
  1925. * @param mixed $end_date (default: null) 
  1926. * @param string $status (default: 'active') 
  1927. * @param mixed $approvedcolumn (default: null) 
  1928. * @param bool $limituser (default: false) 
  1929. * @return void 
  1930. */ 
  1931. private static function sort_by_default_field_query($form_id, $sort_field, $sort_direction='DESC', $search='', $search_criterias, $offset=0, $page_size=30, $star=null, $read=null, $is_numeric_sort = false, $start_date=null, $end_date=null, $status='active', $approvedcolumn = null, $limituser = false) { 
  1932. global $wpdb, $current_user; 
  1933.  
  1934. if(!is_numeric($form_id) || !is_numeric($offset)|| !is_numeric($page_size)) { 
  1935. return ""; 
  1936.  
  1937. $lead_detail_table_name = RGFormsModel::get_lead_details_table_name(); 
  1938. $lead_table_name = RGFormsModel::get_lead_table_name(); 
  1939.  
  1940. $search_term = "%$search%"; 
  1941. $search_filter = empty($search) ? "" : $wpdb->prepare(" AND value LIKE %s", $search_term); 
  1942.  
  1943. // new search criterias since 3.5 
  1944. $in_search_criteria = ''; 
  1945. if( !empty( $search_criterias ) ) { 
  1946. foreach( $search_criterias as $field_id => $value ) { 
  1947. $value = "%$value%"; 
  1948. $in_search_criteria .= $wpdb->prepare(" AND l.id IN (SELECT lead_id from $lead_detail_table_name WHERE field_number = %s AND value LIKE %s)", $field_id, $value ); 
  1949.  
  1950. $star_filter = $star !== null && $status == 'active' ? $wpdb->prepare(" AND is_starred=%d AND status='active' ", $star) : ""; 
  1951. $read_filter = $read !== null && $status == 'active' ? $wpdb->prepare(" AND is_read=%d AND status='active' ", $read) : ""; 
  1952. if(function_exists('gform_get_meta')) { 
  1953. $status_filter = $wpdb->prepare(" AND status=%s ", $status); 
  1954. } else { 
  1955. $status_filter = ''; 
  1956.  
  1957. $start_date_filter = empty($start_date) ? "" : " AND datediff(date_created, '$start_date') >=0"; 
  1958. $end_date_filter = empty($end_date) ? "" : " AND datediff(date_created, '$end_date') <=0"; 
  1959.  
  1960. $in_filter = ""; 
  1961. if(!empty($approvedcolumn)) { 
  1962. $in_filter = $wpdb->prepare("l.id IN (SELECT lead_id from $lead_detail_table_name WHERE field_number BETWEEN %f AND %f) AND", $approvedcolumn - 0.001, $approvedcolumn + 0.001); 
  1963. // This will work once all the fields are converted to the meta_key after 1.6 
  1964. #$search_filter .= $wpdb->prepare(" AND m.meta_key = 'is_approved' AND m.meta_value = %s", 1); 
  1965.  
  1966. $user_filter = ''; 
  1967. if($limituser) { 
  1968. get_currentuserinfo(); 
  1969. if((int)$current_user->ID !== 0 || ($current_user->ID === 0 && apply_filters('kws_gf_show_entries_if_not_logged_in', apply_filters('kws_gf_treat_not_logged_in_as_user', true)))) { 
  1970. if((int)$current_user->ID === 0) { 
  1971. $user_filter = $wpdb->prepare(" AND (created_by IS NULL OR created_by=%d)", $current_user->ID); 
  1972. } else { 
  1973. $user_filter = $wpdb->prepare(" AND created_by=%d ", $current_user->ID); 
  1974. } else { 
  1975. return false; 
  1976.  
  1977. $limit_filter = ''; //paging is done later since 3.5 to allow multisort 
  1978. if( !apply_filters( 'kws_gf_directory_want_multisort', false ) ) { 
  1979. if($page_size > 0) { $limit_filter = "LIMIT $offset, $page_size"; } 
  1980.  
  1981. $sql = " 
  1982. SELECT filtered.sort, l.*, d.field_number, d.value 
  1983. FROM $lead_table_name l 
  1984. INNER JOIN $lead_detail_table_name d ON d.lead_id = l.id 
  1985. INNER JOIN 
  1986. SELECT @rownum:=@rownum + 1 as sort, id 
  1987. FROM 
  1988. SELECT distinct l.id 
  1989. FROM $lead_table_name l 
  1990. INNER JOIN $lead_detail_table_name d ON d.lead_id = l.id 
  1991. WHERE $in_filter 
  1992. l.form_id=$form_id 
  1993. $search_filter 
  1994. $in_search_criteria 
  1995. $star_filter 
  1996. $read_filter 
  1997. $user_filter 
  1998. $status_filter 
  1999. $start_date_filter 
  2000. $end_date_filter 
  2001. ORDER BY $sort_field $sort_direction 
  2002. $limit_filter 
  2003. ) page 
  2004. ) filtered ON filtered.id = l.id 
  2005. ORDER BY filtered.sort"; 
  2006.  
  2007. return $sql; 
  2008.  
  2009. static function directory_anchor_text($value = null) { 
  2010.  
  2011. if(apply_filters('kws_gf_directory_anchor_text_striphttp', true)) { 
  2012. $value = str_replace('http://', '', $value); 
  2013. $value = str_replace('https://', '', $value); 
  2014.  
  2015. if(apply_filters('kws_gf_directory_anchor_text_stripwww', true)) { 
  2016. $value = str_replace('www.', '', $value); 
  2017. if(apply_filters('kws_gf_directory_anchor_text_rootonly', true)) { 
  2018. $value = preg_replace('/(.*?)\/(.+)/ism', '$1', $value); 
  2019. if(apply_filters('kws_gf_directory_anchor_text_nosubdomain', true)) { 
  2020. $value = preg_replace('/((.*?)\.)+(.*?)\.(.*?)/ism', '$3.$4', $value); 
  2021. if(apply_filters('kws_gf_directory_anchor_text_noquerystring', true)) { 
  2022. $ary = explode("?", $value); 
  2023. $value = $ary[0]; 
  2024. return $value; 
  2025.  
  2026. static public function r($content, $die = false) { 
  2027. echo '<pre>'.print_r($content, true).'</pre>'; 
  2028. if($die) { die(); } 
  2029.  
  2030. static private function get_entrylink_column($form, $entry = false) { 
  2031. if(!is_array($form)) { return false; } 
  2032.  
  2033. $columns = empty($entry) ? array() : array('id' => 'id'); 
  2034. foreach(@$form['fields'] as $key=>$col) { 
  2035. if(!empty($col['useAsEntryLink'])) { 
  2036. $columns[$col['id']] = $col['useAsEntryLink']; 
  2037.  
  2038. return empty($columns) ? false : $columns; 
  2039.  
  2040. static private function prep_address_field($field) { 
  2041. return !empty($field) ? GFCommon::trim_all($field) : ''; 
  2042.  
  2043. static function format_address($address = array(), $linknewwindow = false) { 
  2044. $address_field_id = @self::prep_address_field($address['id']); 
  2045. $street_value = @self::prep_address_field($address[$address_field_id . ".1"]); 
  2046. $street2_value = @self::prep_address_field($address[$address_field_id . ".2"]); 
  2047. $city_value = @self::prep_address_field($address[$address_field_id . ".3"]); 
  2048. $state_value = @self::prep_address_field($address[$address_field_id . ".4"]); 
  2049. $zip_value = @self::prep_address_field($address[$address_field_id . ".5"]); 
  2050. $country_value = @self::prep_address_field($address[$address_field_id . ".6"]); 
  2051.  
  2052. $address = $street_value; 
  2053. $address .= !empty($address) && !empty($street2_value) ? "<br />$street2_value" : $street2_value; 
  2054. $address .= !empty($address) && (!empty($city_value) || !empty($state_value)) ? "<br />$city_value" : $city_value; 
  2055. $address .= !empty($address) && !empty($city_value) && !empty($state_value) ? ", $state_value" : $state_value; 
  2056. $address .= !empty($address) && !empty($zip_value) ? " $zip_value" : $zip_value; 
  2057. $address .= !empty($address) && !empty($country_value) ? "<br />$country_value" : $country_value; 
  2058.  
  2059. //adding map link 
  2060. if(!empty($address) && apply_filters('kws_gf_directory_td_address_map', 1)) { 
  2061. $address_qs = str_replace("<br />", " ", $address); //replacing <br/> with spaces 
  2062. $address_qs = urlencode($address_qs); 
  2063. $target = ''; if($linknewwindow) { $target = ' target="_blank"'; } 
  2064. $address .= "<br/>".apply_filters('kws_gf_directory_map_link', "<a href='http://maps.google.com/maps?q=$address_qs'".$target." class='map-it-link'>".__('Map It')."</a>"); 
  2065. return $address; 
  2066.  
  2067. static public function html_display_type_filter($content = null, $type = 'table', $single = false) { 
  2068. switch($type) { 
  2069. case 'table': 
  2070. return $content; 
  2071. break; 
  2072. case 'ul': 
  2073. $content = self::convert_to_ul($content, $single); 
  2074. break; 
  2075. case 'dl': 
  2076. $content = self::convert_to_dl($content, $single); 
  2077. break; 
  2078. return $content; 
  2079.  
  2080. static public function convert_to_ul($content = null, $singleUL = false) { 
  2081.  
  2082. $strongHeader = apply_filters('kws_gf_convert_to_ul_strong_header', 1); 
  2083.  
  2084. // Directory View 
  2085. if(!$singleUL) { 
  2086. $content = preg_replace("/<table([^>]*)>/ism", "<ul$1>", $content); 
  2087. $content = preg_replace("/<\/table([^>]*)>/ism", "</ul>", $content); 
  2088. if($strongHeader) { 
  2089. $content = preg_replace("/<tr([^>]*)>\s+/", "\n\t\t\t\t\t\t\t\t\t\t\t\t<li$1><ul>", $content); 
  2090. $content = preg_replace("/<th([^>]*)\>(.*?)\<\/th\>/", "$2</strong>\n\t\t\t\t\t\t\t\t\t\t\t\t\t<ul>", $content); 
  2091. } else { 
  2092. $content = preg_replace("/<tr([^>]*)>\s+/", "\n\t\t\t\t\t\t\t\t\t\t\t\t<li$1>", $content); 
  2093. $content = preg_replace("/<th([^>]*)\>(.*?)\<\/th\>/", "$2\n\t\t\t\t\t\t\t\t\t\t\t\t\t<ul>", $content); 
  2094. $content = preg_replace("/<\/tr[^>]*>/", "\t\t\t\t\t</ul>\n\t\t\t\t\t\t\t\t\t\t\t\t</li>", $content); 
  2095. // Single listing view 
  2096. else { 
  2097. $content = preg_replace("/<table([^>]*)>/ism", "<ul$1>", $content); 
  2098. $content = preg_replace("/<\/table([^>]*)>/ism", "</ul>", $content); 
  2099. if($strongHeader) { 
  2100. $content = preg_replace("/<tr([^>]*)>\s+/", "\n\t\t\t\t\t\t\t\t\t\t\t\t<li$1><strong>", $content); 
  2101. $content = preg_replace("/<th([^>]*)\>(.*?)\<\/th\>/", "$2</strong>\n\t\t\t\t\t\t\t\t\t\t\t\t\t<ul>", $content); 
  2102. } else { 
  2103. $content = preg_replace("/<tr([^>]*)>\s+/", "\n\t\t\t\t\t\t\t\t\t\t\t\t<li$1>", $content); 
  2104. $content = preg_replace("/<th([^>]*)\>(.*?)\<\/th\>/", "$2\n\t\t\t\t\t\t\t\t\t\t\t\t\t<ul>", $content); 
  2105. $content = preg_replace("/<\/tr[^>]*>/", "\t\t\t\t\t</ul>\n\t\t\t\t\t\t\t\t\t\t\t\t</li>", $content); 
  2106. # $content = preg_replace("/\<\/p\>\s+\<\/li/ism", "\<\/p\>\<\/li", $content); 
  2107. $content = preg_replace("/(?:\s+)?(valign\=\"(?:.*?)\"|width\=\"(?:.*?)\"|cellspacing\=\"(?:.*?)\")(?:\s+)?/ism", ' ', $content); 
  2108. $content = preg_replace("/<\/?tbody[^>]*>/", "", $content); 
  2109. $content = preg_replace("/<thead[^>]*>.*<\/thead>|<tfoot[^>]*>.*<\/tfoot>/is", "", $content); 
  2110. $content = preg_replace("/\<td([^>]*)\>(\ |)\<\/td\>/", "", $content); 
  2111. $content = preg_replace("/\<td([^>]*)\>/", "\t\t\t\t\t<li$1>", $content); 
  2112. $content = preg_replace("/<\/td[^>]*>/", "</li>", $content); 
  2113. $content = preg_replace('/\s?colspan\="([^>]*?)"\s?/ism', ' ', $content); 
  2114. return $content; 
  2115.  
  2116. static public function convert_to_dl($content, $singleDL = false) { 
  2117. $back = ''; 
  2118. // Get the back link, if it exists 
  2119. preg_match("/\<p\sclass=\"entryback\"\>(.*?)\<\/p\>/", $content, $matches); 
  2120. if(isset($matches[0])) { $back = $matches[0]; } 
  2121. $content = preg_replace("/\<p\sclass=\"entryback\"\>(.*?)\<\/p\>/", "", $content); 
  2122. $content = preg_replace("/<\/?table[^>]*>|<\/?tbody[^>]*>/", "", $content); 
  2123. $content = preg_replace("/<thead[^>]*>.*<\/thead>|<tfoot[^>]*>.*<\/tfoot>/is", "", $content); 
  2124. if(!$singleDL) { 
  2125. $content = preg_replace("/<tr([^>]*)>/", "<dl$1>", $content); 
  2126. $content = preg_replace("/<\/tr[^>]*>/", "</dl>", $content); 
  2127. } else { 
  2128. $content = preg_replace("/<tr([^>]*)>/", "", $content); 
  2129. $content = preg_replace("/<\/tr[^>]*>/", "", $content); 
  2130. $content = preg_replace("/\<td([^>]*)\>(\ |)\<\/td\>/", "", $content); 
  2131. $content = preg_replace("/\<th([^>]*)\>(.*?)<\/th\>/ism", "<dt$1>$2</dt>", $content); 
  2132. $content = preg_replace('/<td(.*?)(title="(.*?)")?>(.*?)<\/td[^>]*>/ism', "<dt$1>$3</dt><dd>$4</dd>", $content); 
  2133. $output = $back; 
  2134. $output .= "\n\t\t\t\t\t\t\t\t".'<dl>'; 
  2135. $output .= $content; 
  2136. $output .= "\t\t\t\t\t\t".'</dl>'; 
  2137. return $output; 
  2138.  
  2139. static public function make_entry_link($options = array(), $link = false, $lead_id = '', $form_id = '', $field_id = '', $field_label = '', $linkClass = '') { 
  2140. global $wp_rewrite, $post, $wp; 
  2141. extract($options); 
  2142. $entrylink = (empty($link) || $link === ' ') ? $field_label : $link; //$entrylink; 
  2143.  
  2144. $entrytitle = apply_filters('kws_gf_directory_detail_title', apply_filters('kws_gf_directory_detail_title_'.$lead_id, $entrytitle)); 
  2145.  
  2146. if(!empty($lightboxsettings['entry'])) { 
  2147. $href = wp_nonce_url(plugins_url( "/entry-details.php?leadid=$lead_id&form={$form_id}&post={$post->ID}", __FILE__), sprintf('view-%d-%d', $lead_id, $form_id), 'view'); 
  2148. if(wp_script_is('colorbox', 'registered')) { 
  2149. $linkClass = ' class="colorbox lightbox" rel="directory_all directory_entry"'; 
  2150. } else if(wp_script_is('thickbox', 'registered')) { 
  2151. $linkClass = ' class="thickbox lightbox" rel="directory_all directory_entry"'; 
  2152. } else { 
  2153. $multisite = (function_exists('is_multisite') && is_multisite() && $wpdb->blogid == 1); 
  2154. if($wp_rewrite->using_permalinks()) { 
  2155. // example.com/example-directory/entry/4/14/ 
  2156. if(isset($post->ID)) { 
  2157. $url = get_permalink($post->ID); 
  2158. } else { 
  2159. $url = parse_url(add_query_arg(array())); 
  2160. $url = $url['path']; 
  2161. $href = trailingslashit($url).sanitize_title(apply_filters('kws_gf_directory_endpoint', 'entry')).'/'.$form_id.apply_filters('kws_gf_directory_endpoint_separator', '/').$lead_id.'/'; 
  2162. #if(!empty($url['query'])) { $href .= '?'.$url['query']; } 
  2163. $href = add_query_arg(array('gf_search' => !empty($_REQUEST['gf_search']) ? $_REQUEST['gf_search'] : null, 'sort' => isset($_REQUEST['sort']) ? $_REQUEST['sort'] : null, 'dir' => isset($_REQUEST['dir']) ? $_REQUEST['dir'] : null, 'pagenum' => isset($_REQUEST['pagenum']) ? $_REQUEST['pagenum'] : null, 'start_date' => isset($_REQUEST['start_date']) ? $_REQUEST['start_date'] : null, 'end_date' => isset($_REQUEST['start_date']) ? $_REQUEST['end_date'] : null), $href); 
  2164. } else { 
  2165. // example.com/?page_id=24&leadid=14&form=4 
  2166. $href = wp_nonce_url(add_query_arg(array('leadid'=>$lead_id, 'form' => $form_id)), sprintf('view-%d-%d', $lead_id, $form_id), 'view'); 
  2167.  
  2168. // If this is a preview, add preview arguments to the link. 
  2169. // @since 3.5 
  2170. if(!empty($_GET['preview']) && !empty($_GET['preview_id']) && !empty($_GET['preview_nonce'])) { 
  2171. if(current_user_can( 'edit_posts' )) { 
  2172. $href = add_query_arg(array('preview' => $_GET['preview'], 'preview_id' => $_GET['preview_id'], 'preview_nonce' => $_GET['preview_nonce']), $href); 
  2173.  
  2174. $value = '<a href="'.$href.'"'.$linkClass.' title="'.$entrytitle.'">'.$entrylink.'</a>'; 
  2175. return $value; 
  2176.  
  2177. static function get_lead_count($form_id, $search, $star=null, $read=null, $column, $approved = false, $leads = array(), $start_date = null, $end_date = null, $limituser = false, $search_criterias ) { 
  2178. global $wpdb, $current_user; 
  2179.  
  2180. if(!is_numeric($form_id)) 
  2181. return ""; 
  2182.  
  2183. $detail_table_name = RGFormsModel::get_lead_details_table_name(); 
  2184. $lead_table_name = RGFormsModel::get_lead_table_name(); 
  2185.  
  2186. $star_filter = $star !== null ? $wpdb->prepare("AND is_starred=%d ", $star) : ""; 
  2187. $read_filter = $read !== null ? $wpdb->prepare("AND is_read=%d ", $read) : ""; 
  2188. if(function_exists('gform_get_meta')) { 
  2189. $status_filter = $wpdb->prepare(" AND status=%s ", 'active'); 
  2190. } else { 
  2191. $status_filter = ''; 
  2192. $start_date_filter = empty($start_date) ? "" : " AND datediff(date_created, '$start_date') >=0"; 
  2193. $end_date_filter = empty($end_date) ? "" : " AND datediff(date_created, '$end_date') <=0"; 
  2194.  
  2195. $search_term = "%$search%"; 
  2196. $search_filter = empty($search) ? "" : $wpdb->prepare("AND ld.value LIKE %s", $search_term ); 
  2197.  
  2198. // new search criterias since 3.5 
  2199. $in_search_criteria = ''; 
  2200. if( !empty( $search_criterias ) ) { 
  2201. foreach( $search_criterias as $field_id => $value ) { 
  2202. $value = "%$value%"; 
  2203. $in_search_criteria .= $wpdb->prepare(" AND l.id IN (SELECT lead_id from $detail_table_name WHERE field_number = %s AND value LIKE %s)", $field_id, $value ); 
  2204.  
  2205.  
  2206. $user_filter = ''; 
  2207. if($limituser) { 
  2208. get_currentuserinfo(); 
  2209. if((int)$current_user->ID !== 0 || ($current_user->ID === 0 && apply_filters('kws_gf_show_entries_if_not_logged_in', apply_filters('kws_gf_treat_not_logged_in_as_user', true)))) { 
  2210. if(!empty($current_user->ID)) { 
  2211. $user_filter = $wpdb->prepare(" AND l.created_by=%d ", $current_user->ID); 
  2212. } else { 
  2213. $user_filter = $wpdb->prepare(" AND (created_by IS NULL OR created_by=%d)", $current_user->ID); 
  2214. } else { 
  2215. return false; 
  2216.  
  2217.  
  2218. $in_filter = ""; 
  2219. if($approved) { 
  2220. $in_filter = $wpdb->prepare("l.id IN (SELECT lead_id from $detail_table_name WHERE field_number BETWEEN %f AND %f) AND", $column - 0.001, $column + 0.001); 
  2221. // This will work once all the fields are converted to the meta_key after 1.6 
  2222. #$search_filter .= $wpdb->prepare(" AND m.meta_key = 'is_approved' AND m.meta_value = %s", 1); 
  2223.  
  2224. $sql = "SELECT count(distinct l.id) FROM $lead_table_name as l,  
  2225. $detail_table_name as ld"; 
  2226. # $sql .= function_exists('gform_get_meta') ? " INNER JOIN wp_rg_lead_meta m ON l.id = m.lead_id " : ""; // After 1.6 
  2227. $sql .= " 
  2228. WHERE $in_filter 
  2229. l.form_id=$form_id 
  2230. AND ld.form_id=$form_id 
  2231. AND l.id = ld.lead_id 
  2232. $star_filter 
  2233. $read_filter 
  2234. $status_filter 
  2235. $user_filter 
  2236. $start_date_filter 
  2237. $end_date_filter 
  2238. $search_filter 
  2239. $in_search_criteria"; 
  2240.  
  2241. return $wpdb->get_var($sql); 
  2242.  
  2243. static function check_meta_approval($lead_id) { 
  2244. return gform_get_meta($lead_id, 'is_approved'); 
  2245.  
  2246. static function check_approval($lead, $column) { 
  2247. return self::check_meta_approval($lead['id']); 
  2248.  
  2249. static function hide_in_directory($form, $field_id) { 
  2250. return self::check_hide_in('hideInDirectory', $form, $field_id); 
  2251.  
  2252. static function hide_in_single( $form, $field_id ) { 
  2253. return self::check_hide_in('hideInSingle', $form, $field_id); 
  2254.  
  2255. static function check_hide_in($type, $form, $field_id) { 
  2256. foreach( $form['fields'] as $field ) { 
  2257. # echo $field['label'] . ' / ' . floor($field['id']).' / '.floor($field_id).' / <strong>'.$field["{$type}"].'</strong><br />'; 
  2258. if(floor($field_id) === floor($field['id']) && !empty($field["{$type}"])) { 
  2259. return true; 
  2260.  
  2261. return false; 
  2262.  
  2263. /** 
  2264. * get field property value, for a specific field_id on a $form 
  2265. * 
  2266. * @since 3.5 
  2267. */ 
  2268. static function get_field_property( $property, $form, $field_id = '' ) { 
  2269. if( empty( $property ) || empty( $form ) || '' === $field_id ) { 
  2270. return false; 
  2271. foreach( $form['fields'] as $field ) { 
  2272.  
  2273. if( floor( $field_id ) === floor( $field['id'] ) && !empty( $field[ $property ] ) ) { 
  2274. return $field[ $property ]; 
  2275.  
  2276. return false; 
  2277.  
  2278. /** 
  2279. * get field properties, for a specific field_id on a $form 
  2280. * 
  2281. * @since 3.5 
  2282. * @param array $form GF Form array 
  2283. * @param string $field_id Field ID 
  2284. * @return boolean|array If the field matches the searched-for field ID, return the field array. Otherwise, return false. 
  2285. */ 
  2286. static function get_field_properties( $form, $field_id = '' ) { 
  2287. if( empty( $form ) || '' === $field_id ) { 
  2288. return false; 
  2289.  
  2290. foreach( $form['fields'] as $field ) { 
  2291. if( floor( $field_id ) === floor( $field['id'] ) ) { 
  2292. return $field; 
  2293. return false; 
  2294.  
  2295. /** 
  2296. * Deprecated. 
  2297. * 
  2298. * @deprecated 3.5 
  2299. */ 
  2300. static function remove_admin_only() {} 
  2301.  
  2302. static function remove_approved_column($type = 'form', $fields, $approvedcolumn) { 
  2303.  
  2304. foreach($fields as $key => $column) { 
  2305. if((int)floor($column['id']) === (int)floor($approvedcolumn)) { 
  2306. unset($fields["{$key}"]); 
  2307.  
  2308. return $fields; 
  2309.  
  2310.  
  2311. /** 
  2312. * Filter columns and fields when generating directory or single entry view based on Admin Only fields, or "hide from directory" fields or (since 3.5) only visible if user is logged in. 
  2313. * 
  2314. * This method replaces GFDirectory::remove_admin_only() in 3.5 
  2315. * 
  2316. * @since 3.5 
  2317. * @access public 
  2318. * @static 
  2319. * @param mixed $leads 
  2320. * @param mixed $admin_only 
  2321. * @param mixed $approved 
  2322. * @param mixed $is_leads 
  2323. * @param bool $is_single (default: false) 
  2324. * @param bool $show_admin_only (default: false) 
  2325. * @param mixed $form 
  2326. * @return void 
  2327. */ 
  2328. static function remove_hidden_fields( $leads, $admin_only, $approved, $is_leads, $is_single = false, $show_admin_only = false, $form ) { 
  2329.  
  2330. if( empty( $admin_only ) || !is_array( $admin_only ) ) { $admin_only = array(); } 
  2331.  
  2332. if( empty( $leads ) || !is_array( $leads ) ) { return $leads; } 
  2333.  
  2334. if( $is_leads ) { 
  2335.  
  2336. foreach( $leads as $index => $lead ) { 
  2337. // the field_ids are the numeric array keys of a lead 
  2338. $field_ids = array_filter( array_keys( $lead ), 'is_int' ); 
  2339.  
  2340. foreach( $field_ids as $id ) { 
  2341. if( self::check_hide_field_conditions( $id, $admin_only, $approved, $is_single, $show_admin_only, $form ) ) { 
  2342. unset( $leads[ $index ][ $id ] ); 
  2343.  
  2344.  
  2345. return $leads; 
  2346.  
  2347. } else { 
  2348.  
  2349. // the KEY = field_id (to be used to check directory columns) 
  2350. foreach( $leads as $key => $column) { 
  2351.  
  2352. if( self::check_hide_field_conditions( $key, $admin_only, $approved, $is_single, $show_admin_only, $form ) ) { 
  2353. unset( $leads[ $key ] ); 
  2354.  
  2355.  
  2356. return $leads; 
  2357.  
  2358.  
  2359.  
  2360. /** returns true if field should be hidden / returns false if not , since 3.5 */ 
  2361. static function check_hide_field_conditions( $field_id, $admin_only, $approved, $is_single = false, $show_admin_only = false, $form ) { 
  2362.  
  2363.  
  2364. $properties = self::get_field_properties( $form, $field_id ); 
  2365. if( empty( $properties ) ) { 
  2366. return false; 
  2367.  
  2368. //check if set to be hidden in directory or in single entry view 
  2369. if( ( $is_single && !empty( $properties['hideInSingle'] ) ) || ( !$is_single && !empty( $properties['hideInDirectory'] ) ) ) { 
  2370. return true; 
  2371.  
  2372. // check if is and admin only field and remove if not authorized to be shown 
  2373. if( !$show_admin_only && @in_array( $field_id, $admin_only ) && $field_id != $approved && $field_id != floor($approved) ) { 
  2374. return true; 
  2375.  
  2376. //check if field is only visible for logged in users, and in that case, check capabilities level 
  2377. if( !empty( $properties['visibleToLoggedIn'] ) && !current_user_can( $properties['visibleToLoggedInCap'] ) ) { 
  2378. return true; 
  2379.  
  2380. return false; 
  2381.  
  2382.  
  2383. /** 
  2384. * Adapted from forms_model.php, RGFormsModel::save_lead($Form, $lead) 
  2385. * @param array $form Form object. 
  2386. * @param array $lead Lead object 
  2387. * @return void 
  2388. */ 
  2389. public static function save_lead($form, &$lead ) { 
  2390. global $wpdb; 
  2391.  
  2392. if(IS_ADMIN && !GFCommon::current_user_can_any("gravityforms_edit_entries")) 
  2393. die(__("You don't have adequate permission to edit entries.", "gravityforms")); 
  2394.  
  2395. $lead_detail_table = RGFormsModel::get_lead_details_table_name(); 
  2396.  
  2397. //Inserting lead if null 
  2398. if($lead == null) { 
  2399. global $current_user; 
  2400. $user_id = $current_user && $current_user->ID ? $current_user->ID : 'NULL'; 
  2401.  
  2402. $lead_table = RGFormsModel::get_lead_table_name(); 
  2403. $user_agent = RGFormsModel::truncate($_SERVER["HTTP_USER_AGENT"], 250); 
  2404. $currency = GFCommon::get_currency(); 
  2405. $source_url = RGFormsModel::truncate(RGFormsModel::get_current_page_url(), 200); 
  2406.  
  2407. $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"], RGFormsModel::get_ip(), $source_url, $user_agent, $currency)); 
  2408.  
  2409.  
  2410. //reading newly created lead id 
  2411. $lead_id = $wpdb->insert_id; 
  2412. $lead = array("id" => $lead_id); 
  2413.  
  2414.  
  2415. $current_fields = $wpdb->get_results($wpdb->prepare("SELECT id, field_number FROM $lead_detail_table WHERE lead_id=%d", $lead["id"])); 
  2416. $original_post_id = rgget("post_id", $lead); 
  2417.  
  2418. $total_fields = array(); 
  2419. $calculation_fields = array(); 
  2420. $recalculate_total = false; 
  2421.  
  2422. foreach($form["fields"] as $field) { 
  2423.  
  2424. //Ignore fields that are marked as display only 
  2425. if(rgget("displayOnly", $field) && $field["type"] != "password") { 
  2426. continue; 
  2427.  
  2428. //ignore pricing fields in the entry detail 
  2429. if(RG_CURRENT_VIEW == "entry" && GFCommon::is_pricing_field($field["type"])) { 
  2430. continue; 
  2431.  
  2432.  
  2433. //process total field after all fields have been saved 
  2434. if($field["type"] == "total") { 
  2435. $total_fields[] = $field; 
  2436. continue; 
  2437.  
  2438. //only save fields that are not hidden (except on entry screen) 
  2439. if(RG_CURRENT_VIEW == "entry" || !RGFormsModel::is_field_hidden($form, $field, array(), $lead ) ) { 
  2440. // process calculation fields after all fields have been saved (moved after the is hidden check) 
  2441. if( GFCommon::has_field_calculation($field) ) { 
  2442. $calculation_fields[] = $field; 
  2443. continue; 
  2444.  
  2445. if($field['type'] == 'post_category') 
  2446. $field = GFCommon::add_categories_as_choices($field, ''); 
  2447.  
  2448. if(isset($field["inputs"]) && is_array($field["inputs"])) { 
  2449.  
  2450. foreach($field["inputs"] as $input) 
  2451. RGFormsModel::save_input($form, $field, $lead, $current_fields, $input["id"]); 
  2452. else{ 
  2453. RGFormsModel::save_input($form, $field, $lead, $current_fields, $field["id"]); 
  2454.  
  2455. //Refresh lead to support conditionals (not optimal but...) 
  2456. $lead = RGFormsModel::get_lead( $lead['id'] ); 
  2457.  
  2458. if(!empty($calculation_fields)) { 
  2459. foreach($calculation_fields as $calculation_field) { 
  2460.  
  2461. if(isset($calculation_field["inputs"]) && is_array($calculation_field["inputs"])) { 
  2462. foreach($calculation_field["inputs"] as $input) { 
  2463. RGFormsModel::save_input($form, $calculation_field, $lead, $current_fields, $input["id"]); 
  2464. RGFormsModel::refresh_lead_field_value($lead["id"], $input["id"]); 
  2465. else{ 
  2466. RGFormsModel::save_input($form, $calculation_field, $lead, $current_fields, $calculation_field["id"]); 
  2467. RGFormsModel::refresh_lead_field_value($lead["id"], $calculation_field["id"]); 
  2468.  
  2469. RGFormsModel::refresh_product_cache($form, $lead = RGFormsModel::get_lead($lead['id'])); 
  2470.  
  2471. //saving total field as the last field of the form. 
  2472. if(!empty($total_fields)) { 
  2473. foreach($total_fields as $total_field) { 
  2474. GFCommon::log_debug("Saving total field."); 
  2475. RGFormsModel::save_input($form, $total_field, $lead, $current_fields, $total_field["id"]); 
  2476.  
  2477.  
  2478.  
  2479.  
  2480. function kws_gf_load_functions() { 
  2481.  
  2482. // If Gravity Forms is installed and exists 
  2483. if(defined('RG_CURRENT_PAGE')) { 
  2484.  
  2485. function gf_field_value($leadid, $fieldid, $form = array()) { 
  2486. echo get_gf_field_value($leadid, $fieldid, $form); 
  2487.  
  2488.  
  2489. // To retrieve textarea inputs from a lead 
  2490. // Example: get_gf_field_value_long(22, '14'); 
  2491. function get_gf_field_value_long($leadid, $fieldid, $form = array(), $apply_filter=true) { 
  2492. return RGFormsModel::get_field_value_long($leadid, $fieldid, $form, $apply_filter); 
  2493.  
  2494. // To retrieve textarea inputs from a lead 
  2495. // Example: get_gf_field_value_long(22, '14'); 
  2496. function get_gf_field_value($leadid, $fieldid, $form = array()) { 
  2497. $lead = RGFormsModel::get_lead($leadid); 
  2498. $fieldid = floatval($fieldid); 
  2499. if(is_numeric($fieldid)) { 
  2500. $result = $lead["$fieldid"]; 
  2501.  
  2502. $max_length = GFORMS_MAX_FIELD_LENGTH; 
  2503.  
  2504. if(strlen($result) >= ($max_length - 50)) { 
  2505. $result = get_gf_field_value_long($lead["id"], $fieldid, $form); 
  2506. $result = trim($result); 
  2507.  
  2508. if(!empty($result)) { return $result; } 
  2509. return false; 
  2510.  
  2511. function gf_field_value_long($leadid, $fieldid, $form = array()) { 
  2512. echo get_gf_field_value_long($leadid, $fieldid, $form); 
  2513.  
  2514.  
  2515. // Gives you the label for a form input (such as First Name). Enter in the form and the field ID to access the label. 
  2516. // Example: echo get_gf_field_label(1, 1.3); 
  2517. // Gives you the label for a form input (such as First Name). Enter in the form and the field ID to access the label. 
  2518. // Example: echo get_gf_field_label(1, 1.3); 
  2519. function get_gf_field_label($form_id, $field_id) { 
  2520. $form = RGFormsModel::get_form_meta($form_id); 
  2521. foreach($form["fields"] as $field) { 
  2522. if($field['id'] == $field_id) { 
  2523. # $output = RGForms::escape_text($field['label']); // No longer used 
  2524. $output = esc_html($field['label']); // Using esc_html(), a WP function 
  2525. }elseif(is_array($field['inputs'])) { 
  2526. foreach($field["inputs"] as $input) { 
  2527. if($input['id'] == $field_id) { 
  2528. if(class_exists('GFCommon')) { 
  2529. $output = esc_html(GFCommon::get_label($field, $field_id)); 
  2530. } else { 
  2531. #$output = RGForms::escape_text(RGForms::get_label($field, $field_id)); // No longer used 
  2532. $output = esc_html(RGForms::get_label($field, $field_id)); // No longer used 
  2533. return $output; 
  2534. function gf_field_label($form_id, $field_id) { 
  2535. echo get_gf_field_label($form_id, $field_id); 
  2536.  
  2537. // Returns a form using php instead of shortcode 
  2538. function get_gf_form($id, $display_title=true, $display_description=true, $force_display=false, $field_values=null) { 
  2539. if(class_exists('GFFormDisplay')) { 
  2540. return GFFormDisplay::get_form($id, $display_title=true, $display_description=true, $force_display=false, $field_values=null); 
  2541. } else { 
  2542. return RGFormsModel::get_form($id, $display_title, $display_description); 
  2543. function gf_form($id, $display_title=true, $display_description=true, $force_display=false, $field_values=null) { 
  2544. echo get_gf_form($id, $display_title, $display_description, $force_display, $field_values); 
  2545.  
  2546. // Returns array of leads for a specific form 
  2547. function get_gf_leads($form_id, $sort_field_number=0, $sort_direction='DESC', $search='', $offset=0, $page_size=3000, $star=null, $read=null, $is_numeric_sort = false, $start_date=null, $end_date=null, $status = 'active', $approvedcolumn = false, $limituser = false) { 
  2548. return GFDirectory::get_leads($form_id, $sort_field_number, $sort_direction, $search, $offset, $page_size, $star, $read, $is_numeric_sort, $start_date, $end_date, $status, $approvedcolumn, $limituser); 
  2549.  
  2550. function gf_leads($form_id, $sort_field_number=0, $sort_direction='DESC', $search='', $offset=0, $page_size=3000, $star=null, $read=null, $is_numeric_sort = false, $start_date=null, $end_date=null) { 
  2551. echo get_gf_leads($form_id, $sort_field_number, $sort_direction, $search, $offset, $page_size, $star, $read, $is_numeric_sort, $start_date, $end_date); 
  2552.  
  2553. function kws_gf_directory($atts) { 
  2554. GFDirectory::make_directory($atts); 
  2555.  
  2556.  
  2557. if(!function_exists('kws_print_r')) { 
  2558. function kws_print_r($content, $die = false) { 
  2559. echo '<pre>'.print_r($content, true).'</pre>'; 
  2560. if($die) { die(); } 
  2561. return $content; 
  2562.  
  2563.  
  2564. /** Ending ?> left out intentionally */ 
.