Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

New plugin: SmartyColumnSort!
Goto page Previous  1, 2, 3, 4, 5, 6  Next
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins
View previous topic :: View next topic  

How useful is SmartyColumnSort?
Not at all
6%
 6%  [ 1 ]
It's ok
6%
 6%  [ 1 ]
Quite useful!
13%
 13%  [ 2 ]
Most useful!!
73%
 73%  [ 11 ]
Total Votes : 15

Author Message
gazoot
Smarty Regular


Joined: 20 Feb 2005
Posts: 35

PostPosted: Thu Dec 29, 2005 12:12 am    Post subject: Reply with quote

Again, thanks for the followup for the new version. I fixed everything you commented on, except for a logical error in the strpos fix: Since strpos() can return both FALSE and 0, it is a mistake to use loose comparison because it will be treated as false even if found (when it returns 0 for first position in string). So you need the strict comparison, and so the fix will be:

[php:1:23fca1f697]if(strpos($url, '&') !== FALSE)
{
$url = str_replace('&', '& amp;', $url);
}[/php:1:23fca1f697]
I hope SmartyColumnSort is getting good enough for a version 1.0 release! What do you think?

/Gazoot
Back to top
View user's profile Send private message
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Thu Dec 29, 2005 9:37 pm    Post subject: Reply with quote

Ok I see what you are saying... the strpos could return a 0 if the & was the first character of the string, and the If statement in PHP evaluates 0's as false...

After thinking about it though, is it even neccessary in this case to perform a conditional?

Code:

   $url = str_replace('&', '& amp;', $url);


Wouldn't this alone work? If there are no ampersands it essentially becomes $url = $url right? But now I'm just nit-picking Smile

But yeah I think you're getting a good following here, and it has been working for me perfectly ever since I implemented it.

The main thing on my wishlist now is to have the class utilize ID's for multiple sort tables on on page.. What I'm thinking about is possibly having it ride on the Session array that SmartyPaginate is using... This way I could actually have multiple sortable lists on one page =)

Anyway happy Holidays Smile

Owen
Back to top
View user's profile Send private message Visit poster's website
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Fri Jan 13, 2006 10:19 pm    Post subject: My mods to this class Reply with quote

Hello everyone, all the way back since page one of this thread, I have been asking for functionality simliar to SmartyPaginate where everything is saved into the session and each ColumnSort can get it's own ID so that you can have multiple columnsorts on one page.

Well guess what I have went ahead and did just that! I have only tested it on PHP 5.1 but I'm pretty sure it will work for PHP 4+

I have also removed some unneccessary methods... I didn't see a reason for some of the getters and setters that were in there, and seeing as how they were undocumented it looks like they were just auto-created... I guess if someone was using them just post here about it.

Oh as an added benefit, the user's sorted columns will be saved in that state until you destroy the session. So if someone likes to see something ascending, well then let it ascend!

To reduce any confusion I have incremented the version number to .0935-dev

Basically to use it, you only need to add an $id onto the end of each of the calls to it... I have also set it up so that it does not require the $id to be set, just in the spirit of backwards compatibility.

For example :
Code:

$myID = "Customers";
$columns = array(); //Fill in the blanks
$columnsort = new SmartyColumnSort($columns, $myID);
$columnsort->setDefault($defaultcolumn, $defaultorder, $myID);
$columnsort->sortOrder(false, $myID);


