Skip to content

Commit

Permalink
New "status" paramter and EE3 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
elivz committed Dec 6, 2017
1 parent 506b310 commit 0904319
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
6 changes: 4 additions & 2 deletions README.markdown → README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
VZ Picky
========

VZ Picky will display a dropdown <select> list of all the unique values for a particular custom field. Perfect for advanced search forms! You can also use it as a tag pair to loop through the unique values and generate your own markup.
VZ Picky will display a dropdown `<select>` list of all the unique values for a particular custom field. Perfect for advanced search forms! You can also use it as a tag pair to loop through the unique values and generate your own markup.

Usage
-----
Expand Down Expand Up @@ -34,9 +34,11 @@ Optional parameters

`sort="alpha"` - Sort the values alphabetically. If this is not set, they will be displayed in the order they appear in the database.

`status` - Only use values from entries with a particular status (or statuses). Uses the [same syntax as the channel:entries tag](https://docs.expressionengine.com/v2/add-ons/channel/channel_entries.html#status). Defaults to `open`.

`site_id` - On a multi-site installation, the id of the site to use. Defaults to 1.

#### The following parameters only apply to the single-tag usage:
### The following parameters only apply to the single-tag usage:

`value` - Value to pre-select.

Expand Down
18 changes: 18 additions & 0 deletions addon.setup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/**
* VZ Picky Add-On Setup File
*
* @author Eli Van Zoeren <eli@elivz.com>
* @copyright Copyright (c) 2017 Eli Van Zoeren
* @license http://opensource.org/licenses/MIT
*/

return array(
'author' => 'Eli Van Zoeren',
'author_url' => 'http://elivz.com',
'name' => 'VZ Picky',
'description' => 'Generates an option tag for each unique entry in a particular custom field.',
'version' => '1.3.0',
'namespace' => 'Vz\Picky'
);
97 changes: 72 additions & 25 deletions vz_picky/pi.vz_picky.php → pi.vz_picky.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* VZ Picky Plugin
*
* @author Eli Van Zoeren <eli@elivz.com>
* @copyright Copyright (c) 2011 Eli Van Zoeren
* @license http://creativecommons.org/licenses/by-sa/3.0/ Attribution-Share Alike 3.0 Unported
*
* @package ExpressionEngine
* @subpackage Addons
* @category Plugin
* @author Eli Van Zoeren
* @link http://elivz.com
*/


$plugin_info = array(
'pi_name' => 'VZ Picky',
'pi_version' => '1.2.0',
'pi_version' => '1.3.0',
'pi_author' => 'Eli Van Zoeren',
'pi_author_url' => 'https://github.com/elivz/vz_picky.ee_addon',
'pi_description' => 'Generates an option tag for each unique entry in a particular custom field.',
'pi_usage' => Vz_picky::usage()
);

class Vz_picky {

public $return_data = "";
class Vz_picky
{
/**
* Template output
* @var string
*/
public $return_data;

/**
* Constructor
*/
* Main template tag
*
* @access public
* @return void
*/
public function __construct()
{
// Store the template parameters
Expand All @@ -35,6 +45,7 @@ public function __construct()
$hide_placeholder = ee()->TMPL->fetch_param('hide_placeholder');
$site_id = ee()->TMPL->fetch_param('site_id', '1');
$selected = ee()->TMPL->fetch_param('value');
$status = ee()->TMPL->fetch_param('status');
$sort = ee()->TMPL->fetch_param('sort');
$sep = ee()->TMPL->fetch_param('separator');

Expand All @@ -44,7 +55,7 @@ public function __construct()
'exp_channel_fields',
array(
'field_name' => $field,
'site_id' => $site_id
'site_id' => $site_id,
),
1
);
Expand All @@ -56,18 +67,41 @@ public function __construct()
}
else
{
$this->return_data = "Field was not found";
$this->return_data = '';
return;
}

// Handle status parameter
if ($status)
{
$status = str_replace('Open', 'open', $status);
$status = str_replace('Closed', 'closed', $status);

$status = ee()->functions->sql_andor_string($status, 'exp_channel_titles.status');
$status = str_replace('AND', '', $status);

if (stristr($status, "'closed'") === FALSE)
{
$status .= "exp_channel_titles.status != 'closed'";
}
}
else
{
$status = "exp_channel_titles.status = 'open'";
}

// Get all the values from the database
$query = ee()->db->select($field_column)
->distinct()
->get('exp_channel_data');
ee()->db->select($field_column);
ee()->db->distinct();
ee()->db->from('exp_channel_titles');
ee()->db->join('exp_channel_data', 'exp_channel_data.entry_id = exp_channel_titles.entry_id');
ee()->db->where($status, NULL, FALSE);

$result = ee()->db->get();

// Collect the values
$values = array();
foreach ($query->result_array() as $row)
foreach ($result->result_array() as $row)
{
if ($row[$field_column] != '')
{
Expand Down Expand Up @@ -122,48 +156,60 @@ public function __construct()
foreach ($values as $value)
{
$output .= ($value == $selected) ? '<option selected="selected">' : '<option>';
$output .= $value.'</option>';
$output .= strip_tags($value);
$output .= '</option>';
}
print($output); die();

$this->return_data = $output;
}
}

// --------------------------------------------------------------------

/**
* Usage
*
* This function describes how the plugin is used.
*
* @access public
* @return string
*/
public static function usage()
{
ob_start();
?>
ob_start(); ?>

VZ Picky will display a dropdown <select> list of all the unique values for a particular custom field. Perfect for advanced search forms! You can also use it as a tag pair to loop through the unique values and generate your own markup.

USAGE

Single tag
---------------------------

<select>{exp:vz_picky field="field_name"}</select>

Outputs a list of <option> elements containing each unique value in the custom field.

Tag pair
---------------------------

{exp:vz_picky field="field_name"}
{value}<br/>
{value}<br/>
{/exp:vz_picky}

Loops through each unique value in the field.

REQUIRED PARAMETER
Required Parameter
---------------------------

field - The short name of the custom field to pull values from.

OPTIONAL PARAMETERS
Optional Parameters
---------------------------

separator - You can further break apart each value using this parameter. If a field contains a comma-separated list of tags, for instance, this will list each tag as its own option

sort="alpha" - Sort the values alphabetically. If this is not set, they will be displayed in the order they appear in the database.

status - Only use values from entries with a particular status (or statuses). Uses the <a href="https://docs.expressionengine.com/v2/add-ons/channel/channel_entries.html#status">same syntax as the channel:entries tag</a>. Defaults to 'open'.

site_id - On a multi-site installation, the id of the site to use. Defaults to 1.

The following parameters only apply to the single-tag usage:
Expand All @@ -177,6 +223,7 @@ public static function usage()
<?php
$buffer = ob_get_contents();
ob_end_clean();

return $buffer;
}

Expand Down

0 comments on commit 0904319

Please sign in to comment.