Source for file Basket.class.php

Documentation is available at Basket.class.php

  1. <?php
  2. /**
  3. * @since 8/5/05
  4. * @package polyphony.basket
  5. *
  6. * @copyright Copyright &copy; 2005, Middlebury College
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  8. *
  9. * @version $Id: Basket.class.php,v 1.17 2007/09/19 14:04:43 adamfranco Exp $
  10. */
  11.  
  12. /**
  13. * A Basket is a session-persistant ordered collection of Asset Ids. Items can be
  14. * added and removed from it, as well as its contents viewed.
  15. *
  16. * @since 8/5/05
  17. * @package polyphony.basket
  18. *
  19. * @copyright Copyright &copy; 2005, Middlebury College
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  21. *
  22. * @version $Id: Basket.class.php,v 1.17 2007/09/19 14:04:43 adamfranco Exp $
  23. */
  24. class Basket
  25. extends OrderedSet
  26. {
  27. /*******************************************************
  28. * Class Methods - Instance-Creation/Singlton
  29. *********************************************************/
  30.  
  31.  
  32. /**
  33. * Get the instance of the Basket.
  34. * The Basket class implements the Singleton pattern. There is only ever
  35. * on instance of the Basket object and it is accessed only via the
  36. * Basket::instance() method.
  37. *
  38. * @return object Basket
  39. * @access public
  40. * @since 5/26/05
  41. * @static
  42. */
  43. function instance () {
  44. if (!isset($_SESSION['__basket'])) {
  45. $_SESSION['__basket'] = new Basket();
  46. }
  47. return $_SESSION['__basket'];
  48. }
  49.  
  50. /*******************************************************
  51. * Instance Methods
  52. *********************************************************/
  53.  
  54. /**
  55. * The constructor.
  56. * @access public
  57. * @return void
  58. ***/
  59. function Basket() {
  60. // Verify that there is only one instance of Harmoni.
  61. $backtrace = debug_backtrace();
  62. if (false && $GLOBALS['BASKET_INSTANTIATED']
  63. || !(
  64. strtolower($backtrace[1]['class']) == strtolower('Basket')
  65. && $backtrace[1]['function'] == 'instance'
  66. // && $backtrace[1]['type'] == '::' // PHP 5.2.1 seems to get this wrong
  67. ))
  68. {
  69. die("\n<dl style='border: 1px solid #F00; padding: 10px;'>"
  70. ."\n\t<dt><strong>Invalid Basket instantiation at...</strong></dt>"
  71. ."\n\t<dd> File: ".$backtrace[0]['file']
  72. ."\n\t\t<br/> Line: ".$backtrace[0]['line']
  73. ."\n\t</dd>"
  74. ."\n\t<dt><strong>Access Basket with <em>Basket::instance()</em></strong></dt>"
  75. ."\n\t<dt><strong>Backtrace:</strong></dt>"
  76. ."\n\t<dd>".printDebugBacktrace(debug_backtrace(), true)."</dd>"
  77. ."\n\t<dt><strong>PHP Version:</strong></dt>"
  78. ."\n\t<dd>".phpversion()."</dd>"
  79. ."\n</dl>");
  80. }
  81. $idManager = Services::getService("Id");
  82. $this->OrderedSet($idManager->getId("__basket"));
  83. }
  84. /**
  85. * removes unauthorized assets from the basket
  86. *
  87. * @return void
  88. * @access public
  89. * @since 12/14/05
  90. */
  91. function clean () {
  92. $authZ = Services::getService("AuthZ");
  93. $idManager = Services::getService("Id");
  94.  
  95. $this->reset();
  96. while ($this->hasNext()) {
  97. $id =$this->next();
  98. if (!$authZ->isUserAuthorized(
  99. $idManager->getId("edu.middlebury.authorization.view"), $id))
  100. {
  101. $this->removeItem($id);
  102. $this->reset();
  103. }
  104. }
  105. $this->reset();
  106. }
  107. /**
  108. * Return an XHTML string of a small version of the basket for use in a header.
  109. * Includes a link and the number of items in it.
  110. *
  111. * @param integer $level The level of component to return
  112. * @return object Component
  113. * @access public
  114. * @since 8/5/05
  115. */
  116. function getSmallBasketBlock ($level = ALERT_BLOCK) {
  117. $block = new Block(
  118. "<div id='basket_small'>\n".$this->getSmallBasketHtml()."\n</div>",
  119. $level);
  120. // controlling JS
  121. ob_start();
  122. $harmoni = Harmoni::instance();
  123. $placeHolderUrl = POLYPHONY_PATH."/icons/1x1.png";
  124. $harmoni->request->startNamespace("basket");
  125. $addBasketURL = str_replace("&amp;", "&",
  126. $harmoni->request->quickURL("basket", "addAjax", array("assets" => "xxxxx")));
  127. $emptyBasketURL = str_replace("&amp;", "&",
  128. $harmoni->request->quickURL("basket", "emptyAjax"));
  129. $harmoni->request->endNamespace();
  130. print<<<END
  131.  
  132. <script type='text/javascript'>
  133. // <![CDATA[
  134.  
  135. /**
  136. * Basket class
  137. * Contains functions for interacting with the basket
  138. *
  139. * @since 5/5/06
  140. */
  141. function Basket () {
  142. }
  143. /**
  144. * Add the asset ids to the basket and refresh the basket contents
  145. *
  146. * @param array idArray
  147. * @return void
  148. * @access public
  149. * @since 5/2/06
  150. */
  151. Basket.addAssets = function ( idArray ) {
  152. if (idArray.length == 0)
  153. return;
  154. // Get the basket element
  155. var basketElement = document.get_element_by_id('basket_small');
  156. var basketContentsElement = document.get_element_by_id('basket_small_contents');
  157. // Add placeholders to the basket
  158. for (var i = 0; i < idArray.length; i++) {
  159. var elem = document.createElement('img');
  160. elem.style.border = '1px solid';
  161. elem.style.margin = '2px';
  162. elem.style.marginRight = '1px';
  163. elem.style.height = '60px';
  164. elem.style.width = '60px';
  165. elem.style.verticalAlign = 'middle';
  166. elem.src = '$placeHolderUrl';
  167. basketContentsElement.appendChild(elem);
  168. basketContentsElement.appendChild(document.createTextNode("\\n"));
  169. }
  170. // Build the destination url
  171. var addBasketURL = new String('$addBasketURL');
  172. var regex = new RegExp("xxxxx");
  173. addBasketURL = addBasketURL.replace(regex, idArray.join(','));
  174. Basket.reload(addBasketURL);
  175. }
  176. /**
  177. * Empty the basket and refresh the small basket contents
  178. *
  179. * @return void
  180. * @access public
  181. * @since 5/5/06
  182. */
  183. Basket.empty = function () {
  184. Basket.reload('$emptyBasketURL');
  185. }
  186. /**
  187. * Reload the small basket display via AJAX
  188. *
  189. * @param string url
  190. * @return void
  191. * @access public
  192. * @since 5/5/06
  193. */
  194. Basket.reload = function ( url ) {
  195. /*********************************************************
  196. * Do the AJAX request and repopulate the basket with
  197. * the contents of the result
  198. *********************************************************/
  199. // branch for native XMLHttpRequest object (Mozilla, Safari, etc)
  200. if (window.XMLHttpRequest)
  201. var req = new XMLHttpRequest();
  202. // branch for IE/Windows ActiveX version
  203. else if (window.ActiveXObject)
  204. var req = new ActiveXObject("Microsoft.XMLHTTP");
  205. if (req) {
  206. req.onreadystatechange = function () {
  207. // only if req shows "loaded"
  208. if (req.readyState == 4) {
  209. // only if we get a good load should we continue.
  210. if (req.status == 200) {
  211. var basketElement = document.get_element_by_id('basket_small');
  212. basketElement.innerHTML = req.responseText;
  213. Basket.removeBorders();
  214. } else {
  215. alert("There was a problem retrieving the XML data:\\n" +
  216. req.statusText);
  217. }
  218. }
  219. }
  220. req.open("GET", url, true);
  221. req.send(null);
  222. }
  223. }
  224. /**
  225. * Remove borders from div's surrounding images (in the basket) if the
  226. * images are loaded.
  227. *
  228. * @param element parentNode
  229. * @return boolean True if all are loaded
  230. * @access public
  231. * @since 6/15/06
  232. */
  233. Basket.removeBoardersForCompletedImages = function (parentNode) {
  234. var allLoaded = true;
  235. for (var i in parentNode.childNodes) {
  236. var child = parentNode.childNodes[i];
  237. if (child.nodeType == 1 && child.tagName.toLowerCase() == "div") {
  238. for (var j in child.childNodes) {
  239. var grandchild = child.childNodes[j];
  240. if (grandchild.nodeType == 1 && grandchild.tagName.toLowerCase() == "img") {
  241. if (grandchild.height > 0 && grandchild.width > 0) {
  242. child.style.border='0px';
  243. child.style.margin='3px';
  244. /* Resize images for IE */
  245. if (grandchild.height > 50 || grandchild.width > 50) {
  246. grandchild.width = 50;
  247. }
  248. } else {
  249. allLoaded = false;
  250. }
  251. }
  252. }
  253. }
  254. }
  255. return allLoaded;
  256. }
  257. /**
  258. * Loop waiting for images to load and then remove their boarders if they are
  259. * loaded.
  260. *
  261. * @return void
  262. * @access public
  263. * @since 6/15/06
  264. */
  265. Basket.removeBorders = function () {
  266. if (!Basket.removeBoardersForCompletedImages(
  267. document.get_element_by_id('basket_small_contents')))
  268. {
  269. window.setTimeout('Basket.removeBorders()', 100);
  270. }
  271. }
  272. // ]]>
  273. </script>
  274. END;
  275. $block->setPreHTML(ob_get_clean());
  276. return $block;
  277. }
  278. /**
  279. * Answer the XHTML string of the small version of the basket contents
  280. *
  281. * @param <##>
  282. * @return <##>
  283. * @access public
  284. * @since 5/2/06
  285. */
  286. function getSmallBasketHtml () {
  287. $harmoni = Harmoni::instance();
  288. $harmoni->request->startNamespace("basket");
  289. $this->clean();
  290. ob_start();
  291. print "\n\t<a href='";
  292. print $harmoni->request->quickURL("basket", "view");
  293. print "'>";
  294. print _("Selection: ");
  295. print "(".$this->count()." "._("items").")";
  296. print "</a>";
  297. print "\n\t<div id='basket_small_contents' style='text-align: left; min-width: 200px;'>";
  298. $this->reset();
  299. $i = 0;
  300. if ($this->hasNext()) {
  301. while ($this->hasNext()) {
  302. $id =$this->next();
  303. $thumbnailURL = RepositoryInputOutputModuleManager::getThumbnailUrlForAsset($id);
  304. if ($thumbnailURL !== FALSE) {
  305. print "\n\t<div style='border: 1px solid; height: 60px; width: 60px; float: left; text-align: center; vertical-align: middle; padding: 0px; margin: 2px;'>";
  306. // The image
  307. print "\n\t\t<img class='thumbnail_image' \n\t\t\tsrc='$thumbnailURL' \n\t\t\talt='Thumbnail Image'";
  308. print " \n\t\t\tstyle='max-height: 50px; max-width: 50px; vertical-align: middle; margin: 5px; cursor: pointer;'";
  309. // border removal
  310. print " \n\t\t\tonload=\"if (this.parentNode) { this.parentNode.style.border='0px'; this.parentNode.style.margin='3px'; } /* Resize images for IE */ if (this.height > 50 || this.width > 50) {this.width = 50;}\" ";
  311. // Viewer Link
  312. print " \n\t\t\tonclick='window.open(";
  313. print '"'.VIEWER_URL."?&amp;source=";
  314. print urlencode($harmoni->request->quickURL("basket", "browse_xml"));
  315. print '&amp;start='.$i.'", ';
  316. print '"_blank", ';
  317. print '"toolbar=no,location=no,directories=no,status=yes,scrollbars=yes,resizable=yes,copyhistory=no,width=600,height=500"';
  318. print ");'";
  319. print "\n\t\t/>";
  320. print "\n\t</div>";
  321. $i++;
  322. }
  323. }
  324. }
  325. print "\n\t</div>";
  326. print <<< END
  327. <script type='text/javascript'>
  328. // <![CDATA[
  329. Basket.removeBorders();
  330. // ]]>
  331. </script>
  332. END;
  333. if ($this->count()) {
  334. print "\n\t<div style='text-align: right; font-size: small; clear: both;'>";
  335. print "<a onclick='Basket.empty()'>"._("Empty")."</a>";
  336. print "\n\t</div>";
  337. }
  338. $harmoni->request->endNamespace();
  339. return ob_get_clean();
  340. }
  341. /**
  342. * Answer the link to add a particular id to the basket
  343. *
  344. * @param object Id $assetId
  345. * @return string XHTML
  346. * @access public
  347. * @since 5/2/06
  348. */
  349. function getAddLink ( $assetId ) {
  350. $harmoni = Harmoni::instance();
  351. $harmoni->request->startNamespace("basket");
  352. ob_start();
  353. print "<a ";
  354. print " style='cursor: pointer;'";
  355. print " onclick='Basket.addAssets(new Array(\"".$assetId->getIdString()."\"));'";
  356. print ">"._('+ Selection');
  357. print "</a>";
  358. $harmoni->request->endNamespace();
  359. $harmoni->history->markReturnURL("polyphony/basket",
  360. $harmoni->request->mkURLWithPassthrough());
  361. return ob_get_clean();
  362. }
  363. }
  364.  
  365. ?>

Documentation generated on Wed, 19 Sep 2007 10:21:45 -0400 by phpDocumentor 1.3.0RC3