SmartyColumnSort.class.php
[php:1:811c266172]
<?php
/**
* Project: SmartyColumnSort: Auto-sorting of table columns for the Smarty Template Engine
* File: SmartyColumnSort.class.php
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @copyright 2005 Andreas Söderlund
* @author Andreas Söderlund <gazoot [at] home [dot] se>
* @author Owen Cole <owenc [at] totalsales [dot] com>
* @package SmartyColumnSort
* @version 0.935-dev
*/
class SmartyColumnSort {
/**
* Class Constructor
* @param array $column_array An optional database column array, can also be supplied later using {@link SmartyColumnSort::setColumns()}.
* @param String $id (optional) A string to identify this column sorter object
* @author GET var patches by TGKnIght and jchum on the Smarty forum
*/
function SmartyColumnSort($column_array = NULL, $id = 'default') {
//Only setup the variables if they do not already exist or
//the $id wasn't set and is left as default
if((!isset($_SESSION['SmartyColumnSort'][$id]) || $id == 'default') && $column_array) {
$this->defaultSettings($column_array, $id);
}

$_SESSION['SmartyColumnSort'][$id]['target_page'] = $this->nextURL($id);
}

/**
* This method generates the next URL to be used as links
* @param String $id (optional) A string to identify this column sorter object
* @return String The prepared URL
*/
function nextURL ($id = 'default') {
$nexturl = $_SERVER['PHP_SELF'].'?';
foreach($_GET as $key => $value) {
if($key != $_SESSION['SmartyColumnSort'][$id]['column_var'] && $key != $_SESSION['SmartyColumnSort'][$id]['sort_var']) {
if(is_array($value)) {
foreach($value AS $v) {
$nexturl .= $key.'[]='.$v.'&';
}
} else {
$nexturl .= $key.'='.$value.'&';
}
}
}
return substr($nexturl, 0, -1);
}

/**
* This method allows the session variables for a particular ID to be reset to default
* This method will be useful to call if the user wants to reset his columnsort view
* @param Array $column_array Holds all the columns that will be sorted
* @param String $id The id of the columnsort that will be set to default
*/
function defaultSettings ($column_array, $id = 'default') {
$_SESSION['SmartyColumnSort'][$id]['column_var'] = $id.'Col';
$_SESSION['SmartyColumnSort'][$id]['sort_var'] = $id.'Sort';

//Assign the columns and set the default as the first column
$this->setColumns($column_array, $id);
$this->setDefault($column_array[0], 'asc', $id);

$_SESSION['SmartyColumnSort'][$id]['current_column'] = $_SESSION['SmartyColumnSort'][$id]['default_column'];
$_SESSION['SmartyColumnSort'][$id]['current_sort'] = $_SESSION['SmartyColumnSort'][$id]['default_sort'];
}

/**
* Set the default column and sort order for a column set.
*
* The column name must exist in the default column before default is set, so {@link SmartyColumnSort::setColumns()} must be set
* before this function is called.
*
* @param string $column A column name.
* @param string $sort_order Default sort order for columns. Can be either "asc" or "desc".
* @param String $id (optional) A string to identify this column sorter object
* @return bool TRUE if default was set, FALSE if not.
*/
function setDefault($column, $sort_order, $id = 'default') {
if(!is_array($_SESSION['SmartyColumnSort'][$id]['column_array'])) {
trigger_error('setColumns() must be called before setDefault()', E_USER_ERROR);
return FALSE;
}

$_SESSION['SmartyColumnSort'][$id]['default_column'] = NULL;
$_SESSION['SmartyColumnSort'][$id]['default_sort'] = NULL;

// Search for column id and set the key value.
foreach($_SESSION['SmartyColumnSort'][$id]['column_array'] as $key => $value) {
if(is_array($value)) $value = $value[0];

if($column == $value) {
$_SESSION['SmartyColumnSort'][$id]['default_column'] = $key;
break;
}
}

if($_SESSION['SmartyColumnSort'][$id]['default_column'] === NULL) {
trigger_error("column '$column' not found in column array!", E_USER_ERROR);
return FALSE;
}

$sort_order = strtolower($sort_order);
if($sort_order != 'asc' && $sort_order != 'desc') {
trigger_error('sort_order must be "asc" or "desc"', E_USER_ERROR);
return FALSE;
}

$_SESSION['SmartyColumnSort'][$id]['default_sort'] = $sort_order;

return TRUE;
}

/**
* Set the default GET var for sorting.
*
* @param string $column The GET var for column id
* @param string $sort The GET var for sort values
* @param String $id (optional) A string to identify this column sorter object
*/
function setVars($column, $sort, $id = 'default') {
$_SESSION['SmartyColumnSort'][$id]['column_var'] = $column;
$_SESSION['SmartyColumnSort'][$id]['sort_var'] = $sort;
}

/**
* Set the column array for the table.
*
* The column array is used to assign a column id to the links in the template, and
* to the database fields. The array has the following format:
* <code>
* $array = array('db_field', 'db_field2', array('db_field3', 'asc'));
* $columnsort->setColumns($array, $id);
* </code>
* Afterwards, set default field and order with the {@link SmartyColumnSort::setDefault()} function.
*
* @param array $array The array to use.
* @param String $id (optional) A string to identify this column sorter object
*/
function setColumns($array, $id = 'default') {
$_SESSION['SmartyColumnSort'][$id]['column_array'] = $array;
}

/**
* Set the target page for the column URLs.
*
* @param string $page A link to another page. The column and sort GET vars will be appended to this link.
* @param String $id (optional) A string to identify this column sorter object
*/
function setTargetPage($page, $id = 'default') {
$_SESSION['SmartyColumnSort'][$id]['target_page'] = $page;
}

/**
* Return the current sort order
*
* If no value exists (no GET vars), the default value from setDefault() is used.
* @param bool $halt_on_error (optional) If TRUE, function returns FALSE in case of illegal get vars. If FALSE, just return default value.
* @param String $id (optional) A string to identify this column sorter object
* @return mixed the current sort value.
*/
function sortOrder($halt_on_error = FALSE, $id = 'default') {
$sort = $this->_createFromGet($halt_on_error, $id);

if($halt_on_error && $sort === FALSE)
return FALSE;
else
return $sort;
}

/**
* Create sort query to be used in DB.
*
* @param bool $halt_on_error If TRUE, function returns FALSE in case of illegal get vars. If FALSE, just return default value.
* @param String $id (optional) A string to identify this column sorter object
*/
function _createFromGet($halt_on_error, $id = 'default') {

if(!isset($_SESSION['SmartyColumnSort'][$id]['default_column'])) trigger_error('No default column set!', E_USER_ERROR);
else if(!$_SESSION['SmartyColumnSort'][$id]['default_sort']) trigger_error('No default sort set!', E_USER_ERROR);

// If both GET vars exists, fetch them.
if(isset($_GET[$_SESSION['SmartyColumnSort'][$id]['column_var']]) && isset($_GET[$_SESSION['SmartyColumnSort'][$id]['sort_var']])) {

$_SESSION['SmartyColumnSort'][$id]['current_column'] = $_GET[$_SESSION['SmartyColumnSort'][$id]['column_var']];
$_SESSION['SmartyColumnSort'][$id]['current_sort'] = strtoupper($_GET[$_SESSION['SmartyColumnSort'][$id]['sort_var']]);

$column = $_SESSION['SmartyColumnSort'][$id]['current_column'];
$sort = $_SESSION['SmartyColumnSort'][$id]['current_sort'];

$error = FALSE;

if(!is_numeric($column)) $error = TRUE;
else if($sort != 'DESC' && $sort != 'ASC') $error = TRUE;

if($error) {
if($halt_on_error) {
return FALSE;
} else {
$column = $_SESSION['SmartyColumnSort'][$id]['default_column'];
$sort = strtoupper($_SESSION['SmartyColumnSort'][$id]['default_sort']);
}
}
} else {
if($halt_on_error && (isset($_GET[$_SESSION['SmartyColumnSort'][$id]['column_var']]) || isset($_GET[$_SESSION['SmartyColumnSort'][$id]['sort_var']])))
return FALSE;

$column = $_SESSION['SmartyColumnSort'][$id]['current_column'];
$sort = strtoupper($_SESSION['SmartyColumnSort'][$id]['current_sort']);
}

// Translate get var to DB field name.
if(!($column = $this->_translateColumns($column, $id))) {
if($halt_on_error) {
return FALSE;
} else {
$column = $this->_translateColumns($_SESSION['SmartyColumnSort'][$id]['default_column'], $id);
$sort = strtoupper($_SESSION['SmartyColumnSort'][$id]['default_sort']);
}
}

return $column.' '.$sort;
}

/**
* @todo Add explanation of what this really does
* @param String $id (optional) A string to identify this column sorter object
*/
function _translateColumns($columnid, $id = 'default') {
$array = &$_SESSION['SmartyColumnSort'][$id]['column_array'];

if(!isset($array[$columnid])) return FALSE;

$value = $array[$columnid];

// If value is an array, translated value is in 0, sort order in 1.
if(!is_array($value)) return $value;
return $value[0];
}
}
?>
[/php:1:811c266172]

