ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilShopPurchaseGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once "./Services/Object/classes/class.ilObjectGUI.php";
5 
16 {
17  public $ctrl;
18  public $lng;
19  public $tpl;
20 
21  public $object = null;
22  public $pobject = null;
23  public $cur_row_type = null;
24  public $price_obj = null;
25  public $sc_obj = null;
26 
27  public function __construct($a_ref_id)
28  {
29  global $ilCtrl,$lng,$ilErr,$tpl,$ilTabs;
30 
31  $this->ctrl = $ilCtrl;
32  $this->ctrl->saveParameter($this, array("ref_id"));
33 
34  $this->ilErr = $ilErr;
35 
36  $this->lng = $lng;
37  $this->lng->loadLanguageModule('payment');
38 
39  $this->tpl = $tpl;
40 
41  $this->ref_id = $a_ref_id;
42 
43  $this->object = ilObjectFactory::getInstanceByRefId($this->ref_id, false);
44 
45  $this->tpl->getStandardTemplate();
46 
47  $ilTabs->clearTargets();
48  $ilTabs->addTarget('buy', $this->ctrl->getLinkTarget($this, 'showDetails').'&purchasetype=buy');
49  $ilTabs->addTarget('payment_demo', $this->ctrl->getLinkTarget($this, 'showDemoVersion').'&purchasetype=demo');
50 
51  $this->ctrl->setParameter($this, 'purchasetype', ($_GET['purchasetype'] == 'demo' ? 'demo' : 'buy'));
52 
53  }
54 
58  public function executeCommand()
59  {
60  $cmd = $this->ctrl->getCmd();
61  switch($this->ctrl->getCmdClass())
62  {
63  case 'ilpageobjectgui':
64  $this->__initPaymentObject();
65  include_once 'Services/Style/classes/class.ilObjStyleSheet.php';
66  include_once 'Services/COPage/classes/class.ilPageObjectGUI.php';
67  $page_gui = new ilPageObjectGUI('shop', $this->pobject->getPobjectId());
68  $this->ctrl->forwardCommand($page_gui);
69  return true;
70  break;
71  }
72 
73 
74  switch($cmd)
75  {
76  case 'addToShoppingCart':
77  break;
78 
79  default:
80  if(!in_array($cmd, array('showDemoVersion', 'showDetails', 'addToShoppingCart')))
81  {
82  $cmd = ($_GET['purchasetype'] == 'demo' ? 'showDemoVersion' : 'showDetails');
83  }
84  break;
85  }
86  $this->$cmd();
87  return true;
88  }
89 
90  public function showDemoVersion()
91  {
92  global $ilMainMenu, $ilTabs, $ilToolbar;
93 
94  $this->__initPaymentObject();
95  $this->__initPricesObject();
96  $this->__initShoppingCartObject();
97 
98  $ilToolbar->addButton($this->lng->txt('payment_back_to_shop'),'ilias.php?baseClass=ilShopController');
99 
100  $ilTabs->setTabActive('payment_demo');
101  $ilMainMenu->setActive('shop');
102  $this->tpl->addBlockFile('ADM_CONTENT', 'adm_content', 'tpl.shop_abstract_details.html', 'Services/Payment');
103 
104  $this->tpl->setVariable("DETAILS_FORMACTION",$this->ctrl->getFormAction($this));
105 
106  if($this->object)
107  {
108  $this->tpl->setVariable("TYPE_IMG",ilUtil::getImagePath('icon_'.$this->object->getType().'_b.png'));
109  $this->tpl->setVariable("ALT_IMG",$this->lng->txt('obj_'.$this->object->getType()));
110  $this->tpl->setVariable("TITLE",$this->object->getTitle());
111  }
112  else
113  {
114  $this->tpl->setVariable("TITLE",$this->lng->txt('object_not_found'));
115  }
116 
117 
118 
119  // abstracts
120  if(($abstract_html = $this->__getAbstractHTML($this->pobject->getPobjectId())) != '')
121  {
122  $this->tpl->setCurrentBlock('abstract_block');
123  $this->tpl->setVariable('TXT_ABSTRACT', $this->lng->txt('pay_abstract'));
124  $this->tpl->setVariable('ABSTRACT_HTML', $abstract_html);
125  $this->tpl->parseCurrentBlock();
126  }
127 
128  // public content ilias lm
129  global $ilObjDataCache;
130  if($ilObjDataCache->lookupType($ilObjDataCache->lookupObjId($this->pobject->getRefId())) == 'lm')
131  {
132  include_once 'Modules/LearningModule/classes/class.ilShopPublicSectionSelector.php';
133  $exp = new ilShopPublicSectionSelector($this->ctrl->getLinkTarget($this, 'layout'),
134  ilObjectFactory::getInstanceByRefId($this->pobject->getRefId()), get_class($this));
135  $exp->setTargetGet('obj_id');
136  $exp->setOutput(0);
137  $output = $exp->getOutput();
138 
139  if(trim($output) != '')
140  {
141 
142  $this->tpl->setCurrentBlock('public_content_block');
143  $this->tpl->setVariable('TXT_CONTENT', $this->lng->txt('content'));
144  $this->tpl->setVariable('PUBLIC_CONTENT_HTML', $output);
145  $this->tpl->parseCurrentBlock();
146  }
147  }
148  else if($ilObjDataCache->lookupType($ilObjDataCache->lookupObjId($this->pobject->getRefId())) == 'crs')
149  {
150  $oCourse = ilObjectFactory::getInstanceByRefId($this->pobject->getRefId());
151  $items = $oCourse->getSubItems();
152  $this->__getCourseItemsHTML($items);
153  }
154  }
155 
156  private function getItemsByObjType($items, $type_group)
157  {
158  return is_array($items[$type_group]) ? $items[$type_group] : array();
159  }
160 
161  private function __getCourseItemsHTML($container_items)
162  {
163  global $objDefinition, $ilSetting;
164 
165  $output = false;
166 
167  $tpl_sub_items = new ilTemplate('tpl.pay_purchase_demo_list_block.html', true, true, 'Services/Payment');
168 
169  $objtype_groups = $objDefinition->getGroupedRepositoryObjectTypes(
170  array('cat', 'crs', 'grp', 'fold')
171  );
172 
173  foreach($objtype_groups as $grp => $grpdata)
174  {
175 // $title = $this->lng->txt('objs_'.$grp);
176  $items = $this->getItemsByObjType($container_items, $grp);
177 
178  $item_html = array();
179  $rel_header = 'th_'.$grp;
180 
181  if(count($items) > 0)
182  {
183  foreach($items as $item)
184  {
185  if($item['title'] != '')
186  {
187  $item_html[] = array(
188  'html' => $item['title'],
189  'item_ref_id' => $item['ref_id'],
190  'item_obj_id' => $item['obj_id']
191  );
192  }
193  }
194 
195  // output block for resource type
196  if(count($item_html) > 0)
197  {
198  $output = true;
199 
200  // add a header for each resource type
201  if($ilSetting->get('icon_position_in_lists') == 'item_rows')
202  {
203  $this->addHeaderRow($tpl_sub_items, $grp, false);
204  }
205  else
206  {
207  $this->addHeaderRow($tpl_sub_items, $grp);
208  }
209  $this->resetRowType();
210 
211  // content row
212  foreach($item_html as $item)
213  {
214  if($ilSetting->get('icon_position_in_lists') == 'item_rows')
215  {
216  // BEGIN WebDAV: Use $item_list_gui to determine icon image type
217  $this->addStandardRow($tpl_sub_items, $item['html'], $item['item_obj_id'],
218  $item['item_icon_image_type'],
219  $rel_header);
220  // END WebDAV: Use $item_list_gui to determine icon image type
221  }
222  else
223  {
224  $this->addStandardRow($tpl_sub_items, $item['html'], $item['item_obj_id'], '', $rel_header);
225  }
226  }
227  }
228  }
229  }
230 
231  if($output == true)
232  {
233  $this->tpl->setCurrentBlock('public_content_block');
234  $this->tpl->setVariable('TXT_CONTENT', $this->lng->txt('content'));
235  $this->tpl->setVariable('PUBLIC_CONTENT_HTML', $tpl_sub_items->get());
236  $this->tpl->parseCurrentBlock();
237  }
238  }
239 
240  private function resetRowType()
241  {
242  $this->cur_row_type = "";
243  }
244 
252  private function addHeaderRow($a_tpl, $a_type, $a_show_image = true)
253  {
254  $icon = ilUtil::getImagePath("icon_".$a_type.".png");
255  $title = $this->lng->txt("objs_".$a_type);
256  $header_id = "th_".$a_type;
257 
258  if ($a_show_image)
259  {
260  $a_tpl->setCurrentBlock("container_header_row_image");
261  $a_tpl->setVariable("HEADER_IMG", $icon);
262  $a_tpl->setVariable("HEADER_ALT", $title);
263  }
264  else
265  {
266  $a_tpl->setCurrentBlock("container_header_row");
267  }
268 
269  $a_tpl->setVariable("BLOCK_HEADER_CONTENT", $title);
270  $a_tpl->setVariable("BLOCK_HEADER_ID", $header_id);
271  $a_tpl->parseCurrentBlock();
272  $a_tpl->touchBlock("container_row");
273  }
274 
282  private function addStandardRow(&$a_tpl, $a_html, $a_item_obj_id = "",
283  $a_image_type = "", $a_related_header = "")
284  {
285  global $ilSetting;
286 
287  $this->cur_row_type = ($this->cur_row_type == "row_type_1")
288  ? "row_type_2"
289  : "row_type_1";
290  $a_tpl->touchBlock($this->cur_row_type);
291 
292  if ($a_image_type != "")
293  {
294  if (!is_array($a_image_type) && !in_array($a_image_type, array("lm", "dbk", "htlm", "sahs")))
295  {
296  $icon = ilUtil::getImagePath("icon_".$a_image_type.".png");
297  $title = $this->lng->txt("obj_".$a_image_type);
298  }
299  else
300  {
301  $icon = ilUtil::getImagePath("icon_lm.png");
302  $title = $this->lng->txt("learning_resource");
303  }
304 
305  // custom icon
306  if ($ilSetting->get("custom_icons") &&
307  in_array($a_image_type, array("cat","grp","crs")))
308  {
309  require_once("./Services/Container/classes/class.ilContainer.php");
310  if (($path = ilContainer::_lookupIconPath($a_item_obj_id, "small")) != "")
311  {
312  $icon = $path;
313  }
314  }
315 
316  $a_tpl->setCurrentBlock("block_row_image");
317  $a_tpl->setVariable("ROW_IMG", $icon);
318  $a_tpl->setVariable("ROW_ALT", $title);
319  $a_tpl->parseCurrentBlock();
320  }
321  else
322  {
323  $a_tpl->setVariable("ROW_NBSP", "&nbsp;");
324  }
325  $a_tpl->setCurrentBlock("container_standard_row");
326  $a_tpl->setVariable("BLOCK_ROW_CONTENT", $a_html);
327  $rel_headers = ($a_related_header != "")
328  ? "th_selected_items ".$a_related_header
329  : "th_selected_items";
330  $a_tpl->setVariable("BLOCK_ROW_HEADERS", $rel_headers);
331  $a_tpl->parseCurrentBlock();
332  $a_tpl->touchBlock("container_row");
333  }
334 
335  public function showDetails()
336  {
337  global $ilMainMenu, $ilTabs, $ilToolbar, $ilUser;
338 
339  $this->__initPaymentObject();
340  $this->__initPricesObject();
341  $this->__initShoppingCartObject();
342 
343  $ilToolbar->addButton($this->lng->txt('payment_back_to_shop'),'ilias.php?baseClass=ilShopController');
344 
345  $this->tpl->getStandardTemplate();
346  $ilTabs->setTabActive('buy');
347  $ilMainMenu->setActive('shop');
348 
349  $this->tpl->addBlockFile('ADM_CONTENT', 'adm_content', 'tpl.pay_purchase_details.html', 'Services/Payment');
350 
351  if($this->pobject->getStatus() == $this->pobject->STATUS_EXPIRES)
352  {
353  ilUtil::sendInfo($this->lng->txt('pay_expires_info'));
354 
355  return false;
356  }
357 
358  $extension_prices = array();
359 
360  if($ilUser->getId() != ANONYMOUS_USER_ID)
361  {
362  include_once './Services/Payment/classes/class.ilPaymentBookings.php';
363  $has_extension_price = ilPaymentBookings::_hasAccesstoExtensionPrice(
364  $ilUser->getId(), $this->pobject->getPobjectId());
365 
366 
367  if($has_extension_price)
368  {
369  $extension_prices = $this->price_obj->getExtensionPrices();
370  }
371  }
372 
373  $org_prices = $this->price_obj->getPrices();
374  $tmp_prices = array_merge($org_prices, $extension_prices );
375 
376  $prices = array();
377  foreach($tmp_prices as $price)
378  {
379  // expired prices must be filtered out
380  if($price['price_type'] == ilPaymentPrices::TYPE_DURATION_DATE && $price['duration_until'] < date('Y-m-d'))
381  {
382  //do nothing
383  }
384  else
385  {
386  $prices[] = $price;
387  }
388  }
389 
390  $buyedObject = "";
391  if($this->sc_obj->isInShoppingCart($this->pobject->getPobjectId()))
392  {
393  $buyedObject = $this->sc_obj->getEntry($this->pobject->getPobjectId());
394  if (is_array($prices) &&
395  count($prices) > 1)
396  {
397  ilUtil::sendInfo($this->lng->txt('pay_item_already_in_sc_choose_another'));
398  }
399  else
400  {
401  ilUtil::sendInfo($this->lng->txt('pay_item_already_in_sc'));
402  }
403 
404  $this->tpl->setCurrentBlock("shopping_cart_1");
405 
406  $this->tpl->setVariable("LINK_GOTO_SHOPPING_CART",'ilias.php?baseClass=ilShopController&cmd=redirect&redirect_class=ilShopShoppingCartGUI');
407  $this->tpl->setVariable("TXT_GOTO_SHOPPING_CART", $this->lng->txt('pay_goto_shopping_cart'));
408  $this->tpl->parseCurrentBlock("shopping_cart_1");
409 
410  }
411 
412  $this->ctrl->setParameter($this, "ref_id", $this->pobject->getRefId());
413  $subtype = '';
414  if($this->object)
415  {
416  if($this->object->getType() == 'exc')
417  {
418  $subtype = ' ('.$this->lng->txt($this->pobject->getSubtype()).')';
419  }
420 
421  $this->tpl->setVariable("DETAILS_FORMACTION",$this->ctrl->getFormAction($this));
422  $this->tpl->setVariable("TYPE_IMG",ilUtil::getImagePath('icon_'.$this->object->getType().'_b.png'));
423  $this->tpl->setVariable("ALT_IMG",$this->lng->txt('obj_'.$this->object->getType()));
424  $this->tpl->setVariable("TITLE",$this->object->getTitle().' '.$subtype);
425  }
426  else
427  {
428  $this->tpl->setVariable("DETAILS_FORMACTION",$this->ctrl->getFormAction($this));
429  $this->tpl->setVariable("TITLE",$this->lng->txt('object_not_found'));
430  }
431  // payment infos
432  $this->tpl->setVariable("TXT_INFO",$this->lng->txt('info'));
433 
434  $this->tpl->setVariable("INFO_PAY",$this->lng->txt('pay_info'));
435  if (is_array($buyedObject))
436  {
437  if (is_array($prices) && count($prices) > 1)
438  {
439  $this->tpl->setVariable("INPUT_CMD",'addToShoppingCart');
440  $this->tpl->setVariable("INPUT_VALUE",$this->lng->txt('pay_change_price'));
441  }
442  else
443  {
444  $this->tpl->setVariable("INPUT_CMD",'addToShoppingCart');
445  $this->tpl->setVariable("INPUT_VALUE",$this->lng->txt('pay_add_to_shopping_cart'));
446  }
447  }
448  else
449  {
450  $this->tpl->setVariable("INPUT_CMD",'addToShoppingCart');
451  $this->tpl->setVariable("INPUT_VALUE",$this->lng->txt('pay_add_to_shopping_cart'));
452  }
453 
454  $this->tpl->setVariable("ROWSPAN",count($prices));
455  $this->tpl->setVariable("TXT_PRICES",$this->lng->txt('prices'));
456 
457  if (is_array($prices))
458  {
459  $counter = 0;
460  foreach($prices as $price)
461  {
462  if ($counter == 0)
463  {
464  $placeholderCheckbox = "CHECKBOX";
465  $placeholderDuration = "DURATION";
466  $placeholderPrice = "PRICE";
467  $placeholderDescription = "DESCRIPTION";
468  }
469  else
470  {
471  $placeholderCheckbox = "ROW_CHECKBOX";
472  $placeholderDuration = "ROW_DURATION";
473  $placeholderPrice = "ROW_PRICE";
474  $placeholderDescription = "ROW_DESCRIPTION";
475  }
476  $this->tpl->setCurrentBlock("price_row");
477  if ($buyedObject["price_id"] == $price['price_id'])
478  {
479  $this->tpl->setVariable($placeholderCheckbox,ilUtil::formRadioButton(1,'price_id',$price['price_id']));
480  }
481  else if (count($prices) == 1)
482  {
483  $this->tpl->setVariable($placeholderCheckbox,ilUtil::formRadioButton(1,'price_id',$price['price_id']));
484  }
485  else
486  {
487  $this->tpl->setVariable($placeholderCheckbox,ilUtil::formRadioButton(0,'price_id',$price['price_id']));
488  }
489 
490  switch($price['price_type'])
491  {
493  $this->tpl->setVariable($placeholderDuration,$price['duration'].' '.$this->lng->txt('paya_months').': ');
494  break;
497  $this->tpl->setVariable($placeholderDuration,
498  ilDatePresentation::formatDate(new ilDate($price['duration_from'], IL_CAL_DATE))
499  .' - '.ilDatePresentation::formatDate(new ilDate($price['duration_until'], IL_CAL_DATE)).' -> ');
500  break;
502  $this->tpl->setVariable($placeholderDuration, $this->lng->txt('unlimited_duration').': ');
503  break;
504  }
505 
506  $tmp_price = $price['price'];
507 
508  if($price['extension'] == 1)
509  $extension_txt = '('.$this->lng->txt('extension_price').')';
510  else $extension_txt = '';
511 
512  $this->tpl->setVariable($placeholderPrice, ilPaymentPrices::_formatPriceToString((float)$tmp_price).' '.$extension_txt );
513  if($price['description'] != NULL)
514  {
515  $this->tpl->setVariable($placeholderDescription, $price['description']);
516  }
517  $this->tpl->parseCurrentBlock();
518  $counter++;
519  }
520  }
521  return true;
522  }
523 
524  private function __getAbstractHTML($a_payment_object_id)
525  {
526  // page object
527  include_once 'Services/COPage/classes/class.ilPageObject.php';
528  include_once 'Services/COPage/classes/class.ilPageObjectGUI.php';
529 
530  // if page does not exist, return nothing
531  if(!ilPageObject::_exists('shop', $a_payment_object_id))
532  {
533  return '';
534  }
535 
536  include_once 'Services/Style/classes/class.ilObjStyleSheet.php';
537  // get page object
538  $page_gui = new ilPageObjectGUI('shop', $a_payment_object_id);
539  $page_gui->setIntLinkHelpDefault('StructureObject', $a_payment_object_id);
540  $page_gui->setLinkXML('');
541  $page_gui->setFileDownloadLink($this->ctrl->getLinkTargetByClass('ilPageObjectGUI', 'downloadFile'));
542  $page_gui->setFullscreenLink($this->ctrl->getLinkTargetByClass('ilPageObjectGUI', 'displayMediaFullscreen'));
543  $page_gui->setSourcecodeDownloadScript($this->ctrl->getLinkTargetByClass('ilPageObjectGUI', 'download_paragraph'));
544  $page_gui->setPresentationTitle('');
545  $page_gui->setTemplateOutput(false);
546  $page_gui->setHeader('');
547  $page_gui->setEnabledRepositoryObjects(false);
548  $page_gui->setEnabledFileLists(false);
549  $page_gui->setEnabledPCTabs(true);
550  $page_gui->setEnabledMaps(true);
551 
552  return $page_gui->showPage();
553  }
554 
555  public function addToShoppingCart()
556  {
557  global $ilTabs;
558 
559  $ilTabs->setTabActive('buy');
560 
561  if(!isset($_POST['price_id']))
562  {
563  ilUtil::sendInfo($this->lng->txt('pay_select_price'));
564  $this->showDetails();
565 
566  return true;
567  }
568  else
569  {
570  $this->__initPaymentObject();
571 
572  $this->__initShoppingCartObject();
573 
574  $this->sc_obj->setSessionId(session_id());
575  $this->sc_obj->setPriceId((int) $_POST['price_id']);
576  $this->sc_obj->setPobjectId($this->pobject->getPobjectId());
577  $this->sc_obj->add();
578 
579  ilUtil::redirect('ilias.php?baseClass=ilShopController&cmd=redirect&redirect_class=ilshopshoppingcartgui');
580 
581  return true;
582  }
583  }
584 
585  // PRIVATE
586  private function __initShoppingCartObject()
587  {
588  global $ilUser;
589  include_once './Services/Payment/classes/class.ilPaymentShoppingCart.php';
590  $this->sc_obj = new ilPaymentShoppingCart($ilUser);
591  return true;
592  }
593 
594  private function __initPaymentObject()
595  {
596  global $ilUser;
597  $this->pobject = new ilPaymentObject($ilUser ,ilPaymentObject::_lookupPobjectId($this->ref_id));
598  return true;
599  }
600  private function __initPricesObject()
601  {
602  include_once './Services/Payment/classes/class.ilPaymentPrices.php';
603  $this->price_obj = new ilPaymentPrices($this->pobject->getPobjectId());
604  return true;
605  }
606 
607 /* depricated?!
608  * function __buildHeader()
609  {
610  $this->tpl->addBlockFile("CONTENT", "content", "tpl.payb_content.html",
611  'Services/Payment');
612  $this->tpl->setVariable("HEADER",$this->object->getTitle());
613  $this->tpl->setVariable("DESCRIPTION",$this->object->getDescription());
614  $this->tpl->parseCurrentBlock();
615  }
616 */
617 // function __buildStatusline()
618 // {
619 // $this->tpl->addBlockFile("STATUSLINE", "statusline", "tpl.statusline.html");
620 // $this->__buildLocator();
621 // }
622 
623 //
624 // function __buildStylesheet()
625 // {
626 // $this->tpl->setVariable("LOCATION_STYLESHEET",ilUtil::getStyleSheetLocation());
627 // }
628 }
629 ?>