adding search and filters to admin
authorSteve Sutton <steve@gaslightmedia.com>
Mon, 15 Dec 2014 21:59:53 +0000 (16:59 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Mon, 15 Dec 2014 21:59:53 +0000 (16:59 -0500)
glm-employment.php
models/job.php
models/list-applications.php
views/admin/applicationList.php

index 3c2aff4..042e6de 100644 (file)
@@ -14,6 +14,8 @@ define('GLM_EMP_PLUGIN_PATH_FILE', __FILE__);
 define('GLM_EMP_VERSION', '1.0');
 define('GLM_EMP_VRS_OPTION_NAME', 'glm_jobs_db_version');
 define('GLM_EMP_POST_TYPE', 'glm_jobs');
+define('GLM_EMP_TAX_CATEGORIES', 'glm_jobscategory');
+define('GLM_EMP_TAX_DEPARTMENTS', 'glm_jobsdepartment');
 define('GLM_EMP_APPLICATION_TABLE', 'glm_jobs_application');
 define('GLM_EMP_FORM_TABLE', 'glm_jobs_app_form');
 
index ddaf14d..2927364 100644 (file)
@@ -13,12 +13,12 @@ class glm_employment_job
         add_filter('post_updated_messages', array($this, 'jobsUpdatedMessages'));
 
         add_filter('manage_edit-glm_jobs_columns',
-                    array($this, 'jobs_edit_columns'));
+                    array($this, 'jobsEditColumns'));
 
         add_action('manage_posts_custom_column',
-                    array($this, 'jobs_custom_columns'));
-        add_action('admin_init', array($this, 'meta_dates'));
-        add_action('save_post', array($this, 'save_emp_meta'));
+                    array($this, 'jobsCustomColumns'));
+        add_action('admin_init', array($this, 'metaDates'));
+        add_action('save_post', array($this, 'saveEmpMeta'));
         add_filter('manage_edit-glm_jobs_sortable_columns', array($this, 'sortByDates'));
         add_filter('restrict_manage_posts', array($this, 'jobFilterList'));
         add_filter('parse_query', array($this, 'jobFilter'));
@@ -35,13 +35,17 @@ class glm_employment_job
     public function jobFilter($query)
     {
         $qv = &$query->query_vars;
-        if ( ( isset($qv['glm_jobscategory']) &&  $qv['glm_jobscategory'] ) && is_numeric( $qv['glm_jobscategory'] ) ) {
-            $term = get_term_by( 'id', $qv['glm_jobscategory'], 'glm_jobscategory' );
-            $qv['glm_jobscategory'] = $term->slug;
+        if (isset($qv['post_type']) && $qv['post_type'] == GLM_EMP_POST_TYPE
+            && isset($qv[GLM_EMP_TAX_CATEGORIES]) && $qv[GLM_EMP_TAX_CATEGORIES] != 0
+        ) {
+            $term = get_term_by('id',$qv[GLM_EMP_TAX_CATEGORIES],GLM_EMP_TAX_CATEGORIES);
+            $qv[GLM_EMP_TAX_CATEGORIES] = $term->slug;
         }
-        if ( ( isset($qv['glm_jobsdepartment']) &&  $qv['glm_jobsdepartment'] ) && is_numeric( $qv['glm_jobsdepartment'] ) ) {
-            $term = get_term_by( 'id', $qv['glm_jobsdepartment'], 'glm_jobsdepartment' );
-            $qv['glm_jobsdepartment'] = $term->slug;
+        if (isset($qv['post_type']) && $qv['post_type'] == GLM_EMP_POST_TYPE
+            && isset($qv[GLM_EMP_TAX_DEPARTMENTS]) && $qv[GLM_EMP_TAX_DEPARTMENTS] != 0
+        ) {
+            $term = get_term_by('id',$qv[GLM_EMP_TAX_DEPARTMENTS],GLM_EMP_TAX_DEPARTMENTS);
+            $qv[GLM_EMP_TAX_DEPARTMENTS] = $term->slug;
         }
     }
 
@@ -50,23 +54,25 @@ class glm_employment_job
         global $wp_query;
         $screen = get_current_screen();
         if ($screen->post_type == GLM_EMP_POST_TYPE) {
+            $tax1 = get_taxonomy(GLM_EMP_TAX_CATEGORIES);
             wp_dropdown_categories(array(
-                'show_option_all' => 'Show all Categories',
-                'taxonomy'        => 'glm_jobscategory',
-                'name'            => 'glm_jobscategory',
+                'show_option_all' => __("Show all {$tax1->label}"),
+                'taxonomy'        => GLM_EMP_TAX_CATEGORIES,
+                'name'            => GLM_EMP_TAX_CATEGORIES,
                 'orderby'         => 'name',
-                'selected'        => (isset($wp_query->query['glm_jobscategory'])? $wp_query->query['glm_jobscategory']: ''),
+                'selected'        => (isset($wp_query->query[GLM_EMP_TAX_CATEGORIES])? $wp_query->query[GLM_EMP_TAX_CATEGORIES]: ''),
                 'hierarchical'    => true,
                 'depth'           => 3,
                 'show_count'      => true,
                 'hide_empty'      => true
             ));
+            $tax2 = get_taxonomy(GLM_EMP_TAX_DEPARTMENTS);
             wp_dropdown_categories(array(
-                'show_option_all' => 'Show all Departments',
-                'taxonomy'        => 'glm_jobsdepartment',
-                'name'            => 'glm_jobsdepartment',
+                'show_option_all' => __("Show all {$tax2->label}"),
+                'taxonomy'        => GLM_EMP_TAX_DEPARTMENTS,
+                'name'            => GLM_EMP_TAX_DEPARTMENTS,
                 'orderby'         => 'name',
-                'selected'        => (isset($wp_query->query['glm_jobsdepartment'])? $wp_query->query['glm_jobsdepartment']: ''),
+                'selected'        => (isset($wp_query->query[GLM_EMP_TAX_DEPARTMENTS])? $wp_query->query[GLM_EMP_TAX_DEPARTMENTS]: ''),
                 'hierarchical'    => true,
                 'depth'           => 3,
                 'show_count'      => true,
@@ -75,13 +81,13 @@ class glm_employment_job
         }
     }
 
-    public function meta_dates()
+    public function metaDates()
     {
         add_meta_box('glm_employment_meta', 'Position Information',
                      array($this, 'edit_job_meta'), GLM_EMP_POST_TYPE);
     }
 
-    public function edit_job_meta()
+    public function editJobMeta()
     {
         global $post;
         $custom  = get_post_custom($post->ID);
@@ -122,7 +128,7 @@ class glm_employment_job
         include $this->pluginDirName . 'views/admin/job_meta.php';
     }
 
-    public function save_emp_meta()
+    public function saveEmpMeta()
     {
         global $post;
         if (!isset($_POST['glm-jobs-nonce'])) {
@@ -175,6 +181,7 @@ class glm_employment_job
             'name'               => _x('Jobs', 'job general name'),
             'singular_name'      => _x('Job', 'job type singular name'),
             'add_new'            => _x('Add New', 'job'),
+            'add_new_item'       => __('Add New Job'),
             'edit_item'          => __('Edit Job'),
             'new_item'           => __('New Job'),
             'all_items'          => __('All Jobs'),
@@ -192,12 +199,13 @@ class glm_employment_job
             'public'          => true,
             'can_export'      => true,
             'menu_position'   => 21,
-            'supports'        => array('title', 'editor', 'thumbnail'),
-            'has_archive'     => false,
+            'supports'        => array('title', 'editor'),
+            'has_archive'     => true,
             'menu_icon'       => 'dashicons-businessman',
-            'capability_type' => 'page',
+            'capability_type' => 'post',
             'hierarchical'    => false,
-//            'rewrite'         => array('slug' => 'jobs')
+            'rewrite'         => array('slug' => 'jobs'),
+            'taxonomies'      => array(GLM_EMP_TAX_CATEGORIES, GLM_EMP_TAX_DEPARTMENTS)
         );
         register_post_type(GLM_EMP_POST_TYPE, $args);
     }
@@ -247,15 +255,15 @@ class glm_employment_job
         );
 
         register_taxonomy(
-            'glm_jobscategory',
+            GLM_EMP_TAX_CATEGORIES,
             GLM_EMP_POST_TYPE,
             array(
                 'label'        => __('Job Category'),
                 'labels'       => $labels,
                 'hierarchical' => true,
                 'show_ui'      => true,
-                'query_var'    => true,
-                'rewrite'      => array('slug' => 'job-category'),
+                'query_var'    => GLM_EMP_TAX_CATEGORIES,
+                'rewrite'      => array('slug' => GLM_EMP_TAX_CATEGORIES),
         ));
 
         $labels = array(
@@ -278,19 +286,19 @@ class glm_employment_job
         );
 
         register_taxonomy(
-            'glm_jobsdepartment',
+            GLM_EMP_TAX_DEPARTMENTS,
             GLM_EMP_POST_TYPE,
             array(
                 'label'        => __('Job Department'),
                 'labels'       => $labels,
                 'hierarchical' => true,
                 'show_ui'      => true,
-                'query_var'    => true,
-                'rewrite'      => array('slug' => 'job-department'),
+                'query_var'    => GLM_EMP_TAX_DEPARTMENTS,
+                'rewrite'      => array('slug' => GLM_EMP_TAX_DEPARTMENTS),
         ));
     }
 
-    function jobs_edit_columns($columns)
+    function jobsEditColumns($columns)
     {
         $columns = array(
             "cb"               => "<input type=\"checkbox\" />",
@@ -302,7 +310,7 @@ class glm_employment_job
         return $columns;
     }
 
-    function jobs_custom_columns($column)
+    function jobsCustomColumns($column)
     {
         global $post;
         $custom = get_post_custom();
index 8e136ab..6c72df8 100644 (file)
@@ -21,11 +21,25 @@ class List_Applications extends GLM_List_Table
     }
     public function get_data()
     {
+        $where = array();
+        if ($_POST['s']) {
+//            var_dump($_POST);
+            $search = filter_var($_REQUEST['s'], FILTER_SANITIZE_STRING);
+            $where[] = "CONCAT(UPPER(fname), UPPER(lname))  like '%".strtoupper($search)."%'";
+        }
+        if ($archived = filter_var($_REQUEST['archived'], FILTER_VALIDATE_BOOLEAN)) {
+            $where[] = "archived = true";
+        } else {
+            $where[] = "archived <> true";
+        }
         $sql = "
-        SELECT id as ID,fname, lname,create_time
+        SELECT id as ID,fname,lname,create_time
           FROM " . $this->wpdb->prefix . GLM_EMP_APPLICATION_TABLE . "
-         WHERE archived <> true
         ";
+        if (!empty($where)) {
+            $sql .= " WHERE ".implode(" AND ", $where);
+        }
+//        var_dump($sql);
         return $this->wpdb->get_results(
            $sql,
             ARRAY_A
index a0d8ae4..8bca340 100644 (file)
@@ -1,4 +1,21 @@
-<form method="post">
+<h2>Applications</h2>
+<?php
+if ($search = filter_var($_REQUEST['s'], FILTER_SANITIZE_STRING)) {
+    echo '<span class="subtitle"> Search Results for "' . $search.'"</span>';
+}
+?>
+<ul class="subsubsub">
+    <li class="all"><a href="edit.php?post_type=<?php echo GLM_EMP_POST_TYPE;?>&page=applications">All</a> |</li>
+    <li class="all"><a href="edit.php?post_type=<?php echo GLM_EMP_POST_TYPE;?>&page=applications&archived=1">Archived</a></li>
+</ul>
+
+<form method="post" action="">
+    <p class="search-box">
+        <label class="screen-reader-text" for="app-search-input">Search Applicants</label>
+        <input id="app-search-input" name="s" type="search"
+               value="<?php echo htmlspecialchars($_REQUEST['s']);?>">
+        <input type="submit" class="button" value="Search Applicants">
+    </p>
     <div class="wrap">
         <?php $applicationList->prepare_items();?>
     <?php $applicationList->display();?>