function.columnsort.php
[php:1:811c266172]
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/

/**
* Smarty {columnsort} function plugin
*
* Type: function
* Name: columnsort
* Purpose: easy sorting of a html table by columns
* @param array parameters (cid, html, selected_class, id, asc_image, desc_image)
* @param Smarty
* @return string|null
* @author XML compliance patch by Vaccafoeda on the Smarty forum
* @author TGKnIght - Modified to allow for unique ids and store in $_SESSION scope
*/
function smarty_function_columnsort($params, &$smarty) {
static $selected_class = NULL;
static $current_id = 0;

static $sort_asc_image = NULL;
static $sort_desc_image = NULL;

static $SMCS_id = 'default';

if(isset($params['cid'])) {
$SMCS_id = $params['cid'];
}

// Retrieve the $_SESSION columnsort object.
if(!isset($_SESSION['SmartyColumnSort'][$SMCS_id])) {
$smarty->trigger_error('columnsort: SmartyColumnSort.class.php needs to be included for columnsort to work.');
return;
}
$columnsort = $_SESSION['SmartyColumnSort'][$SMCS_id];

// HTML
if(!isset($params['html'])) {
$smarty->trigger_error("columnsort: missing 'html' parameter.");
return;
}
$html = $params['html'];

// selected_class
if(isset($params['selected_class'])) {
$selected_class = $params['selected_class'];
}

// ID for column table
if(isset($params['id'])) {

$id = $params['id'];

// Increase current id with 1 to prepare for next value
$current_id = $id + 1;
} else {
$id = $current_id++;
}

if(isset($params['asc_image']) && isset($params['desc_image'])) {
// Set asc and desc sort images (will be placed after the sorted column)
$sort_asc_image = $params['asc_image'];
$sort_desc_image = $params['desc_image'];
} else if(isset($params['asc_image']) || isset($params['desc_image'])) {
$smarty->trigger_error('columnsort: Both "asc_image" and "desc_image" needs to be present, or none of them.');
}

// Get current sort order for current column id
$sort_order = _smarty_columnsort_sort_order($id, $columnsort['column_array'], $columnsort['default_sort'], $smarty);

if($sort_order === FALSE) {
$smarty->trigger_error('columnsort: too few columns in translate table!');
return;
}

// The column is selected if the get vars exists and is the current column OR
// if the get vars does not exist and the current column is default.
if($columnsort['current_column'] !== NULL && $columnsort['current_column'] == $id) {
$selected = TRUE;

// Reverse sort order for the output.
if($columnsort['current_sort'])
$sort_order = strtolower($columnsort['current_sort']) == 'asc' ? 'desc' : 'asc';
} else if($columnsort['current_column'] === NULL && $id == $columnsort['default_column']) {
$selected = TRUE;

// Reverse sort order for the output.
$sort_order = $sort_order == 'asc' ? 'desc' : 'asc';
} else {
$selected = FALSE;
}

$columnsort['target_page'] .= (strpos($columnsort['target_page'], '?') !== FALSE ? '&' : '?');

$url = $columnsort['target_page'] . $columnsort['column_var'] . "=$id&" . $columnsort['sort_var'] . "=$sort_order";

// XML compliance patch by Vaccafoeda
$url = str_replace('&', '&', $url);

$class = $selected && $selected_class ? "class=\"$selected_class\" " : '';

// If asc/desc image exists, append it.
if($selected && $sort_asc_image !== NULL) {
$image_src = $sort_order == 'asc' ? $sort_desc_image : $sort_asc_image;
$image = " <img src=\"$image_src\">";
} else {
$image = "";
}

return "<a {$class}href=\"$url\">$html</a>$image";
}

function _smarty_columnsort_sort_order($id, $columns, $default_sort, &$smarty) {
if(!isset($columns[$id])) return FALSE;

if(!is_array($columns[$id])) return $default_sort;

if(count($columns[$id]) != 2) {
$smarty->trigger_error('columnsort: column array must be array("value", "asc|desc")');
return FALSE;
}

return $columns[$id][1];
}
?>
[/php:1:811c266172]


Last edited by TGKnIght on Thu Jan 19, 2006 8:02 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Thu Jan 19, 2006 7:51 pm    Post subject: Reply with quote

I forgot to mention that if you are using ID's for multiple column sorts in your template you will also need to specify the same ID, so that the plugin will know which set of columns you are talking about.

Unfortunately the attribute "ID" was already taken for the plugin (AFAIK if you use the ID attribute it allows you to override the order the columns are displayed), so it is called "cid" for "column id" Smile

You first columnsort tag should look somethign like this :
Code:

{columnsort cid=$myID selected_class="SelectedHeader" html="ID" asc_image="`$Site.image_path`/arrowup.gif" desc_image="`$Site.image_path`/arrowdown.gif"}


Subsequent {columnsort} tags only need to be like this :
Code:

{columnsort html="Name"}
{columnsort html="City"}

_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template


Last edited by TGKnIght on Thu Feb 02, 2006 7:46 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
reynier
Smarty Pro


