📄 common.php
字号:
$pageID = empty($pageID) ? $this->_currentPage : $pageID; if (!isset($this->_pageData)) { $this->_generatePageData(); } if (!empty($this->_pageData[$pageID])) { return $this->_pageData[$pageID]; } return array(); } // }}} // {{{ getPageIdByOffset() /** * Returns pageID for given offset * * @param $index Offset to get pageID for * @return int PageID for given offset */ function getPageIdByOffset($index) { $msg = '<b>PEAR::Pager Error:</b>' .' function "getPageIdByOffset()" not implemented.'; return $this->raiseError($msg, ERROR_PAGER_NOT_IMPLEMENTED); } // }}} // {{{ getOffsetByPageId() /** * Returns offsets for given pageID. Eg, if you * pass it pageID one and your perPage limit is 10 * it will return (1, 10). PageID of 2 would * give you (11, 20). * * @param integer PageID to get offsets for * @return array First and last offsets * @access public */ function getOffsetByPageId($pageid = null) { $pageid = isset($pageid) ? $pageid : $this->_currentPage; if (!isset($this->_pageData)) { $this->_generatePageData(); } if (isset($this->_pageData[$pageid]) || is_null($this->_itemData)) { return array( max(($this->_perPage * ($pageid - 1)) + 1, 1), min($this->_totalItems, $this->_perPage * $pageid) ); } else { return array(0, 0); } } // }}} // {{{ getPageRangeByPageId() /** * @param integer PageID to get offsets for * @return array First and last offsets */ function getPageRangeByPageId($pageID) { $msg = '<b>PEAR::Pager Error:</b>' .' function "getPageRangeByPageId()" not implemented.'; return $this->raiseError($msg, ERROR_PAGER_NOT_IMPLEMENTED); } // }}} // {{{ getLinks() /** * Returns back/next/first/last and page links, * both as ordered and associative array. * * NB: in original PEAR::Pager this method accepted two parameters, * $back_html and $next_html. Now the only parameter accepted is * an integer ($pageID), since the html text for prev/next links can * be set in the factory. If a second parameter is provided, then * the method act as it previously did. This hack was done to mantain * backward compatibility only. * * @param integer $pageID Optional pageID. If specified, links * for that page are provided instead of current one. [ADDED IN NEW PAGER VERSION] * @param string $next_html HTML to put inside the next link [deprecated: use the factory instead] * @return array back/next/first/last and page links */ function getLinks($pageID=null, $next_html='') { $msg = '<b>PEAR::Pager Error:</b>' .' function "getLinks()" not implemented.'; return $this->raiseError($msg, ERROR_PAGER_NOT_IMPLEMENTED); } // }}} // {{{ getCurrentPageID() /** * Returns ID of current page * * @return integer ID of current page */ function getCurrentPageID() { return $this->_currentPage; } // }}} // {{{ getNextPageID() /** * Returns next page ID. If current page is last page * this function returns FALSE * * @return mixed Next page ID */ function getNextPageID() { return ($this->getCurrentPageID() == $this->numPages() ? false : $this->getCurrentPageID() + 1); } // }}} // {{{ getPreviousPageID() /** * Returns previous page ID. If current page is first page * this function returns FALSE * * @return mixed Previous pages' ID */ function getPreviousPageID() { return $this->isFirstPage() ? false : $this->getCurrentPageID() - 1; } // }}} // {{{ numItems() /** * Returns number of items * * @return int Number of items */ function numItems() { return $this->_totalItems; } // }}} // {{{ numPages() /** * Returns number of pages * * @return int Number of pages */ function numPages() { return (int)$this->_totalPages; } // }}} // {{{ isFirstPage() /** * Returns whether current page is first page * * @return bool First page or not */ function isFirstPage() { return ($this->_currentPage < 2); } // }}} // {{{ isLastPage() /** * Returns whether current page is last page * * @return bool Last page or not */ function isLastPage() { return ($this->_currentPage == $this->_totalPages); } // }}} // {{{ isLastPageComplete() /** * Returns whether last page is complete * * @return bool Last age complete or not */ function isLastPageComplete() { return !($this->_totalItems % $this->_perPage); } // }}} // {{{ _generatePageData() /** * Calculates all page data * @access private */ function _generatePageData() { // Been supplied an array of data? if (!is_null($this->_itemData)) { $this->_totalItems = count($this->_itemData); } $this->_totalPages = ceil((float)$this->_totalItems / (float)$this->_perPage); $i = 1; if (!empty($this->_itemData)) { foreach ($this->_itemData as $key => $value) { $this->_pageData[$i][$key] = $value; if (count($this->_pageData[$i]) >= $this->_perPage) { $i++; } } } else { $this->_pageData = array(); } //prevent URL modification $this->_currentPage = min($this->_currentPage, $this->_totalPages); } // }}} // {{{ _renderLink() /** * Renders a link using the appropriate method * * @param altText Alternative text for this link (title property) * @param linkText Text contained by this link * @return string The link in string form * @access private */ function _renderLink($altText, $linkText) { if ($this->_httpMethod == 'GET') { if ($this->_append) { $href = '?' . $this->_http_build_query_wrapper($this->_linkData); } else { $href = str_replace('%d', $this->_linkData[$this->_urlVar], $this->_fileName); } return sprintf('<a href="%s"%s%s%s title="%s">%s</a>', htmlentities($this->_url . $href), empty($this->_classString) ? '' : ' '.$this->_classString, empty($this->_attributes) ? '' : ' '.$this->_attributes, empty($this->_accesskey) ? '' : ' accesskey="'.$this->_linkData[$this->_urlVar].'"', $altText, $linkText ); } elseif ($this->_httpMethod == 'POST') { return sprintf("<a href='javascript:void(0)' onclick='%s'%s%s%s title='%s'>%s</a>", $this->_generateFormOnClick($this->_url, $this->_linkData), empty($this->_classString) ? '' : ' '.$this->_classString, empty($this->_attributes) ? '' : ' '.$this->_attributes, empty($this->_accesskey) ? '' : ' accesskey=\''.$this->_linkData[$this->_urlVar].'\'', $altText, $linkText ); } return ''; } // }}} // {{{ _generateFormOnClick() /** * Mimics http_build_query() behavior in the way the data * in $data will appear when it makes it back to the server. * For example: * $arr = array('array' => array(array('hello', 'world'), * 'things' => array('stuff', 'junk')); * http_build_query($arr) * and _generateFormOnClick('foo.php', $arr) * will yield * $_REQUEST['array'][0][0] === 'hello' * $_REQUEST['array'][0][1] === 'world' * $_REQUEST['array']['things'][0] === 'stuff' * $_REQUEST['array']['things'][1] === 'junk' * * However, instead of generating a query string, it generates * Javascript to create and submit a form. * * @param string $formAction where the form should be submitted * @param array $data the associative array of names and values * @return string A string of javascript that generates a form and submits it * @access private */ function _generateFormOnClick($formAction, $data) { // Check we have an array to work with if (!is_array($data)) { trigger_error( '_generateForm() Parameter 1 expected to be Array or Object. Incorrect value given.', E_USER_WARNING ); return false; } if (!empty($this->_formID)) { $str = 'var form = document.getElementById("'.$this->_formID.'"); var input = ""; '; } else { $str = 'var form = document.createElement("form"); var input = ""; '; } // We /shouldn't/ need to escape the URL ... $str .= sprintf('form.action = "%s"; ', htmlentities($formAction)); $str .= sprintf('form.method = "%s"; ', $this->_httpMethod); foreach ($data as $key => $val) { $str .= $this->_generateFormOnClickHelper($val, $key); } if (empty($this->_formID)) { $str .= 'document.getElementsByTagName("body")[0].appendChild(form);'; } $str .= 'form.submit(); return false;'; return $str; } // }}} // {{{ _generateFormOnClickHelper /** * This is used by _generateFormOnClick(). * Recursively processes the arrays, objects, and literal values. * * @param data Data that should be rendered * @param prev The name so far * @return string A string of Javascript that creates form inputs * representing the data * @access private */ function _generateFormOnClickHelper($data, $prev = '') { $str = ''; if (is_array($data) || is_object($data)) { // foreach key/visible member foreach ((array)$data as $key => $val) { // append [$key] to prev $tempKey = sprintf('%s[%s]', $prev, $key); $str .= $this->_generateFormOnClickHelper($val, $tempKey); } } else { // must be a literal value // escape newlines and carriage returns $search = array("\n", "\r"); $replace = array('\n', '\n'); $escapedData = str_replace($search, $replace, $data); // am I forgetting any dangerous whitespace? // would a regex be faster? // if it's already encoded, don't encode it again if (!$this->_isEncoded($escapedData)) { $escapedData = urlencode($escapedData); } $escapedData = htmlentities($escapedData, ENT_QUOTES, 'UTF-8'); $str .= 'input = document.createElement("input"); '; $str .= 'input.type = "hidden"; '; $str .= sprintf('input.name = "%s"; ', $prev); $str .= sprintf('input.value = "%s"; ', $escapedData); $str .= 'form.appendChild(input); '; } return $str; } // }}} // {{{ _getLinksData() /** * Returns the correct link for the back/pages/next links * * @return array Data * @access private */ function _getLinksData() { $qs = array(); if ($this->_importQuery) { if ($this->_httpMethod == 'POST') { $qs = $_POST; } elseif ($this->_httpMethod == 'GET') { $qs = $_GET; } } if (count($this->_extraVars)){ $this->_recursive_urldecode($this->_extraVars); } $qs = array_merge($qs, $this->_extraVars); foreach ($this->_excludeVars as $exclude) { if (array_key_exists($exclude, $qs)) { unset($qs[$exclude]); } } if (count($qs) && get_magic_quotes_gpc()){ $this->_recursive_stripslashes($qs); } return $qs; } // }}} // {{{ _recursive_stripslashes() /** * Helper method * @param mixed $var * @access private */ function _recursive_stripslashes(&$var) { if (is_array($var)) { foreach (array_keys($var) as $k) { $this->_recursive_stripslashes($var[$k]); } } else { $var = stripslashes($var); } } // }}} // {{{ _recursive_urldecode() /** * Helper method * @param mixed $var * @access private */ function _recursive_urldecode(&$var) { if (is_array($var)) { foreach (array_keys($var) as $k) { $this->_recursive_urldecode($var[$k]); } } else { $trans_tbl = array_flip(get_html_translation_table(HTML_ENTITIES)); $var = strtr($var, $trans_tbl); } } // }}} // {{{ _getBackLink() /** * Returns back link * * @param $url URL to use in the link [deprecated: use the factory instead] * @param $link HTML to use as the link [deprecated: use the factory instead] * @return string The link * @access private */ function _getBackLink($url='', $link='') { //legacy settings... the preferred way to set an option //now is passing it to the factory if (!empty($url)) { $this->_path = $url; } if (!empty($link)) { $this->_prevImg = $link; } $back = ''; if ($this->_currentPage > 1) { $this->_linkData[$this->_urlVar] = $this->getPreviousPageID(); $back = $this->_renderLink($this->_altPrev, $this->_prevImg) . $this->_spacesBefore . $this->_spacesAfter; } return $back; } // }}} // {{{ _getPageLinks() /** * Returns pages link * * @param $url URL to use in the link [deprecated: use the factory instead] * @return string Links * @access private */ function _getPageLinks($url='') { $msg = '<b>PEAR::Pager Error:</b>' .' function "_getPageLinks()" not implemented.'; return $this->raiseError($msg, ERROR_PAGER_NOT_IMPLEMENTED); } // }}} // {{{ _getNextLink() /** * Returns next link * * @param $url URL to use in the link [deprecated: use the factory instead]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -