From 232aa1924c8c0f10d87b210b46c9f061af5c844c Mon Sep 17 00:00:00 2001 From: Antonio Gallo Date: Sun, 17 Oct 2010 13:29:57 +0000 Subject: added files --- h-source/Library/Helper/Array.php | 11 ++ h-source/Library/Helper/Html.php | 14 ++ h-source/Library/Helper/List.php | 278 +++++++++++++++++++++++++++++++++++++ h-source/Library/Helper/Menu.php | 75 ++++++++++ h-source/Library/Helper/Pages.php | 108 ++++++++++++++ h-source/Library/Helper/Popup.php | 87 ++++++++++++ h-source/Library/Helper/index.html | 1 + 7 files changed, 574 insertions(+) create mode 100755 h-source/Library/Helper/Array.php create mode 100755 h-source/Library/Helper/Html.php create mode 100755 h-source/Library/Helper/List.php create mode 100755 h-source/Library/Helper/Menu.php create mode 100755 h-source/Library/Helper/Pages.php create mode 100755 h-source/Library/Helper/Popup.php create mode 100644 h-source/Library/Helper/index.html (limited to 'h-source/Library/Helper') diff --git a/h-source/Library/Helper/Array.php b/h-source/Library/Helper/Array.php new file mode 100755 index 0000000..32a4c27 --- /dev/null +++ b/h-source/Library/Helper/Array.php @@ -0,0 +1,11 @@ + null, + 'del' => null, + 'up' => null, + 'down' => null, + 'link' => null + ); + + //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' + ); + + //$position: array. First element: page number, second element: number of pages + public $position = array(); + + //it can be: both, top, bottom, none + protected $_boundaries = 'none'; + + public function build($identifierName = 'identifier') + { + $this->_identifierName = $identifierName; + } + + public function setIdentifierName($identifierName) + { + $this->_identifierName = $identifierName; + } + + //add a list Item. $type: the type of the item, $field: the table.field to exctract (use colon to separate the table and the field),$action: controller/action,$value=if type == link->the value of the link + public function addItem($type, $action = '', $field = '', $name = '') { + if (!in_array($type,$this->_allowedItems)) { + throw new Exception('"'.$type. '" argument not allowed in '.__METHOD__.' method'); + } + $temp=array(); + $temp['type'] = $type; + $temp['action'] = $action; + $temp['field'] = $field; + $temp['name'] = $name; + $this->_itemsList[] = $temp; + + //set the $this->_head array + $head = array(); + $head['type'] = $type; + + if ($type === 'simpleText') { + $head['action'] = $this->extractFieldName($action); + } else { + $head['action'] = ' '; + } + $this->_head[] = $head; + } + + + //set the head of the table + //$columnsName: name of the columns. It has to be a comma-separated list of strings + public function setHead($columnsName = '') + { + //get the array from the list + $columnsArray = explode(',',$columnsName); + for ($i = 0; $i < count($columnsArray); $i++) + { + if ($i < count($this->_itemsList)) $this->_head[$i]['action'] = $columnsArray[$i]; + } + } + + + //$method to extract the field name from the $action string (;table:field;) + public function extractFieldName($string) { + $string = str_replace(';','',$string); + return $string; + } + + //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],':')) { + //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(':',$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(':',$stringArray[$i]); + $stringArray[$i] = $rowArray[$temp[0]][$temp[1]]; + } + } + } + return implode('',$stringArray); + } + + //call the method replaceFields upon the $items array using the associative array $rowArray + public function replaceAll($item,$rowArray) { + $item['action'] = $this->replaceFields($item['action'],$rowArray); + $item['field'] = $this->replaceFields($item['field'],$rowArray); + $item['name'] = $this->replaceFields($item['name'],$rowArray); + return $item; + } + + //wrap the column with the tag td + public function wrapColumn($string, $className = null) { + return wrap($string,array('td'=>$className)); + } + + //wrap the row with the tag tr + public function wrapRow($string,$className = null) { + return wrap($string,array('tr'=>$className)); + } + + public function wrapList($string) { + return wrap($string,array('table'=>'listTable')); + } + + //method to create the HTML of the head of the table + public function createHead() { + $htmlHead = null; + foreach ($this->_head as $item) { + $temp = $item['action']; + $htmlHead .= $this->wrapColumn($temp,$item['type']); + } + return $htmlHead; + } + + //create the HTML of a single row (values taken from the associative array $rowArray) + public function getRowList($rowArray) { + $htmlList = null; + foreach ($this->_itemsList as $item) { + $item = $this->replaceAll($item,$rowArray); + + if (($this->_boundaries === 'top' and $item['type'] === 'moveupForm') or ($this->_boundaries === 'bottom' and $item['type'] === 'movedownForm') or ($this->_boundaries === 'both' and ($item['type'] === 'moveupForm' or $item['type'] === 'movedownForm'))) + { + $htmlList .= $this->wrapColumn(' ',$item['type']); + } + else + { + $temp = call_user_func_array(array($this,$item['type']),array($item)); + $htmlList .= $this->wrapColumn($temp,$item['type']); + } + } + return $htmlList; + } + + //$index: record number + public function ifInBoundaries($index) + { + $this->_boundaries = 'none'; + + if (!empty($this->position)) + { + if ($this->_recordNumber === 1 and strcmp($this->position[0],1) === 0) + { + $this->_boundaries = 'both'; + } + else if ($index === 0 and strcmp($this->position[0],1) === 0) + { + $this->_boundaries = 'top'; + } + else if ($index === ($this->_recordNumber-1) and strcmp($this->position[0],$this->position[1]) === 0) + { + $this->_boundaries = 'bottom'; + } + } + + } + + //create the HTML of the entire list. $queryResult: the array coming from the select query + public function render($queryResult) + { + //set the number of records + $this->_recordNumber = count($queryResult); + $htmlList = null; + //create the HTML of the head of the record list + $htmlList .= $this->wrapRow($this->createHead(),'listHead'); + for ($i = 0; $i < count($queryResult); $i++) + { + $this->ifInBoundaries($i); + $temp = $this->getRowList($queryResult[$i]); + $htmlList .= $this->wrapRow($temp,'listRow'); + } + return $this->wrapList($htmlList); + } + + public function generalForm($itemArray, $submitName, $submitValue) + { + $string = "
viewStatus."' method='POST'>\n"; + $name = (strcmp($itemArray['name'],'') !== 0) ? $itemArray['name'] : $submitName; + + if (strcmp($this->submitImageType,'yes') === 0) + { + $string .= "\n"; + $string .= "\n"; + } + else + { + $string .= "\n"; + } + + $string .= "\n"; + $string .= "
\n"; + return $string; + } + + public function moveupForm($itemArray) + { + return $this->generalForm($itemArray, 'moveupAction', 'up'); + } + + public function movedownForm($itemArray) + { + return $this->generalForm($itemArray, 'movedownAction', 'down'); + } + + public function editForm($itemArray) + { + return $this->generalForm($itemArray, 'generalAction', 'edit'); + } + + public function delForm($itemArray) + { + return $this->generalForm($itemArray, 'delAction', 'del'); + } + + public function associateForm($itemArray) + { + return $this->generalForm($itemArray, 'generalAction', 'link'); + } + + public function simpleText($itemArray) { + $string = "".$itemArray['action']."\n"; + return $string; + } + + public function simpleLink($itemArray) { + $string = "viewStatus."'>".$itemArray['name']."\n"; + return $string; + } + +} \ No newline at end of file diff --git a/h-source/Library/Helper/Menu.php b/h-source/Library/Helper/Menu.php new file mode 100755 index 0000000..119ada8 --- /dev/null +++ b/h-source/Library/Helper/Menu.php @@ -0,0 +1,75 @@ + array( + 'title' => 'back', + 'class' => 'mainMenuItem', + 'text' => 'Back', + 'url' => 'main' + ), + + 'add' => array( + 'title' => 'add a new record', + 'class' => 'mainMenuItem', + 'text' => 'Add', + 'url' => 'form/insert' + ), + + 'panel' => array( + 'title' => 'back to the Panel', + 'class' => 'mainMenuItem', + 'text' => 'Panel', + 'url' => 'main' + ) + + ); + + public function build($controller = null, $panelController = null) + { + $this->controller = $controller; + $this->panelController = $panelController; + } + + //$voices: comma-separated list of links you want to print + public function render($linksList) + { + $linksArray = explode(',',$linksList); + $menu = null; + foreach ($linksArray as $linkName) + { + //check that the voice exists + if (array_key_exists($linkName,$this->links)) + { + //check that the text and the ure are defined + if (isset($this->links[$linkName]['text']) and isset($this->links[$linkName]['url'])) + { + $title = isset($this->links[$linkName]['title']) ? "title='".$this->links[$linkName]['title']."'" : null; + $class = isset($this->links[$linkName]['class']) ? "class='".$this->links[$linkName]['class']."'" : null; + + //choose the controller (current or panel) + $controller = (strcmp($linkName,'panel') === 0) ? $this->panelController.'/' : $this->controller.'/'; + $viewStatus = (strcmp($linkName,'panel') === 0) ? null : $this->viewStatus; + + $href = Url::getRoot($controller.$this->links[$linkName]['url'].$viewStatus); + $text = $this->links[$linkName]['text']; + $menu .= "
$text
\n"; + } + } + } + return $menu; + } + +} \ No newline at end of file diff --git a/h-source/Library/Helper/Pages.php b/h-source/Library/Helper/Pages.php new file mode 100755 index 0000000..eec58f6 --- /dev/null +++ b/h-source/Library/Helper/Pages.php @@ -0,0 +1,108 @@ +_variableArg = $variableArg; + $this->_urlViewAction =$urlViewAction; //url of the controller and (/) main action + $this->previousString = $previousString; + $this->nextString = $nextString; + + } + + //return the number of pages + public function getNumbOfPages() + { + return $this->_numbOfPages; + } + + //get the limit of the select query clause + public function getLimit($currentPage,$recordNumber,$recordPerPage) + { + $this->_currentPage = $currentPage; + $this->_numbOfPages=(($recordNumber%$recordPerPage)===0) ? (int) ($recordNumber/$recordPerPage) : ((int) ($recordNumber/$recordPerPage))+1; + $start=(($currentPage-1)*$recordPerPage); + return "$start,$recordPerPage"; + } + + //return the page list string + public function render($pageNumber,$numberOfPages) + { + $pageList = null; + $pageList .= $this->pageLink($this->_currentPage-1,$this->previousString); + $pageList .= $this->recursiveLink($pageNumber,$numberOfPages); + $pageList .= $this->pageLink($this->_currentPage+1,$this->nextString); + return $pageList; + } + + //recorsive function in order to write the page list + public function recursiveLink($pageNumber,$numberOfPages) + { + + if ($numberOfPages === 0) return null; + + if ($numberOfPages === 1) { + return $this->pageLink($pageNumber); + } else { + return $this->pageLink($pageNumber) . $this->recursiveLink($pageNumber+1,$numberOfPages-1); + } + } + + public function pageLink($pageNumber, $string = null) { + if ($pageNumber > 0 and $pageNumber <= $this->_numbOfPages) { + return $this->html($pageNumber,$string); + } else { + return null; + } + } + + //return the html link + public function html($pageNumber,$string = null) { + if (isset($string)) { + $strNumber = $string; + $strClass = "class='itemListPage'"; + } else { + if ($pageNumber === $this->_currentPage) + { + $strNumber = $pageNumber; + $strClass = "class='currentPage'"; + } + else + { + $strNumber = $pageNumber; + $strClass = "class='itemListPage'"; + } + } + $this->viewArgs[$this->_variableArg] = $pageNumber; + $viewStatus = Url::createUrl(array_values($this->viewArgs)); + $href= Url::getRoot(null) . $this->_urlViewAction .$viewStatus; + return $this->getATag($href,$strNumber,$strClass); + } + + //get the HTMl of the tag + //$href: href of the link + //$text: the text of the link + //$strClass: the class of the link + public function getATag($href,$text,$strClass) + { + return "$text"; + } + +} \ No newline at end of file diff --git a/h-source/Library/Helper/Popup.php b/h-source/Library/Helper/Popup.php new file mode 100755 index 0000000..9ceac72 --- /dev/null +++ b/h-source/Library/Helper/Popup.php @@ -0,0 +1,87 @@ +viewArgs[$this->pageArg] is set to 1 if $this->pageArg !== null + + //the type of the popup. If type !=exclusive, than each voice selected is added to the where clause. If type=exclusive, the selection of a popup voice causes the unselection of the other voices + public $type = null; + + //list of popup names + public $popupItems = array(); + + public function build($url, $popupArray = null, $type = 'exclusive', $pageArg = null) { + $this->url = $url; + $this->popupArray = $popupArray; + $this->pageArg = $pageArg; + $this->type = $type; + + foreach ($this->popupArray as $field => $popup) + { + $this->popupItems[] = $field; + } + } + + //check that the ViewArgs array is complete + public function checkViewArgs() + { + foreach ($this->popupArray as $field => $popup) + { + if (!array_key_exists($field,$this->viewArgs)) return false; + } + return true; + } + + //unselect the voices different from the current one + public function unselect($currentVoice) + { + foreach ($this->popupItems as $item) + { + if (strcmp($item,$currentVoice) !== 0) $this->viewArgs[$item] = Params::$nullQueryValue; + } + } + + public function render() { + $returnString = null; + if ($this->checkViewArgs()) + { + if (isset($this->viewArgs[$this->pageArg])) + { + $this->viewArgs[$this->pageArg] = 1; + } + foreach ($this->popupArray as $field => $popup) + { + if ($this->type === 'exclusive') $this->unselect($field); + //save the value of the current ViewArg + $tempArg = $this->viewArgs[$field]; + $returnString .= "\n"; + $this->viewArgs[$field] = $tempArg; + } + if (count($this->popupArray)>0) { + $returnString .= "\n"; + } + } + return $returnString; + } + +} \ No newline at end of file diff --git a/h-source/Library/Helper/index.html b/h-source/Library/Helper/index.html new file mode 100644 index 0000000..8d1c8b6 --- /dev/null +++ b/h-source/Library/Helper/index.html @@ -0,0 +1 @@ + -- cgit v1.2.3