Joined: 12 Apr 2005
Posts: 104
Location: Cuba

PostPosted: Wed Feb 01, 2006 3:32 am    Post subject: Three questions Reply with quote

I have three questions about Plugins and use:
1) If I use SmartyPaginate then I can use SmartyColumnSort?
2) Why you don't publish a simply tutorial for explain how to works with plugin? I read the entire post and not know yet how works with this.
3) From where I can download it?

Best
_________________
ReynierPM
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Wed Feb 01, 2006 2:57 pm    Post subject: Re: Three questions Reply with quote

reynier wrote:

1) If I use SmartyPaginate then I can use SmartyColumnSort?


Yes, I am currently using them both together.

reynier wrote:

2) Why you don't publish a simply tutorial for explain how to works withplugin? I read the entire post and not know yet how works with this.


I would suggest reading Gazoot's first post in this thread which gives a good example of how to use the plugin, also read my last two posts, where I have posted examples. If you are still having problems, please give details as to where you are stuck.

reynier wrote:

3) From where I can download it?


You can either use Gazoot's latest version (he posted a link near the beginning of this thread) which does not save anything to $_SESSION or copy and paste my code from 2 posts above which saves all your column sorts to session and allows for unique id's for each table... I just use the same ID as the SmartyPaginate ID.
_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template
Back to top
View user's profile Send private message Visit poster's website
bob88
Smarty n00b


Joined: 02 Feb 2006
Posts: 3

PostPosted: Thu Feb 02, 2006 2:00 pm    Post subject: Reply with quote

Hi

I have a problem, and i don't understand the problem...

here my code in .php, something like that :
[php]$myID = "users";
$columns = array('login', 'status', 'email', 'language', 'subcription_date');
$columnsort = new SmartyColumnSort($columns, $myID);
$columnsort-&gt;sortOrder(false,$myID);[/php]

and here my .tpl:
Code:
&lt;th&gt;{columnsort cid=$PaginateID selected_class=&quot;selected&quot; html=&quot;login&quot;} &lt;/th&gt;


1st question : What is cid ? and $PaginateID ? in your example (and in mine).
2nd : $myID can be anything ?
3rd : The Big error : I have an error when i display the tpl :
Quote:
Warning: Smarty error: columnsort: SmartyColumnSort.class.php needs to be included for columnsort to work. in C:\Xampp\xampp\php\pear\smarty\libs\Smarty.class.php on line 1095

I don't understand why, SmartyColumnSort.class.php is well included in my .php, "columnsort" is good too, but it doesn't works... (i would try the gazoot version, but the links are dead)

That's all, i think, this plugin looks very good et very useful, so i hope it will work soon in my application.

Thanks for your answers, and sorry for my bad english.


Last edited by bob88 on Wed May 05, 2010 10:55 pm; edited 1 time in total
Back to top
View user's profile Send private message
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Thu Feb 02, 2006 7:41 pm    Post subject: Reply with quote

bob88 wrote:

1st question : What is cid ? and $PaginateID ? in your example (and in mine).


Sorry for my unclear example... I use SmartyColumnSort & SmartyPaginate together.. So I actually just assign a variable called $PaginateID that I end up using in both.

These are just names that are used to uniquely identify each "datatable" that you are displaying. If you would like to save the user's sorting preferences into $_SESSION you will need to use these. You DO have the option to omit these, but no data will be saved once you leave the sorted page. This applies to both ColumnSort & Paginate.

The reason it is called "cid" in the {columnsort} tag is because "ID" was already used Sad

bob88 wrote:

2nd : $myID can be anything ?


Yes, anything alpha-numeric should be safe.

bob88 wrote:

3rd : The Big error : I have an error when i display the tpl :
Quote:
Warning: Smarty error: columnsort: SmartyColumnSort.class.php needs to be included for columnsort to work. in C:\Xampp\xampp\php\pear\smarty\libs\Smarty.class.php on line 1095



Sorry my example did not include the include statement.

Try adding this before everything else. (You will need to modify the path to match your setup)

Code:

include_once("smarty/SmartyColumnSort.class.php");

_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template
Back to top
View user's profile Send private message Visit poster's website
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Thu Feb 02, 2006 7:45 pm    Post subject: Reply with quote

Gazoot's version link for those who are interested :

http://hem.passagen.se/gazoot/scs/smartycolumnsort.zip
_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template
Back to top
View user's profile Send private message Visit poster's website
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Thu Feb 02, 2006 7:48 pm    Post subject: New example for my version Reply with quote

[php:1:99a2eb33eb]
include_once("smarty/SmartyColumnSort.class.php");

$myID = "users";
$columns = array('login', 'status', 'email', 'language', 'subcription_date');
$columnsort = new SmartyColumnSort($columns, $myID);
$columnsort->sortOrder(false,$myID);

$smarty->assign('myID', $myID);
[/php:1:99a2eb33eb]

Code:

{columnsort cid=$myID selected_class="SelectedHeader" html="ID" asc_image="`$Site.image_path`/arrowup.gif" desc_image="`$Site.image_path`/arrowdown.gif"}
{columnsort html="Name"}
{columnsort html="City"}

_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template
Back to top
View user's profile Send private message Visit poster's website
bob88
Smarty n00b


Joined: 02 Feb 2006
Posts: 3

PostPosted: Fri Feb 03, 2006 10:43 am    Post subject: Reply with quote

It's working ! Excellent, thanks a lot !

My problem was here, i think :
[php]$smarty-&gt;assign('myID', $myID);[/php]

So, now it's ok, for who want, this is my code with TGKnIght code version, and i'm using Pear DB:

list.php
[php]require_once('Smarty.class.php');
require_once('SmartyColumnSort.class.php'); //Tri de colonne


$tpl = new Smarty;

//Sort
$myID = "users";
$columns = array('login', 'status', 'email', 'language');
$columnsort = new SmartyColumnSort($columns, $myID);
$columnsort-&gt;setDefault('login', 'asc', $myID);
$order = $columnsort-&gt;sortOrder(false,$myID);
$tpl-&gt;assign('myID', $myID);

//DB
$db = DB::connect($dsn);
$query = 'SELECT * FROM users ORDER BY '.$order;
$result = $db-&gt;getAll($query, array(), DB_FETCHMODE_ASSOC);
$tpl-&gt;assign('user_list', $result);

$tpl-&gt;display('list.tpl');[/php]

and list.tpl

Code:
&lt;table style=&quot;border: solid 2px #000000;&quot;&gt;
   &lt;tr&gt;
      &lt;th&gt;&lt;br /&gt;&lt;/td&gt;
      &lt;th&gt;{columnsort cid=$myID selected_class=&quot;selected&quot; html=&quot;login&quot;}&lt;/th&gt;
      &lt;th&gt;{columnsort html=&quot;id_status&quot;}&lt;/th&gt;
      &lt;th&gt;{columnsort html=&quot;email&quot;}&lt;/th&gt;
      &lt;th&gt;{columnsort html=&quot;language&quot;}&lt;/th&gt;
   &lt;/tr&gt;
{section name=&quot;i&quot; loop=$user_list}   
   &lt;tr style=&quot;background-color:{cycle values='#eeeeee,#d0d0d0'};&quot;&gt;
      &lt;td&gt;{$user_list[i].login}&lt;/td&gt;
      &lt;td&gt;{$user_list[i].status}&lt;/td&gt;
      &lt;td&gt;{$user_list[i].email}&lt;/td&gt;
      &lt;td&gt;{$user_list[i].language}&lt;/td&gt;
   &lt;/tr&gt;
{sectionelse}
   &lt;tr&gt;
      &lt;td colspan=&quot;4&quot;&gt;Nothing&lt;/td&gt;
   &lt;/tr&gt;
