Common.php 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729
  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 class="btn btn-outline-secondary" 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. throw new Exception($msg);
  1394. /*
  1395. include_once 'PEAR.php';
  1396. if (empty($this->_pearErrorMode)) {
  1397. $this->_pearErrorMode = PEAR_ERROR_RETURN;
  1398. }
  1399. return PEAR::raiseError($msg, $code, $this->_pearErrorMode);*/
  1400. }
  1401. // }}}
  1402. // {{{ setOptions()
  1403. /**
  1404. * Set and sanitize options
  1405. *
  1406. * @param mixed $options An associative array of option names and their values
  1407. *
  1408. * @return integer error code (PAGER_OK on success)
  1409. * @access public
  1410. */
  1411. function setOptions($options)
  1412. {
  1413. foreach ($options as $key => $value) {
  1414. if (in_array($key, $this->_allowed_options) && (!is_null($value))) {
  1415. $this->{'_' . $key} = $value;
  1416. }
  1417. }
  1418. //autodetect http method
  1419. if (!isset($options['httpMethod'])
  1420. && !isset($_GET[$this->_urlVar])
  1421. && isset($_POST[$this->_urlVar])
  1422. ) {
  1423. $this->_httpMethod = 'POST';
  1424. } else {
  1425. $this->_httpMethod = strtoupper($this->_httpMethod);
  1426. }
  1427. if (substr($this->_path, -1, 1) == '/') {
  1428. $this->_fileName = ltrim($this->_fileName, '/'); //strip leading slash
  1429. }
  1430. if ($this->_append) {
  1431. if ($this->_fixFileName) {
  1432. $this->_fileName = PAGER_CURRENT_FILENAME; //avoid possible user error;
  1433. }
  1434. $this->_url = $this->_path.(empty($this->_path) ? '' : '/').$this->_fileName;
  1435. } else {
  1436. $this->_url = $this->_path;
  1437. if (0 != strncasecmp($this->_fileName, 'javascript', 10)) {
  1438. $this->_url .= (empty($this->_path) ? '' : '/');
  1439. }
  1440. if (false === strpos($this->_fileName, '%d')) {
  1441. trigger_error($this->errorMessage(ERROR_PAGER_INVALID_USAGE), E_USER_WARNING);
  1442. }
  1443. }
  1444. if (substr($this->_url, 0, 2) == '//') {
  1445. $this->_url = substr($this->_url, 1);
  1446. }
  1447. if (false === strpos($this->_altPage, '%d')) {
  1448. //by default, append page number at the end
  1449. $this->_altPage .= ' %d';
  1450. }
  1451. $this->_classString = '';
  1452. if (strlen($this->_linkClass)) {
  1453. $this->_classString = 'class="'.$this->_linkClass.'"';
  1454. }
  1455. if (strlen($this->_curPageLinkClassName)) {
  1456. $this->_curPageSpanPre .= '<span class="'.$this->_curPageLinkClassName.'">';
  1457. $this->_curPageSpanPost = '</span>' . $this->_curPageSpanPost;
  1458. }
  1459. $this->_perPage = max($this->_perPage, 1); //avoid possible user errors
  1460. if ($this->_useSessions && !isset($_SESSION)) {
  1461. session_start();
  1462. }
  1463. if (!empty($_REQUEST[$this->_sessionVar])) {
  1464. $this->_perPage = max(1, (int)$_REQUEST[$this->_sessionVar]);
  1465. if ($this->_useSessions) {
  1466. $_SESSION[$this->_sessionVar] = $this->_perPage;
  1467. }
  1468. }
  1469. if (!empty($_SESSION[$this->_sessionVar]) && $this->_useSessions) {
  1470. $this->_perPage = $_SESSION[$this->_sessionVar];
  1471. }
  1472. if ($this->_closeSession) {
  1473. session_write_close();
  1474. }
  1475. // Modified by Ivan Tcholakov, 16-MAR-2010. Suppressing warnings on PHP 5.3
  1476. //$this->_spacesBefore = str_repeat('&nbsp;', $this->_spacesBeforeSeparator);
  1477. //$this->_spacesAfter = str_repeat('&nbsp;', $this->_spacesAfterSeparator);
  1478. $this->_spacesBefore = str_repeat('&nbsp;', intval($this->_spacesBeforeSeparator));
  1479. $this->_spacesAfter = str_repeat('&nbsp;', intval($this->_spacesAfterSeparator));
  1480. //
  1481. if (isset($_REQUEST[$this->_urlVar]) && empty($options['currentPage'])) {
  1482. $this->_currentPage = (int)$_REQUEST[$this->_urlVar];
  1483. }
  1484. $this->_currentPage = max($this->_currentPage, 1);
  1485. $this->_linkData = $this->_getLinksData();
  1486. return PAGER_OK;
  1487. }
  1488. // }}}
  1489. // {{{ getOption()
  1490. /**
  1491. * Return the current value of a given option
  1492. *
  1493. * @param string $name option name
  1494. *
  1495. * @return mixed option value
  1496. * @access public
  1497. */
  1498. function getOption($name)
  1499. {
  1500. if (!in_array($name, $this->_allowed_options)) {
  1501. $msg = 'invalid option: '.$name;
  1502. return $this->raiseError($msg, ERROR_PAGER_INVALID);
  1503. }
  1504. return $this->{'_' . $name};
  1505. }
  1506. // }}}
  1507. // {{{ getOptions()
  1508. /**
  1509. * Return an array with all the current pager options
  1510. *
  1511. * @return array list of all the pager options
  1512. * @access public
  1513. */
  1514. function getOptions()
  1515. {
  1516. $options = array();
  1517. foreach ($this->_allowed_options as $option) {
  1518. $options[$option] = $this->{'_' . $option};
  1519. }
  1520. return $options;
  1521. }
  1522. // }}}
  1523. // {{{ errorMessage()
  1524. /**
  1525. * Return a textual error message for a PAGER error code
  1526. *
  1527. * @param integer $code error code
  1528. *
  1529. * @return string error message
  1530. * @access public
  1531. */
  1532. function errorMessage($code)
  1533. {
  1534. static $errorMessages;
  1535. if (!isset($errorMessages)) {
  1536. $errorMessages = array(
  1537. ERROR_PAGER => 'unknown error',
  1538. ERROR_PAGER_INVALID => 'invalid',
  1539. ERROR_PAGER_INVALID_PLACEHOLDER => 'invalid format - use "%d" as placeholder.',
  1540. ERROR_PAGER_INVALID_USAGE => 'if $options[\'append\'] is set to false, '
  1541. .' $options[\'fileName\'] MUST contain the "%d" placeholder.',
  1542. ERROR_PAGER_NOT_IMPLEMENTED => 'not implemented'
  1543. );
  1544. }
  1545. return (isset($errorMessages[$code]) ?
  1546. $errorMessages[$code] : $errorMessages[ERROR_PAGER]);
  1547. }
  1548. // }}}
  1549. }
  1550. ?>