Common.php 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Contains the Pager_Common class
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The name of the author may not be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
  19. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  20. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @category HTML
  30. * @package Pager
  31. * @author Lorenzo Alberton <l.alberton@quipo.it>
  32. * @author Richard Heyes <richard@phpguru.org>
  33. * @copyright 2003-2007 Lorenzo Alberton, Richard Heyes
  34. * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
  35. * @version CVS: $Id: Common.php,v 1.82 2008/05/31 12:44:55 quipo Exp $
  36. * @link http://pear.php.net/package/Pager
  37. */
  38. /**
  39. * Two constants used to guess the path- and file-name of the page
  40. * when the user doesn't set any other value
  41. */
  42. // Modified by Chamilo team, 16-MAR-2010.
  43. //if (substr($_SERVER['PHP_SELF'], -1) == '/') {
  44. // $http = (isset($_SERVER['HTTPS']) && ('on' == strtolower($_SERVER['HTTPS']))) ? 'https://' : 'http://';
  45. // define('PAGER_CURRENT_FILENAME', '');
  46. // define('PAGER_CURRENT_PATHNAME', $http.$_SERVER['HTTP_HOST'].str_replace('\\', '/', $_SERVER['PHP_SELF']));
  47. //} else {
  48. // define('PAGER_CURRENT_FILENAME', preg_replace('/(.*)\?.*/', '\\1', basename($_SERVER['PHP_SELF'])));
  49. // define('PAGER_CURRENT_PATHNAME', str_replace('\\', '/', dirname($_SERVER['PHP_SELF'])));
  50. //}
  51. if (substr(api_get_self(), -1) == '/') {
  52. $http = (isset($_SERVER['HTTPS']) && ('on' == strtolower($_SERVER['HTTPS']))) ? 'https://' : 'http://';
  53. define('PAGER_CURRENT_FILENAME', '');
  54. define('PAGER_CURRENT_PATHNAME', $http.$_SERVER['HTTP_HOST'].str_replace('\\', '/', api_get_self()));
  55. } else {
  56. define('PAGER_CURRENT_FILENAME', preg_replace('/(.*)\?.*/', '\\1', basename(api_get_self())));
  57. define('PAGER_CURRENT_PATHNAME', str_replace('\\', '/', dirname(api_get_self())));
  58. }
  59. //
  60. /**
  61. * Error codes
  62. */
  63. define('PAGER_OK', 0);
  64. define('ERROR_PAGER', -1);
  65. define('ERROR_PAGER_INVALID', -2);
  66. define('ERROR_PAGER_INVALID_PLACEHOLDER', -3);
  67. define('ERROR_PAGER_INVALID_USAGE', -4);
  68. define('ERROR_PAGER_NOT_IMPLEMENTED', -5);
  69. /**
  70. * Pager_Common - Common base class for [Sliding|Jumping] Window Pager
  71. * Extend this class to write a custom paging class
  72. *
  73. * @category HTML
  74. * @package Pager
  75. * @author Lorenzo Alberton <l.alberton@quipo.it>
  76. * @author Richard Heyes <richard@phpguru.org>
  77. * @copyright 2003-2007 Lorenzo Alberton, Richard Heyes
  78. * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
  79. * @link http://pear.php.net/package/Pager
  80. */
  81. class Pager_Common
  82. {
  83. // {{{ class vars
  84. /**
  85. * @var integer number of items
  86. * @access private
  87. */
  88. var $_totalItems;
  89. /**
  90. * @var integer number of items per page
  91. * @access private
  92. */
  93. var $_perPage = 10;
  94. /**
  95. * @var integer number of page links for each window
  96. * @access private
  97. */
  98. var $_delta = 10;
  99. /**
  100. * @var integer current page number
  101. * @access private
  102. */
  103. var $_currentPage = 1;
  104. /**
  105. * @var integer total pages number
  106. * @access private
  107. */
  108. var $_totalPages = 1;
  109. /**
  110. * @var string CSS class for links
  111. * @access private
  112. */
  113. var $_linkClass = '';
  114. /**
  115. * @var string wrapper for CSS class name
  116. * @access private
  117. */
  118. var $_classString = '';
  119. /**
  120. * @var string path name
  121. * @access private
  122. */
  123. var $_path = PAGER_CURRENT_PATHNAME;
  124. /**
  125. * @var string file name
  126. * @access private
  127. */
  128. var $_fileName = PAGER_CURRENT_FILENAME;
  129. /**
  130. * @var boolean If false, don't override the fileName option. Use at your own risk.
  131. * @access private
  132. */
  133. var $_fixFileName = true;
  134. /**
  135. * @var boolean you have to use FALSE with mod_rewrite
  136. * @access private
  137. */
  138. var $_append = true;
  139. /**
  140. * @var string specifies which HTTP method to use
  141. * @access private
  142. */
  143. var $_httpMethod = 'GET';
  144. /**
  145. * @var string specifies which HTML form to use
  146. * @access private
  147. */
  148. var $_formID = '';
  149. /**
  150. * @var boolean whether or not to import submitted data
  151. * @access private
  152. */
  153. var $_importQuery = true;
  154. /**
  155. * @var string name of the querystring var for pageID
  156. * @access private
  157. */
  158. var $_urlVar = 'pageID';
  159. /**
  160. * @var array data to pass through the link
  161. * @access private
  162. */
  163. var $_linkData = array();
  164. /**
  165. * @var array additional URL vars
  166. * @access private
  167. */
  168. var $_extraVars = array();
  169. /**
  170. * @var array URL vars to ignore
  171. * @access private
  172. */
  173. var $_excludeVars = array();
  174. /**
  175. * @var boolean TRUE => expanded mode (for Pager_Sliding)
  176. * @access private
  177. */
  178. var $_expanded = true;
  179. /**
  180. * @var boolean TRUE => show accesskey attribute on <a> tags
  181. * @access private
  182. */
  183. var $_accesskey = false;
  184. /**
  185. * @var string extra attributes for the <a> tag
  186. * @access private
  187. */
  188. var $_attributes = '';
  189. /**
  190. * @var string onclick
  191. * @access private
  192. */
  193. var $_onclick = '';
  194. /**
  195. * @var string alt text for "first page" (use "%d" placeholder for page number)
  196. * @access private
  197. */
  198. var $_altFirst = 'first page';
  199. /**
  200. * @var string alt text for "previous page"
  201. * @access private
  202. */
  203. var $_altPrev = 'previous page';
  204. /**
  205. * @var string alt text for "next page"
  206. * @access private
  207. */
  208. var $_altNext = 'next page';
  209. /**
  210. * @var string alt text for "last page" (use "%d" placeholder for page number)
  211. * @access private
  212. */
  213. var $_altLast = 'last page';
  214. /**
  215. * @var string alt text for "page" (use optional "%d" placeholder for page number)
  216. * @access private
  217. */
  218. var $_altPage = 'page';
  219. /**
  220. * @var string image/text to use as "prev" link
  221. * @access private
  222. */
  223. var $_prevImg = '&lt;&lt; Back';
  224. /**
  225. * image/text to use as "prev" link when no prev link is needed (e.g. on the first page)
  226. * NULL deactivates it
  227. *
  228. * @var string
  229. * @access private
  230. */
  231. var $_prevImgEmpty = null;
  232. /**
  233. * @var string image/text to use as "next" link
  234. * @access private
  235. */
  236. var $_nextImg = 'Next &gt;&gt;';
  237. /**
  238. * image/text to use as "next" link when
  239. * no next link is needed (e.g. on the last page)
  240. * NULL deactivates it
  241. *
  242. * @var string
  243. * @access private
  244. */
  245. var $_nextImgEmpty = null;
  246. /**
  247. * @var string link separator
  248. * @access private
  249. */
  250. var $_separator = '';
  251. /**
  252. * @var integer number of spaces before separator
  253. * @access private
  254. */
  255. var $_spacesBeforeSeparator = 0;
  256. /**
  257. * @var integer number of spaces after separator
  258. * @access private
  259. */
  260. var $_spacesAfterSeparator = 1;
  261. /**
  262. * @var string CSS class name for current page link
  263. * @access private
  264. */
  265. var $_curPageLinkClassName = '';
  266. /**
  267. * @var string Text before current page link
  268. * @access private
  269. */
  270. var $_curPageSpanPre = '';
  271. /**
  272. * @var string Text after current page link
  273. * @access private
  274. */
  275. var $_curPageSpanPost = '';
  276. /**
  277. * @var string Text before first page link
  278. * @access private
  279. */
  280. var $_firstPagePre = '[';
  281. /**
  282. * @var string Text to be used for first page link
  283. * @access private
  284. */
  285. var $_firstPageText = '';
  286. /**
  287. * @var string Text after first page link
  288. * @access private
  289. */
  290. var $_firstPagePost = ']';
  291. /**
  292. * @var string Text before last page link
  293. * @access private
  294. */
  295. var $_lastPagePre = '[';
  296. /**
  297. * @var string Text to be used for last page link
  298. * @access private
  299. */
  300. var $_lastPageText = '';
  301. /**
  302. * @var string Text after last page link
  303. * @access private
  304. */
  305. var $_lastPagePost = ']';
  306. /**
  307. * @var string Will contain the HTML code for the spaces
  308. * @access private
  309. */
  310. var $_spacesBefore = '';
  311. /**
  312. * @var string Will contain the HTML code for the spaces
  313. * @access private
  314. */
  315. var $_spacesAfter = '';
  316. /**
  317. * @var string String used as title in <link rel="first"> tag
  318. * @access private
  319. */
  320. var $_firstLinkTitle = 'first page';
  321. /**
  322. * @var string String used as title in <link rel="next"> tag
  323. * @access private
  324. */
  325. var $_nextLinkTitle = 'next page';
  326. /**
  327. * @var string String used as title in <link rel="previous"> tag
  328. * @access private
  329. */
  330. var $_prevLinkTitle = 'previous page';
  331. /**
  332. * @var string String used as title in <link rel="last"> tag
  333. * @access private
  334. */
  335. var $_lastLinkTitle = 'last page';
  336. /**
  337. * @var string Text to be used for the 'show all' option in the select box
  338. * @access private
  339. */
  340. var $_showAllText = '';
  341. /**
  342. * @var array data to be paged
  343. * @access private
  344. */
  345. var $_itemData = null;
  346. /**
  347. * @var boolean If TRUE and there's only one page, links aren't shown
  348. * @access private
  349. */
  350. var $_clearIfVoid = true;
  351. /**
  352. * @var boolean Use session for storing the number of items per page
  353. * @access private
  354. */
  355. var $_useSessions = false;
  356. /**
  357. * @var boolean Close the session when finished reading/writing data
  358. * @access private
  359. */
  360. var $_closeSession = false;
  361. /**
  362. * @var string name of the session var for number of items per page
  363. * @access private
  364. */
  365. var $_sessionVar = 'setPerPage';
  366. /**
  367. * Pear error mode (when raiseError is called)
  368. * (see PEAR doc)
  369. *
  370. * @var integer $_pearErrorMode
  371. * @access private
  372. */
  373. var $_pearErrorMode = null;
  374. // }}}
  375. // {{{ public vars
  376. /**
  377. * @var string Complete set of links
  378. * @access public
  379. */
  380. var $links = '';
  381. /**
  382. * @var string Complete set of link tags
  383. * @access public
  384. */
  385. var $linkTags = '';
  386. /**
  387. * @var array Complete set of raw link tags
  388. * @access public
  389. */
  390. var $linkTagsRaw = array();
  391. /**
  392. * @var array Array with a key => value pair representing
  393. * page# => bool value (true if key==currentPageNumber).
  394. * can be used for extreme customization.
  395. * @access public
  396. */
  397. var $range = array();
  398. /**
  399. * @var array list of available options (safety check)
  400. * @access private
  401. */
  402. var $_allowed_options = array(
  403. 'totalItems',
  404. 'perPage',
  405. 'delta',
  406. 'linkClass',
  407. 'path',
  408. 'fileName',
  409. 'fixFileName',
  410. 'append',
  411. 'httpMethod',
  412. 'formID',
  413. 'importQuery',
  414. 'urlVar',
  415. 'altFirst',
  416. 'altPrev',
  417. 'altNext',
  418. 'altLast',
  419. 'altPage',
  420. 'prevImg',
  421. 'prevImgEmpty',
  422. 'nextImg',
  423. 'nextImgEmpty',
  424. 'expanded',
  425. 'accesskey',
  426. 'attributes',
  427. 'onclick',
  428. 'separator',
  429. 'spacesBeforeSeparator',
  430. 'spacesAfterSeparator',
  431. 'curPageLinkClassName',
  432. 'curPageSpanPre',
  433. 'curPageSpanPost',
  434. 'firstPagePre',
  435. 'firstPageText',
  436. 'firstPagePost',
  437. 'lastPagePre',
  438. 'lastPageText',
  439. 'lastPagePost',
  440. 'firstLinkTitle',
  441. 'nextLinkTitle',
  442. 'prevLinkTitle',
  443. 'lastLinkTitle',
  444. 'showAllText',
  445. 'itemData',
  446. 'clearIfVoid',
  447. 'useSessions',
  448. 'closeSession',
  449. 'sessionVar',
  450. 'pearErrorMode',
  451. 'extraVars',
  452. 'excludeVars',
  453. 'currentPage',
  454. );
  455. // }}}
  456. // {{{ build()
  457. /**
  458. * Generate or refresh the links and paged data after a call to setOptions()
  459. *
  460. * @return void
  461. * @access public
  462. */
  463. function build()
  464. {
  465. //reset
  466. $this->_pageData = array();
  467. $this->links = '';
  468. $this->linkTags = '';
  469. $this->linkTagsRaw = array();
  470. $this->_generatePageData();
  471. $this->_setFirstLastText();
  472. if ($this->_totalPages > (2 * $this->_delta + 1)) {
  473. $this->links .= $this->_printFirstPage();
  474. }
  475. $this->links .= $this->_getBackLink();
  476. $this->links .= $this->_getPageLinks();
  477. $this->links .= $this->_getNextLink();
  478. $this->linkTags .= $this->_getFirstLinkTag();
  479. $this->linkTags .= $this->_getPrevLinkTag();
  480. $this->linkTags .= $this->_getNextLinkTag();
  481. $this->linkTags .= $this->_getLastLinkTag();
  482. $this->linkTagsRaw['first'] = $this->_getFirstLinkTag(true);
  483. $this->linkTagsRaw['prev'] = $this->_getPrevLinkTag(true);
  484. $this->linkTagsRaw['next'] = $this->_getNextLinkTag(true);
  485. $this->linkTagsRaw['last'] = $this->_getLastLinkTag(true);
  486. if ($this->_totalPages > (2 * $this->_delta + 1)) {
  487. $this->links .= $this->_printLastPage();
  488. }
  489. }
  490. // }}}
  491. // {{{ getPageData()
  492. /**
  493. * Returns an array of current pages data
  494. *
  495. * @param integer $pageID Desired page ID (optional)
  496. *
  497. * @return array Page data
  498. * @access public
  499. */
  500. function getPageData($pageID = null)
  501. {
  502. $pageID = empty($pageID) ? $this->_currentPage : $pageID;
  503. if (!isset($this->_pageData)) {
  504. $this->_generatePageData();
  505. }
  506. if (!empty($this->_pageData[$pageID])) {
  507. return $this->_pageData[$pageID];
  508. }
  509. return array();
  510. }
  511. // }}}
  512. // {{{ getPageIdByOffset()
  513. /**
  514. * Returns pageID for given offset
  515. *
  516. * @param integer $index Offset to get pageID for
  517. *
  518. * @return integer PageID for given offset
  519. * @access public
  520. */
  521. function getPageIdByOffset($index)
  522. {
  523. $msg = 'function "getPageIdByOffset()" not implemented.';
  524. return $this->raiseError($msg, ERROR_PAGER_NOT_IMPLEMENTED);
  525. }
  526. // }}}
  527. // {{{ getOffsetByPageId()
  528. /**
  529. * Returns offsets for given pageID. Eg, if you
  530. * pass it pageID one and your perPage limit is 10
  531. * it will return (1, 10). PageID of 2 would
  532. * give you (11, 20).
  533. *
  534. * @param integer $pageID PageID to get offsets for
  535. *
  536. * @return array First and last offsets
  537. * @access public
  538. */
  539. function getOffsetByPageId($pageID = null)
  540. {
  541. $pageID = isset($pageID) ? $pageID : $this->_currentPage;
  542. if (!isset($this->_pageData)) {
  543. $this->_generatePageData();
  544. }
  545. if (isset($this->_pageData[$pageID]) || is_null($this->_itemData)) {
  546. return array(
  547. max(($this->_perPage * ($pageID - 1)) + 1, 1),
  548. min($this->_totalItems, $this->_perPage * $pageID)
  549. );
  550. }
  551. return array(0, 0);
  552. }
  553. // }}}
  554. // {{{ getPageRangeByPageId()
  555. /**
  556. * Given a PageId, it returns the limits of the range of pages displayed.
  557. *
  558. * @param integer $pageID PageID to get offsets for
  559. *
  560. * @return array First and last offsets
  561. * @access public
  562. */
  563. function getPageRangeByPageId($pageID = null)
  564. {
  565. $msg = 'function "getPageRangeByPageId()" not implemented.';
  566. return $this->raiseError($msg, ERROR_PAGER_NOT_IMPLEMENTED);
  567. }
  568. // }}}
  569. // {{{ getLinks()
  570. /**
  571. * Returns back/next/first/last and page links,
  572. * both as ordered and associative array.
  573. *
  574. * NB: in original PEAR::Pager this method accepted two parameters,
  575. * $back_html and $next_html. Now the only parameter accepted is
  576. * an integer ($pageID), since the html text for prev/next links can
  577. * be set in the factory. If a second parameter is provided, then
  578. * the method act as it previously did. This hack was done to mantain
  579. * backward compatibility only.
  580. *
  581. * @param integer $pageID Optional pageID. If specified, links for that
  582. * page are provided instead of current one.
  583. * [ADDED IN NEW PAGER VERSION]
  584. * @param string $next_html HTML to put inside the next link
  585. * [deprecated: use the factory instead]
  586. *
  587. * @return array back/next/first/last and page links
  588. * @access public
  589. */
  590. function getLinks($pageID=null, $next_html='')
  591. {
  592. $msg = 'function "getLinks()" not implemented.';
  593. return $this->raiseError($msg, ERROR_PAGER_NOT_IMPLEMENTED);
  594. }
  595. // }}}
  596. // {{{ getCurrentPageID()
  597. /**
  598. * Returns ID of current page
  599. *
  600. * @return integer ID of current page
  601. * @access public
  602. */
  603. function getCurrentPageID()
  604. {
  605. return $this->_currentPage;
  606. }
  607. // }}}
  608. // {{{ getNextPageID()
  609. /**
  610. * Returns next page ID. If current page is last page
  611. * this function returns FALSE
  612. *
  613. * @return mixed Next page ID or false
  614. * @access public
  615. */
  616. function getNextPageID()
  617. {
  618. return ($this->getCurrentPageID() == $this->numPages() ? false : $this->getCurrentPageID() + 1);
  619. }
  620. // }}}
  621. // {{{ getPreviousPageID()
  622. /**
  623. * Returns previous page ID. If current page is first page
  624. * this function returns FALSE
  625. *
  626. * @return mixed Previous page ID or false
  627. * @access public
  628. */
  629. function getPreviousPageID()
  630. {
  631. return $this->isFirstPage() ? false : $this->getCurrentPageID() - 1;
  632. }
  633. // }}}
  634. // {{{ numItems()
  635. /**
  636. * Returns number of items
  637. *
  638. * @return integer Number of items
  639. * @access public
  640. */
  641. function numItems()
  642. {
  643. return $this->_totalItems;
  644. }
  645. // }}}
  646. // {{{ numPages()
  647. /**
  648. * Returns number of pages
  649. *
  650. * @return integer Number of pages
  651. * @access public
  652. */
  653. function numPages()
  654. {
  655. return (int)$this->_totalPages;
  656. }
  657. // }}}
  658. // {{{ isFirstPage()
  659. /**
  660. * Returns whether current page is first page
  661. *
  662. * @return bool First page or not
  663. * @access public
  664. */
  665. function isFirstPage()
  666. {
  667. return ($this->_currentPage < 2);
  668. }
  669. // }}}
  670. // {{{ isLastPage()
  671. /**
  672. * Returns whether current page is last page
  673. *
  674. * @return bool Last page or not
  675. * @access public
  676. */
  677. function isLastPage()
  678. {
  679. return ($this->_currentPage == $this->_totalPages);
  680. }
  681. // }}}
  682. // {{{ isLastPageComplete()
  683. /**
  684. * Returns whether last page is complete
  685. *
  686. * @return bool Last age complete or not
  687. * @access public
  688. */
  689. function isLastPageComplete()
  690. {
  691. return !($this->_totalItems % $this->_perPage);
  692. }
  693. // }}}
  694. // {{{ _generatePageData()
  695. /**
  696. * Calculates all page data
  697. *
  698. * @return void
  699. * @access private
  700. */
  701. function _generatePageData()
  702. {
  703. // Been supplied an array of data?
  704. if (!is_null($this->_itemData)) {
  705. $this->_totalItems = count($this->_itemData);
  706. }
  707. $this->_totalPages = ceil((float)$this->_totalItems / (float)$this->_perPage);
  708. $i = 1;
  709. if (!empty($this->_itemData)) {
  710. foreach ($this->_itemData as $key => $value) {
  711. $this->_pageData[$i][$key] = $value;
  712. if (count($this->_pageData[$i]) >= $this->_perPage) {
  713. $i++;
  714. }
  715. }
  716. } else {
  717. $this->_pageData = array();
  718. }
  719. //prevent URL modification
  720. $this->_currentPage = min($this->_currentPage, $this->_totalPages);
  721. }
  722. // }}}
  723. // {{{ _renderLink()
  724. /**
  725. * Renders a link using the appropriate method
  726. *
  727. * @param string $altText Alternative text for this link (title property)
  728. * @param string $linkText Text contained by this link
  729. *
  730. * @return string The link in string form
  731. * @access private
  732. */
  733. function _renderLink($altText, $linkText)
  734. {
  735. if ($this->_httpMethod == 'GET') {
  736. if ($this->_append) {
  737. $href = '?' . $this->_http_build_query_wrapper($this->_linkData);
  738. } else {
  739. $href = str_replace('%d', $this->_linkData[$this->_urlVar], $this->_fileName);
  740. }
  741. $onclick = '';
  742. if (array_key_exists($this->_urlVar, $this->_linkData)) {
  743. $onclick = str_replace('%d', $this->_linkData[$this->_urlVar], $this->_onclick);
  744. }
  745. return sprintf('<a href="%s"%s%s%s%s title="%s">%s</a>',
  746. // Modified by Ivan Tcholakov, 17-OCT-2008.
  747. //htmlentities($this->_url . $href, ENT_COMPAT, 'UTF-8'),
  748. api_htmlentities($this->_url . $href),
  749. //
  750. empty($this->_classString) ? '' : ' '.$this->_classString,
  751. empty($this->_attributes) ? '' : ' '.$this->_attributes,
  752. empty($this->_accesskey) ? '' : ' accesskey="'.$this->_linkData[$this->_urlVar].'"',
  753. empty($onclick) ? '' : ' onclick="'.$onclick.'"',
  754. $altText,
  755. $linkText
  756. );
  757. } elseif ($this->_httpMethod == 'POST') {
  758. $href = $this->_url;
  759. if (!empty($_GET)) {
  760. $href .= '?' . $this->_http_build_query_wrapper($_GET);
  761. }
  762. return sprintf("<a href='javascript:void(0)' onclick='%s'%s%s%s title='%s'>%s</a>",
  763. $this->_generateFormOnClick($href, $this->_linkData),
  764. empty($this->_classString) ? '' : ' '.$this->_classString,
  765. empty($this->_attributes) ? '' : ' '.$this->_attributes,
  766. empty($this->_accesskey) ? '' : ' accesskey=\''.$this->_linkData[$this->_urlVar].'\'',
  767. $altText,
  768. $linkText
  769. );
  770. }
  771. return '';
  772. }
  773. // }}}
  774. // {{{ _generateFormOnClick()
  775. /**
  776. * Mimics http_build_query() behavior in the way the data
  777. * in $data will appear when it makes it back to the server.
  778. * For example:
  779. * $arr = array('array' => array(array('hello', 'world'),
  780. * 'things' => array('stuff', 'junk'));
  781. * http_build_query($arr)
  782. * and _generateFormOnClick('foo.php', $arr)
  783. * will yield
  784. * $_REQUEST['array'][0][0] === 'hello'
  785. * $_REQUEST['array'][0][1] === 'world'
  786. * $_REQUEST['array']['things'][0] === 'stuff'
  787. * $_REQUEST['array']['things'][1] === 'junk'
  788. *
  789. * However, instead of generating a query string, it generates
  790. * Javascript to create and submit a form.
  791. *
  792. * @param string $formAction where the form should be submitted
  793. * @param array $data the associative array of names and values
  794. *
  795. * @return string A string of javascript that generates a form and submits it
  796. * @access private
  797. */
  798. function _generateFormOnClick($formAction, $data)
  799. {
  800. // Check we have an array to work with
  801. if (!is_array($data)) {
  802. trigger_error(
  803. '_generateForm() Parameter 1 expected to be Array or Object. Incorrect value given.',
  804. E_USER_WARNING
  805. );
  806. return false;
  807. }
  808. if (!empty($this->_formID)) {
  809. $str = 'var form = document.getElementById("'.$this->_formID.'"); var input = ""; ';
  810. } else {
  811. $str = 'var form = document.createElement("form"); var input = ""; ';
  812. }
  813. // We /shouldn't/ need to escape the URL ...
  814. // Modified by Ivan Tcholakov, 17-OCT-2008.
  815. //$str .= sprintf('form.action = "%s"; ', htmlentities($formAction, ENT_COMPAT, 'UTF-8'));
  816. $str .= sprintf('form.action = "%s"; ', api_htmlentities($formAction));
  817. //
  818. $str .= sprintf('form.method = "%s"; ', $this->_httpMethod);
  819. foreach ($data as $key => $val) {
  820. $str .= $this->_generateFormOnClickHelper($val, $key);
  821. }
  822. if (empty($this->_formID)) {
  823. $str .= 'document.getElementsByTagName("body")[0].appendChild(form);';
  824. }
  825. $str .= 'form.submit(); return false;';
  826. return $str;
  827. }
  828. // }}}
  829. // {{{ _generateFormOnClickHelper
  830. /**
  831. * This is used by _generateFormOnClick().
  832. * Recursively processes the arrays, objects, and literal values.
  833. *
  834. * @param mixed $data Data that should be rendered
  835. * @param string $prev The name so far
  836. *
  837. * @return string A string of Javascript that creates form inputs
  838. * representing the data
  839. * @access private
  840. */
  841. function _generateFormOnClickHelper($data, $prev = '')
  842. {
  843. $str = '';
  844. if (is_array($data) || is_object($data)) {
  845. // foreach key/visible member
  846. foreach ((array)$data as $key => $val) {
  847. // append [$key] to prev
  848. $tempKey = sprintf('%s[%s]', $prev, $key);
  849. $str .= $this->_generateFormOnClickHelper($val, $tempKey);
  850. }
  851. } else { // must be a literal value
  852. // escape newlines and carriage returns
  853. $search = array("\n", "\r");
  854. $replace = array('\n', '\n');
  855. $escapedData = str_replace($search, $replace, $data);
  856. // am I forgetting any dangerous whitespace?
  857. // would a regex be faster?
  858. // if it's already encoded, don't encode it again
  859. if (!$this->_isEncoded($escapedData)) {
  860. $escapedData = urlencode($escapedData);
  861. }
  862. // Modified by Ivan Tcholakov, 16-MAR-2010.
  863. //$escapedData = htmlentities($escapedData, ENT_QUOTES, 'UTF-8');
  864. $escapedData = api_htmlentities($escapedData, ENT_QUOTES);
  865. //
  866. $str .= 'input = document.createElement("input"); ';
  867. $str .= 'input.type = "hidden"; ';
  868. $str .= sprintf('input.name = "%s"; ', $prev);
  869. $str .= sprintf('input.value = "%s"; ', $escapedData);
  870. $str .= 'form.appendChild(input); ';
  871. }
  872. return $str;
  873. }
  874. // }}}
  875. // {{{ _isRegexp()
  876. /**
  877. * Returns true if the string is a regexp pattern
  878. *
  879. * @param string $string the pattern to check
  880. *
  881. * @return boolean
  882. * @access private
  883. */
  884. function _isRegexp($string) {
  885. return preg_match('/^\/.*\/([Uims]+)?$/', $string);
  886. }
  887. // }}}
  888. // {{{ _getLinksData()
  889. /**
  890. * Returns the correct link for the back/pages/next links
  891. *
  892. * @return array Data
  893. * @access private
  894. */
  895. function _getLinksData()
  896. {
  897. $qs = array();
  898. if ($this->_importQuery) {
  899. if ($this->_httpMethod == 'POST') {
  900. $qs = $_POST;
  901. } elseif ($this->_httpMethod == 'GET') {
  902. $qs = $_GET;
  903. }
  904. }
  905. foreach ($this->_excludeVars as $exclude) {
  906. $use_preg = $this->_isRegexp($exclude);
  907. foreach (array_keys($qs) as $qs_item) {
  908. if ($use_preg) {
  909. if (preg_match($exclude, $qs_item, $matches)) {
  910. foreach ($matches as $m) {
  911. unset($qs[$m]);
  912. }
  913. }
  914. } elseif ($qs_item == $exclude) {
  915. unset($qs[$qs_item]);
  916. break;
  917. }
  918. }
  919. }
  920. if (count($this->_extraVars)) {
  921. $this->_recursive_urldecode($this->_extraVars);
  922. $qs = array_merge($qs, $this->_extraVars);
  923. }
  924. if (count($qs)
  925. && function_exists('get_magic_quotes_gpc')
  926. && -1 == version_compare(PHP_VERSION, '5.2.99')
  927. && get_magic_quotes_gpc()
  928. ) {
  929. $this->_recursive_stripslashes($qs);
  930. }
  931. return $qs;
  932. }
  933. // }}}
  934. // {{{ _recursive_stripslashes()
  935. /**
  936. * Helper method
  937. *
  938. * @param string|array &$var variable to clean
  939. *
  940. * @return void
  941. * @access private
  942. */
  943. function _recursive_stripslashes(&$var)
  944. {
  945. if (is_array($var)) {
  946. foreach (array_keys($var) as $k) {
  947. $this->_recursive_stripslashes($var[$k]);
  948. }
  949. } else {
  950. $var = stripslashes($var);
  951. }
  952. }
  953. // }}}
  954. // {{{ _recursive_urldecode()
  955. /**
  956. * Helper method
  957. *
  958. * @param string|array &$var variable to decode
  959. *
  960. * @return void
  961. * @access private
  962. */
  963. function _recursive_urldecode(&$var)
  964. {
  965. if (is_array($var)) {
  966. foreach (array_keys($var) as $k) {
  967. $this->_recursive_urldecode($var[$k]);
  968. }
  969. } else {
  970. $trans_tbl = array_flip(get_html_translation_table(HTML_ENTITIES));
  971. $var = strtr($var, $trans_tbl);
  972. }
  973. }
  974. // }}}
  975. // {{{ _getBackLink()
  976. /**
  977. * Returns back link
  978. *
  979. * @param string $url URL to use in the link [deprecated: use the factory instead]
  980. * @param string $link HTML to use as the link [deprecated: use the factory instead]
  981. *
  982. * @return string The link
  983. * @access private
  984. */
  985. function _getBackLink($url='', $link='')
  986. {
  987. //legacy settings... the preferred way to set an option
  988. //now is passing it to the factory
  989. if (!empty($url)) {
  990. $this->_path = $url;
  991. }
  992. if (!empty($link)) {
  993. $this->_prevImg = $link;
  994. }
  995. $back = '';
  996. if ($this->_currentPage > 1) {
  997. $this->_linkData[$this->_urlVar] = $this->getPreviousPageID();
  998. $back = $this->_renderLink($this->_altPrev, $this->_prevImg)
  999. . $this->_spacesBefore . $this->_spacesAfter;
  1000. } else if ($this->_prevImgEmpty !== null && $this->_totalPages > 1) {
  1001. $back = $this->_prevImgEmpty
  1002. . $this->_spacesBefore . $this->_spacesAfter;
  1003. }
  1004. return $back;
  1005. }
  1006. // }}}
  1007. // {{{ _getPageLinks()
  1008. /**
  1009. * Returns pages link
  1010. *
  1011. * @param string $url URL to use in the link [deprecated: use the factory instead]
  1012. *
  1013. * @return string Links
  1014. * @access private
  1015. */
  1016. function _getPageLinks($url='')
  1017. {
  1018. $msg = 'function "_getPageLinks()" not implemented.';
  1019. return $this->raiseError($msg, ERROR_PAGER_NOT_IMPLEMENTED);
  1020. }
  1021. // }}}
  1022. // {{{ _getNextLink()
  1023. /**
  1024. * Returns next link
  1025. *
  1026. * @param string $url URL to use in the link [deprecated: use the factory instead]
  1027. * @param string $link HTML to use as the link [deprecated: use the factory instead]
  1028. *
  1029. * @return string The link
  1030. * @access private
  1031. */
  1032. function _getNextLink($url='', $link='')
  1033. {
  1034. //legacy settings... the preferred way to set an option
  1035. //now is passing it to the factory
  1036. if (!empty($url)) {
  1037. $this->_path = $url;
  1038. }
  1039. if (!empty($link)) {
  1040. $this->_nextImg = $link;
  1041. }
  1042. $next = '';
  1043. if ($this->_currentPage < $this->_totalPages) {
  1044. $this->_linkData[$this->_urlVar] = $this->getNextPageID();
  1045. $next = $this->_spacesAfter
  1046. . $this->_renderLink($this->_altNext, $this->_nextImg)
  1047. . $this->_spacesBefore . $this->_spacesAfter;
  1048. } else if ($this->_nextImgEmpty !== null && $this->_totalPages > 1) {
  1049. $next = $this->_spacesAfter
  1050. . $this->_nextImgEmpty
  1051. . $this->_spacesBefore . $this->_spacesAfter;
  1052. }
  1053. return $next;
  1054. }
  1055. // }}}
  1056. // {{{ _getFirstLinkTag()
  1057. /**
  1058. * Returns first link tag
  1059. *
  1060. * @param bool $raw should tag returned as array
  1061. *
  1062. * @return mixed string with html link tag or separated as array
  1063. * @access private
  1064. */
  1065. function _getFirstLinkTag($raw = false)
  1066. {
  1067. if ($this->isFirstPage() || ($this->_httpMethod != 'GET')) {
  1068. return $raw ? array() : '';
  1069. }
  1070. if ($raw) {
  1071. return array(
  1072. 'url' => $this->_getLinkTagUrl(1),
  1073. 'title' => $this->_firstLinkTitle
  1074. );
  1075. }
  1076. return sprintf('<link rel="first" href="%s" title="%s" />'."\n",
  1077. $this->_getLinkTagUrl(1),
  1078. $this->_firstLinkTitle
  1079. );
  1080. }
  1081. // }}}
  1082. // {{{ _getPrevLinkTag()
  1083. /**
  1084. * Returns previous link tag
  1085. *
  1086. * @param bool $raw should tag returned as array
  1087. *
  1088. * @return mixed string with html link tag or separated as array
  1089. * @access private
  1090. */
  1091. function _getPrevLinkTag($raw = false)
  1092. {
  1093. if ($this->isFirstPage() || ($this->_httpMethod != 'GET')) {
  1094. return $raw ? array() : '';
  1095. }
  1096. if ($raw) {
  1097. return array(
  1098. 'url' => $this->_getLinkTagUrl($this->getPreviousPageID()),
  1099. 'title' => $this->_prevLinkTitle
  1100. );
  1101. }
  1102. return sprintf('<link rel="previous" href="%s" title="%s" />'."\n",
  1103. $this->_getLinkTagUrl($this->getPreviousPageID()),
  1104. $this->_prevLinkTitle
  1105. );
  1106. }
  1107. // }}}
  1108. // {{{ _getNextLinkTag()
  1109. /**
  1110. * Returns next link tag
  1111. *
  1112. * @param bool $raw should tag returned as array
  1113. *
  1114. * @return mixed string with html link tag or separated as array
  1115. * @access private
  1116. */
  1117. function _getNextLinkTag($raw = false)
  1118. {
  1119. if ($this->isLastPage() || ($this->_httpMethod != 'GET')) {
  1120. return $raw ? array() : '';
  1121. }
  1122. if ($raw) {
  1123. return array(
  1124. 'url' => $this->_getLinkTagUrl($this->getNextPageID()),
  1125. 'title' => $this->_nextLinkTitle
  1126. );
  1127. }
  1128. return sprintf('<link rel="next" href="%s" title="%s" />'."\n",
  1129. $this->_getLinkTagUrl($this->getNextPageID()),
  1130. $this->_nextLinkTitle
  1131. );
  1132. }
  1133. // }}}
  1134. // {{{ _getLastLinkTag()
  1135. /**
  1136. * Returns last link tag
  1137. *
  1138. * @param bool $raw should tag returned as array
  1139. *
  1140. * @return mixed string with html link tag or separated as array
  1141. * @access private
  1142. */
  1143. function _getLastLinkTag($raw = false)
  1144. {
  1145. if ($this->isLastPage() || ($this->_httpMethod != 'GET')) {
  1146. return $raw ? array() : '';
  1147. }
  1148. if ($raw) {
  1149. return array(
  1150. 'url' => $this->_getLinkTagUrl($this->_totalPages),
  1151. 'title' => $this->_lastLinkTitle
  1152. );
  1153. }
  1154. return sprintf('<link rel="last" href="%s" title="%s" />'."\n",
  1155. $this->_getLinkTagUrl($this->_totalPages),
  1156. $this->_lastLinkTitle
  1157. );
  1158. }
  1159. // }}}
  1160. // {{{ _getLinkTagUrl()
  1161. /**
  1162. * Helper method
  1163. *
  1164. * @param integer $pageID page ID
  1165. *
  1166. * @return string the link tag url
  1167. * @access private
  1168. */
  1169. function _getLinkTagUrl($pageID)
  1170. {
  1171. $this->_linkData[$this->_urlVar] = $pageID;
  1172. if ($this->_append) {
  1173. $href = '?' . $this->_http_build_query_wrapper($this->_linkData);
  1174. } else {
  1175. $href = str_replace('%d', $this->_linkData[$this->_urlVar], $this->_fileName);
  1176. }
  1177. // Modified by Ivan Tcholakov, 17-OCT-2008.
  1178. //return htmlentities($this->_url . $href, ENT_COMPAT, 'UTF-8');
  1179. return api_htmlentities($this->_url . $href);
  1180. //
  1181. }
  1182. // }}}
  1183. // {{{ getPerPageSelectBox()
  1184. /**
  1185. * Returns a string with a XHTML SELECT menu,
  1186. * useful for letting the user choose how many items per page should be
  1187. * displayed. If parameter useSessions is TRUE, this value is stored in
  1188. * a session var. The string isn't echoed right now so you can use it
  1189. * with template engines.
  1190. *
  1191. * @param integer $start starting value for the select menu
  1192. * @param integer $end ending value for the select menu
  1193. * @param integer $step step between values in the select menu
  1194. * @param boolean $showAllData If true, perPage is set equal to totalItems.
  1195. * @param array $extraParams (or string $optionText for BC reasons)
  1196. * - 'optionText': text to show in each option.
  1197. * Use '%d' where you want to see the number of pages selected.
  1198. * - 'attributes': (html attributes) Tag attributes or
  1199. * HTML attributes (id="foo" pairs), will be inserted in the
  1200. * <select> tag
  1201. *
  1202. * @return string xhtml select box
  1203. * @access public
  1204. */
  1205. function getPerPageSelectBox($start=5, $end=30, $step=5, $showAllData=false, $extraParams=array())
  1206. {
  1207. include_once 'Pager/HtmlWidgets.php';
  1208. $widget = new Pager_HtmlWidgets($this);
  1209. return $widget->getPerPageSelectBox($start, $end, $step, $showAllData, $extraParams);
  1210. }
  1211. // }}}
  1212. // {{{ getPageSelectBox()
  1213. /**
  1214. * Returns a string with a XHTML SELECT menu with the page numbers,
  1215. * useful as an alternative to the links
  1216. *
  1217. * @param array $params - 'optionText': text to show in each option.
  1218. * Use '%d' where you want to see the number
  1219. * of pages selected.
  1220. * - 'autoSubmit': if TRUE, add some js code
  1221. * to submit the form on the onChange event
  1222. * @param string $extraAttributes (html attributes) Tag attributes or
  1223. * HTML attributes (id="foo" pairs), will be
  1224. * inserted in the <select> tag
  1225. *
  1226. * @return string xhtml select box
  1227. * @access public
  1228. */
  1229. function getPageSelectBox($params = array(), $extraAttributes = '')
  1230. {
  1231. include_once 'Pager/HtmlWidgets.php';
  1232. $widget = new Pager_HtmlWidgets($this);
  1233. return $widget->getPageSelectBox($params, $extraAttributes);
  1234. }
  1235. // }}}
  1236. // {{{ _printFirstPage()
  1237. /**
  1238. * Print [1]
  1239. *
  1240. * @return string String with link to 1st page,
  1241. * or empty string if this is the 1st page.
  1242. * @access private
  1243. */
  1244. function _printFirstPage()
  1245. {
  1246. if ($this->isFirstPage()) {
  1247. return '';
  1248. }
  1249. $this->_linkData[$this->_urlVar] = 1;
  1250. return $this->_renderLink(
  1251. str_replace('%d', 1, $this->_altFirst),
  1252. $this->_firstPagePre . $this->_firstPageText . $this->_firstPagePost
  1253. ) . $this->_spacesBefore . $this->_spacesAfter;
  1254. }
  1255. // }}}
  1256. // {{{ _printLastPage()
  1257. /**
  1258. * Print [numPages()]
  1259. *
  1260. * @return string String with link to last page,
  1261. * or empty string if this is the 1st page.
  1262. * @access private
  1263. */
  1264. function _printLastPage()
  1265. {
  1266. if ($this->isLastPage()) {
  1267. return '';
  1268. }
  1269. $this->_linkData[$this->_urlVar] = $this->_totalPages;
  1270. return $this->_renderLink(
  1271. str_replace('%d', $this->_totalPages, $this->_altLast),
  1272. $this->_lastPagePre . $this->_lastPageText . $this->_lastPagePost
  1273. );
  1274. }
  1275. // }}}
  1276. // {{{ _setFirstLastText()
  1277. /**
  1278. * sets the private _firstPageText, _lastPageText variables
  1279. * based on whether they were set in the options
  1280. *
  1281. * @return void
  1282. * @access private
  1283. */
  1284. function _setFirstLastText()
  1285. {
  1286. if ($this->_firstPageText == '') {
  1287. $this->_firstPageText = '1';
  1288. }
  1289. if ($this->_lastPageText == '') {
  1290. $this->_lastPageText = $this->_totalPages;
  1291. }
  1292. }
  1293. // }}}
  1294. // {{{ _http_build_query_wrapper()
  1295. /**
  1296. * This is a slightly modified version of the http_build_query() function;
  1297. * it heavily borrows code from PHP_Compat's http_build_query().
  1298. * The main change is the usage of htmlentities instead of urlencode,
  1299. * since it's too aggressive
  1300. *
  1301. * @param array $data array of querystring values
  1302. *
  1303. * @return string
  1304. * @access private
  1305. */
  1306. function _http_build_query_wrapper($data)
  1307. {
  1308. $data = (array)$data;
  1309. if (empty($data)) {
  1310. return '';
  1311. }
  1312. $separator = ini_get('arg_separator.output');
  1313. if ($separator == '&amp;') {
  1314. $separator = '&'; //the string is escaped by htmlentities anyway...
  1315. }
  1316. $tmp = array ();
  1317. foreach ($data as $key => $val) {
  1318. if (is_scalar($val)) {
  1319. //array_push($tmp, $key.'='.$val);
  1320. $val = urlencode($val);
  1321. array_push($tmp, $key .'='. str_replace('%2F', '/', $val));
  1322. continue;
  1323. }
  1324. // If the value is an array, recursively parse it
  1325. if (is_array($val)) {
  1326. array_push($tmp, $this->__http_build_query($val, urlencode($key)));
  1327. continue;
  1328. }
  1329. }
  1330. return implode($separator, $tmp);
  1331. }
  1332. // }}}
  1333. // {{{ __http_build_query()
  1334. /**
  1335. * Helper function
  1336. *
  1337. * @param array $array array of querystring values
  1338. * @param string $name key
  1339. *
  1340. * @return string
  1341. * @access private
  1342. */
  1343. function __http_build_query($array, $name)
  1344. {
  1345. $tmp = array ();
  1346. $separator = ini_get('arg_separator.output');
  1347. if ($separator == '&amp;') {
  1348. $separator = '&'; //the string is escaped by htmlentities anyway...
  1349. }
  1350. foreach ($array as $key => $value) {
  1351. if (is_array($value)) {
  1352. //array_push($tmp, $this->__http_build_query($value, sprintf('%s[%s]', $name, $key)));
  1353. array_push($tmp, $this->__http_build_query($value, $name.'%5B'.$key.'%5D'));
  1354. } elseif (is_scalar($value)) {
  1355. //array_push($tmp, sprintf('%s[%s]=%s', $name, htmlentities($key), htmlentities($value)));
  1356. array_push($tmp, $name.'%5B'.urlencode($key).'%5D='.urlencode($value));
  1357. } elseif (is_object($value)) {
  1358. //array_push($tmp, $this->__http_build_query(get_object_vars($value), sprintf('%s[%s]', $name, $key)));
  1359. array_push($tmp, $this->__http_build_query(get_object_vars($value), $name.'%5B'.$key.'%5D'));
  1360. }
  1361. }
  1362. return implode($separator, $tmp);
  1363. }
  1364. // }}}
  1365. // {{{ _isEncoded()
  1366. /**
  1367. * Helper function
  1368. * Check if a string is an encoded multibyte string
  1369. *
  1370. * @param string $string string to check
  1371. *
  1372. * @return boolean
  1373. * @access private
  1374. */
  1375. function _isEncoded($string)
  1376. {
  1377. $hexchar = '&#[\dA-Fx]{2,};';
  1378. return preg_match("/^(\s|($hexchar))*$/Uims", $string) ? true : false;
  1379. }
  1380. // }}}
  1381. // {{{ raiseError()
  1382. /**
  1383. * conditionally includes PEAR base class and raise an error
  1384. *
  1385. * @param string $msg Error message
  1386. * @param integer $code Error code
  1387. *
  1388. * @return PEAR_Error
  1389. * @access private
  1390. */
  1391. function raiseError($msg, $code)
  1392. {
  1393. include_once 'PEAR.php';
  1394. if (empty($this->_pearErrorMode)) {
  1395. $this->_pearErrorMode = PEAR_ERROR_RETURN;
  1396. }
  1397. return PEAR::raiseError($msg, $code, $this->_pearErrorMode);
  1398. }
  1399. // }}}
  1400. // {{{ setOptions()
  1401. /**
  1402. * Set and sanitize options
  1403. *
  1404. * @param mixed $options An associative array of option names and their values
  1405. *
  1406. * @return integer error code (PAGER_OK on success)
  1407. * @access public
  1408. */
  1409. function setOptions($options)
  1410. {
  1411. foreach ($options as $key => $value) {
  1412. if (in_array($key, $this->_allowed_options) && (!is_null($value))) {
  1413. $this->{'_' . $key} = $value;
  1414. }
  1415. }
  1416. //autodetect http method
  1417. if (!isset($options['httpMethod'])
  1418. && !isset($_GET[$this->_urlVar])
  1419. && isset($_POST[$this->_urlVar])
  1420. ) {
  1421. $this->_httpMethod = 'POST';
  1422. } else {
  1423. $this->_httpMethod = strtoupper($this->_httpMethod);
  1424. }
  1425. if (substr($this->_path, -1, 1) == '/') {
  1426. $this->_fileName = ltrim($this->_fileName, '/'); //strip leading slash
  1427. }
  1428. if ($this->_append) {
  1429. if ($this->_fixFileName) {
  1430. $this->_fileName = PAGER_CURRENT_FILENAME; //avoid possible user error;
  1431. }
  1432. $this->_url = $this->_path.(empty($this->_path) ? '' : '/').$this->_fileName;
  1433. } else {
  1434. $this->_url = $this->_path;
  1435. if (0 != strncasecmp($this->_fileName, 'javascript', 10)) {
  1436. $this->_url .= (empty($this->_path) ? '' : '/');
  1437. }
  1438. if (false === strpos($this->_fileName, '%d')) {
  1439. trigger_error($this->errorMessage(ERROR_PAGER_INVALID_USAGE), E_USER_WARNING);
  1440. }
  1441. }
  1442. if (substr($this->_url, 0, 2) == '//') {
  1443. $this->_url = substr($this->_url, 1);
  1444. }
  1445. if (false === strpos($this->_altPage, '%d')) {
  1446. //by default, append page number at the end
  1447. $this->_altPage .= ' %d';
  1448. }
  1449. $this->_classString = '';
  1450. if (strlen($this->_linkClass)) {
  1451. $this->_classString = 'class="'.$this->_linkClass.'"';
  1452. }
  1453. if (strlen($this->_curPageLinkClassName)) {
  1454. $this->_curPageSpanPre .= '<span class="'.$this->_curPageLinkClassName.'">';
  1455. $this->_curPageSpanPost = '</span>' . $this->_curPageSpanPost;
  1456. }
  1457. $this->_perPage = max($this->_perPage, 1); //avoid possible user errors
  1458. if ($this->_useSessions && !isset($_SESSION)) {
  1459. session_start();
  1460. }
  1461. if (!empty($_REQUEST[$this->_sessionVar])) {
  1462. $this->_perPage = max(1, (int)$_REQUEST[$this->_sessionVar]);
  1463. if ($this->_useSessions) {
  1464. $_SESSION[$this->_sessionVar] = $this->_perPage;
  1465. }
  1466. }
  1467. if (!empty($_SESSION[$this->_sessionVar]) && $this->_useSessions) {
  1468. $this->_perPage = $_SESSION[$this->_sessionVar];
  1469. }
  1470. if ($this->_closeSession) {
  1471. session_write_close();
  1472. }
  1473. // Modified by Ivan Tcholakov, 16-MAR-2010. Suppressing warnings on PHP 5.3
  1474. //$this->_spacesBefore = str_repeat('&nbsp;', $this->_spacesBeforeSeparator);
  1475. //$this->_spacesAfter = str_repeat('&nbsp;', $this->_spacesAfterSeparator);
  1476. $this->_spacesBefore = str_repeat('&nbsp;', intval($this->_spacesBeforeSeparator));
  1477. $this->_spacesAfter = str_repeat('&nbsp;', intval($this->_spacesAfterSeparator));
  1478. //
  1479. if (isset($_REQUEST[$this->_urlVar]) && empty($options['currentPage'])) {
  1480. $this->_currentPage = (int)$_REQUEST[$this->_urlVar];
  1481. }
  1482. $this->_currentPage = max($this->_currentPage, 1);
  1483. $this->_linkData = $this->_getLinksData();
  1484. return PAGER_OK;
  1485. }
  1486. // }}}
  1487. // {{{ getOption()
  1488. /**
  1489. * Return the current value of a given option
  1490. *
  1491. * @param string $name option name
  1492. *
  1493. * @return mixed option value
  1494. * @access public
  1495. */
  1496. function getOption($name)
  1497. {
  1498. if (!in_array($name, $this->_allowed_options)) {
  1499. $msg = 'invalid option: '.$name;
  1500. return $this->raiseError($msg, ERROR_PAGER_INVALID);
  1501. }
  1502. return $this->{'_' . $name};
  1503. }
  1504. // }}}
  1505. // {{{ getOptions()
  1506. /**
  1507. * Return an array with all the current pager options
  1508. *
  1509. * @return array list of all the pager options
  1510. * @access public
  1511. */
  1512. function getOptions()
  1513. {
  1514. $options = array();
  1515. foreach ($this->_allowed_options as $option) {
  1516. $options[$option] = $this->{'_' . $option};
  1517. }
  1518. return $options;
  1519. }
  1520. // }}}
  1521. // {{{ errorMessage()
  1522. /**
  1523. * Return a textual error message for a PAGER error code
  1524. *
  1525. * @param integer $code error code
  1526. *
  1527. * @return string error message
  1528. * @access public
  1529. */
  1530. function errorMessage($code)
  1531. {
  1532. static $errorMessages;
  1533. if (!isset($errorMessages)) {
  1534. $errorMessages = array(
  1535. ERROR_PAGER => 'unknown error',
  1536. ERROR_PAGER_INVALID => 'invalid',
  1537. ERROR_PAGER_INVALID_PLACEHOLDER => 'invalid format - use "%d" as placeholder.',
  1538. ERROR_PAGER_INVALID_USAGE => 'if $options[\'append\'] is set to false, '
  1539. .' $options[\'fileName\'] MUST contain the "%d" placeholder.',
  1540. ERROR_PAGER_NOT_IMPLEMENTED => 'not implemented'
  1541. );
  1542. }
  1543. return (isset($errorMessages[$code]) ?
  1544. $errorMessages[$code] : $errorMessages[ERROR_PAGER]);
  1545. }
  1546. // }}}
  1547. }
  1548. ?>