&lt;/table&gt;
{/section} 


Ok, i have one other question...
1/ I'm using SmartyPaginate too, so :
TGKnIght wrote:
bob88 wrote:

1st question : What is cid ? and $PaginateID ? in your example (and in mine).

Sorry for my unclear example... I use SmartyColumnSort & SmartyPaginate together.. So I actually just assign a variable called $PaginateID that I end up using in both.

$PaginateID can be anything, or what value is ?


Ok, that's all for this time, thanks again to you and to Gazoot !


Last edited by bob88 on Wed May 05, 2010 10:55 pm; edited 1 time in total
Back to top
View user's profile Send private message
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Fri Feb 03, 2006 2:42 pm    Post subject: Reply with quote

Glad you got it working Smile

bob88 wrote:

$PaginateID can be anything, or what value is ?


Yes both the ID you pass into SmartyPaginate, and SmartyColumnSort can be alphanumeric strings.

Generally I just name it the same as however you would describe your data table.

i.e. If it is a Customer list... $PaginateID = 'Customer' or 'CustomerList1' Whatever you feel comfortable with.. If you do a print_r($_SESSION); you should see an associative array called SmartyPaginate and one called SmartyColumnSort which should have whatever ID you choose as the key, if you have been passing ID's to either one.

Please realize in the examples I have been using $PaginateID & $myID interchangeably... They both represent the same variable in your finished product.
_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template
Back to top
View user's profile Send private message Visit poster's website
reynier
Smarty Pro


Joined: 12 Apr 2005
Posts: 104
Location: Cuba

PostPosted: Sat Feb 11, 2006 5:27 pm    Post subject: Amazing ... Reply with quote

Thanks for every who develop this plugin, really is amazing. I made some changes to function.columnsort.php. If any is interested:
* Add a alt attribute for img element:
* Add two DIV one for align html attribute to left and other for align im to right.
* Make img XHTML valid element
This are the changes:
[php:1:5e1d83e442]
// alt for image
if(isset($params['img_alt'])) {
$img_alt = $params['img_alt'];
}

// If asc/desc image exists, append it.
if($selected && $sort_asc_image !== NULL) {
$image_src = $sort_order == 'asc' ? $sort_desc_image : $sort_asc_image;
$image = " <img src=\"$image_src\" alt=\"$img_alt\" />";
} else {
$image = "";
}

return "<div style=\"float:left;\"><a {$class}href=\"$url\">$html</a></div> <div style=\"float: right;\"><a {$class}href=\"$url\">$image</a></div>";
[/php:1:5e1d83e442]

Best,
_________________
ReynierPM
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Wed Feb 22, 2006 8:43 pm    Post subject: Reply with quote

Hi Reynier, thanks for your contribution to this plugin... I have been incredibly busy at work, but still plan on eventually incorporating all your changes into a new version of the plugin..

Things that need to be done :

    Create updated version of plugin with appropriate user submitted changes
    Add the plugin to the Smarty Wiki
    Create a walkthrough with the new functionality for saving data to $_SESSION
    Create a walkthrough for using SmartyColumnSort & SmartyPaginate for the same dataset

_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template
Back to top
View user's profile Send private message Visit poster's website
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Fri Feb 24, 2006 3:55 pm    Post subject: Reply with quote

Updated plugin code to reset counter if a new ID is passed in.. this allows for multiple columnsorts to be used on the same page.

Code:

   if(isset($params['cid'])) {
        if ($SMCS_id != $params['cid']) {
            $current_id = 0;
        }

      $SMCS_id = $params['cid'];
   }


I have also changed the img so that it is XHTML compliant and allows for the alt tag to be entered...

I did not incorporate the <divs> to separate the text and the img... I wanted to see other people's opinion on this...

I don't know how everyone else uses the SelectedHeader class in their CSS, but I outline mine to make it stand out... It doesn't look right with the 2 seperate links and divs...

Code:

<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * Smarty {columnsort} function plugin
 *
 * Type:     function
 * Name:     columnsort
 * Purpose:  easy sorting of a html table by columns
 * @param array parameters (cid, html, selected_class, id, asc_image, desc_image)
 * @param Smarty
 * @return string|null
 * @author XML compliance patch by Vaccafoeda on the Smarty forum
 * @author TGKnIght - Modified to allow for unique ids and store in $_SESSION scope
 */
