GFDirectory

The Gravity Forms Directory & Addons GFDirectory class.

Defined (1)

The class is defined in the following location(s).

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