ormdocument.class.inc.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * ormDocument
  4. * encapsulate the behavior of a binary data set that will be stored an attribute of class AttributeBlob
  5. *
  6. * @package tbd
  7. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  8. * @author Denis Flaven <denisflave@free.fr>
  9. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  10. * @link www.itop.com
  11. * @since 1.0
  12. */
  13. class ormDocument
  14. {
  15. protected $m_data;
  16. protected $m_sMimeType;
  17. protected $m_sFileName;
  18. /**
  19. * Constructor
  20. */
  21. public function __construct($data = null, $sMimeType = 'text/plain', $sFileName = '')
  22. {
  23. $this->m_data = $data;
  24. $this->m_sMimeType = $sMimeType;
  25. $this->m_sFileName = $sFileName;
  26. }
  27. public function __toString()
  28. {
  29. return MyHelpers::beautifulstr($this->m_data, 100, true);
  30. }
  31. public function IsEmpty()
  32. {
  33. return ($this->m_data == null);
  34. }
  35. public function GetMimeType()
  36. {
  37. return $this->m_sMimeType;
  38. }
  39. public function GetMainMimeType()
  40. {
  41. $iSeparatorPos = strpos($this->m_sMimeType, '/');
  42. if ($iSeparatorPos > 0)
  43. {
  44. return substr($this->m_sMimeType, 0, $iSeparatorPos);
  45. }
  46. return $this->m_sMimeType;
  47. }
  48. public function GetData()
  49. {
  50. return $this->m_data;
  51. }
  52. public function GetFileName()
  53. {
  54. return $this->m_sFileName;
  55. }
  56. public function GetAsHTML()
  57. {
  58. $sResult = '';
  59. if ($this->IsEmpty())
  60. {
  61. // If the filename is not empty, display it, this is used
  62. // by the creation wizard while the file has not yet been uploaded
  63. $sResult = $this->GetFileName();
  64. }
  65. else
  66. {
  67. $data = $this->GetData();
  68. $sResult = $this->GetFileName().' [ '.$this->GetMimeType().', size: '.strlen($data).' byte(s) ]';
  69. }
  70. return $sResult;
  71. }
  72. /**
  73. * Returns an HTML fragment that will display the document *inline* (if possible)
  74. * @return string
  75. */
  76. public function GetDisplayInline($sClass, $Id, $sAttCode)
  77. {
  78. switch ($this->GetMainMimeType())
  79. {
  80. case 'text':
  81. case 'html':
  82. $data = $this->GetData();
  83. switch($this->GetMimeType())
  84. {
  85. case 'text/html':
  86. case 'text/xml':
  87. return "<iframe src=\"../pages/ajax.render.php?operation=display_document&class=$sClass&id=$Id&field=$sAttCode\" width=\"100%\" height=\"400\">Loading...</iframe>\n";
  88. default:
  89. return "<pre>".htmlentities(MyHelpers::beautifulstr($data, 1000, true))."</pre>\n";
  90. }
  91. break; // Not really needed, but...
  92. case 'application':
  93. switch($this->GetMimeType())
  94. {
  95. case 'application/pdf':
  96. return "<iframe src=\"../pages/ajax.render.php?operation=display_document&class=$sClass&id=$Id&field=$sAttCode\" width=\"100%\" height=\"400\">Loading...</iframe>\n";
  97. }
  98. break;
  99. case 'image':
  100. return "<img src=\"../pages/ajax.render.php?operation=display_document&class=$sClass&id=$Id&field=$sAttCode\" />\n";
  101. }
  102. }
  103. /**
  104. * Returns an hyperlink to display the document *inline*
  105. * @return string
  106. */
  107. public function GetDisplayLink($sClass, $Id, $sAttCode)
  108. {
  109. return "<a href=\"../pages/ajax.render.php?operation=display_document&class=$sClass&id=$Id&field=$sAttCode\" target=\"_blank\" >".$this->GetFileName()."</a>\n";
  110. }
  111. /**
  112. * Returns an hyperlink to download the document (content-disposition: attachment)
  113. * @return string
  114. */
  115. public function GetDownloadLink($sClass, $Id, $sAttCode)
  116. {
  117. return "<a href=\"../pages/ajax.render.php?operation=download_document&class=$sClass&id=$Id&field=$sAttCode\">".$this->GetFileName()."</a>\n";
  118. }
  119. }
  120. ?>