function smarty_function_columnsort($params, &$smarty) {
   static $selected_class = NULL;
   static $current_id = 0;

   static $sort_asc_image = NULL;
   static $sort_desc_image = NULL;

   static $SMCS_id = 'default';

   if(isset($params['cid'])) {
        if ($SMCS_id != $params['cid']) {
            $current_id = 0;
        }

      $SMCS_id = $params['cid'];
   }

   // Retrieve the $_SESSION columnsort object.
   if(!isset($_SESSION['SmartyColumnSort'][$SMCS_id])) {
      $smarty->trigger_error('columnsort: SmartyColumnSort.class.php needs to be included for columnsort to work.');
      return;
   }
   $columnsort =& $_SESSION['SmartyColumnSort'][$SMCS_id];

   // HTML
   if(!isset($params['html'])) {
      $smarty->trigger_error("columnsort: missing 'html' parameter.");
      return;
   }
   $html = $params['html'];
   
   // selected_class
   if(isset($params['selected_class'])) {
      $selected_class = $params['selected_class'];
   }

   // ID for column table
   if(isset($params['id'])) {
      $id = $params['id'];

      // Increase current id with 1 to prepare for next value
      $current_id = $id + 1;
   } else {
      $id = $current_id++;
   }

   if(isset($params['asc_image']) && isset($params['desc_image'])) {
      // Set asc and desc sort images (will be placed after the sorted column)
      $sort_asc_image = $params['asc_image'];
      $sort_desc_image = $params['desc_image'];
   } else if(isset($params['asc_image']) || isset($params['desc_image']))    {
      $smarty->trigger_error('columnsort: Both "asc_image" and "desc_image" needs to be present, or none of them.');
   }

    // alt for image
    if(isset($params['img_alt'])) {
        $img_alt = $params['img_alt'];
    }

   // Get current sort order for current column id
   $sort_order = _smarty_columnsort_sort_order($id, $columnsort['column_array'], $columnsort['default_sort'], $smarty);

   if($sort_order === FALSE) {
      $smarty->trigger_error('columnsort: too few columns in translate table!');
      return;
   }


   // The column is selected if the get vars exists and is the current column OR
   // if the get vars does not exist and the current column is default.
   if($columnsort['current_column'] !== NULL && $columnsort['current_column'] == $id) {
      $selected = TRUE;

      // Reverse sort order for the output.
      if($columnsort['current_sort'])
         $sort_order = strtolower($columnsort['current_sort']) == 'asc' ? 'desc' : 'asc';
   } else if($columnsort['current_column'] === NULL && $id == $columnsort['default_column']) {
      $selected = TRUE;

      // Reverse sort order for the output.
      $sort_order = $sort_order == 'asc' ? 'desc' : 'asc';
   } else {
      $selected = FALSE;
   }

   $columnsort['target_page'] .= (strpos($columnsort['target_page'], '?') !== FALSE ? '&' : '?');

   $url = $columnsort['target_page'] . $columnsort['column_var'] . "=$id&" . $columnsort['sort_var'] . "=$sort_order";

   // XML compliance patch by Vaccafoeda
   $url = str_replace('&', '&', $url);

   $class = $selected && $selected_class ? "class=\"$selected_class\"" : '';

    // If asc/desc image exists, append it.
    if($selected && $sort_asc_image !== NULL) {
        $image_src = $sort_order == 'asc' ? $sort_desc_image : $sort_asc_image;
        $image = " <img src=\"$image_src\" alt=\"$img_alt\" />";
    } else {
        $image = "";
    }

   return "<a $class href=\"$url\">$html</a>$image";
}

function _smarty_columnsort_sort_order($id, $columns, $default_sort, &$smarty) {
   if(!isset($columns[$id])) return FALSE;

   if(!is_array($columns[$id])) return $default_sort;

   if(count($columns[$id]) != 2) {
      $smarty->trigger_error('columnsort: column array must be array("value", "asc|desc")');
      return FALSE;
   }

   return $columns[$id][1];
}
?>

_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins All times are GMT
Goto page Previous  1, 2, 3, 4, 5, 6  Next
Page 3 of 6

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP