From 14691cd854be3e9ee272c9619a6368c83d72267b Mon Sep 17 00:00:00 2001 From: Antonio Gallo Date: Sat, 18 Feb 2012 00:07:50 +0000 Subject: upload new EasyGiant library and added added a new filter for the model name (part 2) --- h-source/Library/Helper/List.php | 259 ++++++++++++++++++++++++++++++--------- 1 file changed, 202 insertions(+), 57 deletions(-) (limited to 'h-source/Library/Helper/List.php') diff --git a/h-source/Library/Helper/List.php b/h-source/Library/Helper/List.php index 51ed09c..ba59643 100755 --- a/h-source/Library/Helper/List.php +++ b/h-source/Library/Helper/List.php @@ -22,9 +22,11 @@ if (!defined('EG')) die('Direct access not allowed!'); -//class to create the HTML for the view action +//class to create the HTML of a list of records class Helper_List extends Helper_Html { + private $__rowArray = array(); //the current associative array representing the database record + protected $_itemsList = array(); //2-dimensional associative array containing the list items //keys: type,table:field,controller/action,value protected $_head = array(); //2-dimensional array containing the head of the table @@ -32,28 +34,22 @@ class Helper_List extends Helper_Html { protected $_recordNumber = null; //number of records from the table - protected $_allowedItems = array('simpleLink','simpleText','delForm','editForm','associateForm','moveupForm','movedownForm','Form'); //type of items allowed + protected $_allowedItems = array('simpleLink','simpleText','delForm','editForm','associateForm','moveupForm','movedownForm','Form','ledit','link','text'); //type of items allowed + + //it can be: both, top, bottom, none + protected $_boundaries = 'none'; + //array of filters + protected $_filters = array(); + //set if the submit buttons have to be images or not (it can be yse or not) - public $submitImageType = 'no'; + public $submitImageType = 'yes'; //set the files of the images - public $submitImages = array( - 'edit' => null, - 'del' => null, - 'up' => null, - 'down' => null, - 'link' => null - ); + public $submitImages = array(); //set the titles of the input:submit - public $submitTitles = array( - 'edit' => 'edit the record', - 'del' => 'delete the record', - 'up' => 'move up the record', - 'down' => 'move down the record', - 'link' => 'associate the record' - ); + public $submitTitles = array(); //properties of columns public $colProperties = array(); @@ -63,13 +59,46 @@ class Helper_List extends Helper_Html { //$position: array. First element: page number, second element: number of pages public $position = array(); + + //instance of Lang_{language}_Generic + public $strings = null; + + //the url (controller/action) of the current page + public $url = null; + + public $pageArg = null; //the key of the viewArgs array representing the page number. $this->viewArgs[$this->pageArg] is set to 1 if $this->pageArg !== null - //it can be: both, top, bottom, none - protected $_boundaries = 'none'; + public function __construct() { + + //get the generic language class + $this->strings = Factory_Strings::generic(Params::$language); + + $baseUrl = 'http://'.DOMAIN_NAME.'/Public/Img/Icons/elementary_2_5/'; + + //set the files of the images + $this->submitImages = array( + 'up' => $baseUrl.'up.png', + 'down' => $baseUrl.'down.png', + 'edit' => $baseUrl.'edit.png', + 'del' => $baseUrl.'delete.png', + 'link' => $baseUrl.'link.png', + ); + + $this->submitTitles = array( + 'edit' => $this->strings->gtext('edit'), + 'del' => $this->strings->gtext('delete'), + 'up' => $this->strings->gtext('move up'), + 'down' => $this->strings->gtext('move down'), + 'link' => $this->strings->gtext('associate') + ); - public function build($identifierName = 'identifier') + } + + public function build($identifierName = 'identifier', $url = null, $pageArg = null) { $this->_identifierName = $identifierName; + $this->url = $url; + $this->pageArg = $pageArg; } public function setIdentifierName($identifierName) @@ -116,8 +145,24 @@ class Helper_List extends Helper_Html { } } + //set the filters + //$filters: array whose elements are the viewArgs to be used for the filters forms + public function setFilters($filters) + { + $this->_filters = $filters; + } - //$method to extract the field name from the $action string (;table:field;) + //check that the ViewArgs array is complete + public function checkViewArgs() + { + foreach ($this->_filters as $field) + { + if (!array_key_exists($field,$this->viewArgs) and strcmp($field,null) !== 0) return false; + } + return true; + } + + //$method to extract the field name from the $action string (;table:field;). Used for the head public function extractFieldName($string) { $string = str_replace(';','',$string); return $string; @@ -125,41 +170,50 @@ class Helper_List extends Helper_Html { //replace the strings wrapped by ; with their correspondent value taken by the $recordArray associative array (a row of the select query) public function replaceFields($string,$rowArray) { - $stringArray = explode(';',$string); - for ($i = 0; $i < count($stringArray); $i++) { - if (strstr($stringArray[$i],':') or strstr($stringArray[$i],'.')) { - if (strstr($stringArray[$i],':')) - { - $char = ':'; - } - else - { - $char = '.'; - } - //check if a function has been indicated - if (strstr($stringArray[$i],'|')) - { - //get the function - $firstArray = explode('|',$stringArray[$i]); - $func = $firstArray[0]; - //replace the fields - $temp = explode($char,$firstArray[1]); - $stringArray[$i] = $rowArray[$temp[0]][$temp[1]]; - - if (!function_exists($func)) { - throw new Exception('Error in '.__METHOD__.': function '.$func.' does not exists..'); - } - //apply the function - $stringArray[$i] = call_user_func($func,$stringArray[$i]); - } - else - { - $temp = explode($char,$stringArray[$i]); - $stringArray[$i] = $rowArray[$temp[0]][$temp[1]]; + $this->__rowArray = $rowArray; //used by the replaceField method + + $string = preg_replace_callback('/(\;)(.*?)(\;)/', 'Helper_List::replaceField' ,$string); + + return $string; + } + + //replace a single string wrapped by ; with its correspondent value taken by the $recordArray associative array (a row of the select query) + public function replaceField($match) + { + $string = $match[2]; + + if (strstr($string,':') or strstr($string,'.')) { + if (strstr($string,':')) + { + $char = ':'; + } + else + { + $char = '.'; + } + //check if a function has been indicated + if (strstr($string,'|')) + { + //get the function + $firstArray = explode('|',$string); + $func = $firstArray[0]; + //replace the fields + $temp = explode($char,$firstArray[1]); + $string = $this->__rowArray[$temp[0]][$temp[1]]; + + if (!function_exists($func)) { + throw new Exception('Error in '.__METHOD__.': function '.$func.' does not exists..'); } + //apply the function + $string = call_user_func($func,$string); + } + else + { + $temp = explode($char,$string); + $string = $this->__rowArray[$temp[0]][$temp[1]]; } } - return implode('',$stringArray); + return $string; } //call the method replaceFields upon the $items array using the associative array $rowArray @@ -204,9 +258,42 @@ class Helper_List extends Helper_Html { $count++; } + return $htmlHead; } + //method to create the HTML of the head of the table + public function createFilters() { + $htmlFilters = null; + + if (count($this->_filters) > 0) + { + if ($this->checkViewArgs()) + { + $count = 0; + foreach ($this->_head as $item) { + + $prop = $item['type']; + if (isset($this->colProperties[$count])) + { + $prop = $this->colProperties[$count]; + } + + $html = ' '; + if (isset($this->_filters[$count])) + { + $html = $this->filterForm($this->_filters[$count]); + } + $htmlFilters .= $this->wrapColumn($html,$prop); + + $count++; + } + } + } + + return $htmlFilters; + } + //create the HTML of a single row (values taken from the associative array $rowArray) public function getRowList($rowArray) { $htmlList = null; @@ -266,6 +353,8 @@ class Helper_List extends Helper_Html { $htmlList = null; //create the HTML of the head of the record list $htmlList .= $this->wrapRow($this->createHead(),'listHead'); + //create the HTML of the filters + $htmlList .= $this->wrapRow($this->createFilters(),'listFilters'); for ($i = 0; $i < count($queryResult); $i++) { $this->ifInBoundaries($i); @@ -281,18 +370,21 @@ class Helper_List extends Helper_Html { $name = (strcmp($itemArray['name'],'') !== 0) ? $itemArray['name'] : $submitName; $value = (strcmp($itemArray['value'],'') !== 0) ? $itemArray['value'] : $submitValue; + $oldValue = $value; + $value = $this->strings->gtext($value); + if (strcmp($itemArray['title'],'') !== 0) { $title = "title='".$itemArray['title']."'"; } else { - $title = isset($this->submitTitles[$value]) ? "title='".$this->submitTitles[$value]."'" : null; + $title = isset($this->submitTitles[$oldValue]) ? "title='".$this->submitTitles[$oldValue]."'" : null; } - if (strcmp($this->submitImageType,'yes') === 0 and isset($this->submitImages[$value])) + if (strcmp($this->submitImageType,'yes') === 0 and isset($this->submitImages[$oldValue])) { - $imgSrc = $this->submitImages[$value]; + $imgSrc = $this->submitImages[$oldValue]; $string .= "\n"; $string .= "\n"; @@ -343,9 +435,62 @@ class Helper_List extends Helper_Html { return $string; } + public function text($itemArray) + { + return $this->simpleText($itemArray); + } + public function simpleLink($itemArray) { - $string = "viewStatus."'>".$itemArray['name']."\n"; + $string = "viewStatus."'>".$itemArray['name']."\n"; + return $string; + } + + public function link($itemArray) + { + return $this->simpleLink($itemArray); + } + + public function ledit($itemArray) + { + $text = isset($this->submitImages['edit']) ? "" : $itemArray['name']; + $title = isset($this->submitTitles['edit']) ? $this->submitTitles['edit'] : $itemArray['field']; + $string = "viewStatus."'>$text\n"; return $string; } + + //create the HTML of the filter + public function filterForm($viewArgsName) + { + $cleanName = str_replace('n!',null,$viewArgsName); + $cleanName = str_replace('-',null,$cleanName); + + if (isset($this->viewArgs[$this->pageArg])) + { + $this->viewArgs[$this->pageArg] = 1; + } + + $temp = $value = $this->viewArgs[$viewArgsName]; + //set the viewArg to the null query value + if (Params::$nullQueryValue) + { + $this->viewArgs[$viewArgsName] = Params::$nullQueryValue; + $viewStatus = Url::createUrl(array_values($this->viewArgs)); + if (strcmp($value,Params::$nullQueryValue) === 0) $value = ''; + } + $this->viewArgs[$viewArgsName] = $temp; + + $action = Url::getRoot($this->url).$viewStatus; + $imgSrc = Url::getRoot('Public/Img/Icons/elementary_2_5/find.png'); + $title = $this->strings->gtext('filter'); + $clearLinkTitle = $this->strings->gtext('clear the filter'); + + $html = "
\n"; + $html .= ""; + $html .= ""; + $html .= "\n"; + $html .= "
\n"; + + return $html; + } } \ No newline at end of file -- cgit v1.2.3