PageRenderTime 46ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/code/dataobjects/EmbeddedObject.php

https://github.com/Leapfrognz/silverstripe-linkable
PHP | 76 lines | 60 code | 9 blank | 7 comment | 8 complexity | ec22b2b7c818d31ce35f8d498faa4cea MD5 | raw file
  1. <?php
  2. /**
  3. * EmbeddedObject
  4. *
  5. * @package silverstripe-linkable
  6. * @license BSD License http://www.silverstripe.org/bsd-license
  7. * @author <marcus@silverstripe.com.au>
  8. **/
  9. class EmbeddedObject extends DataObject {
  10. private static $db = array(
  11. 'Title' => 'Varchar(255)',
  12. 'Type' => 'Varchar',
  13. 'SourceURL' => 'Varchar(255)',
  14. 'Width' => 'Varchar',
  15. 'Height' => 'Varchar',
  16. 'Description' => 'HTMLText',
  17. 'ThumbURL' => 'Varchar(255)',
  18. 'ExtraClass' => 'Varchar(64)',
  19. 'EmbedHTML' => 'Text',
  20. );
  21. public function Embed() {
  22. $options = array(
  23. 'width' => $this->Width,
  24. 'height' => $this->Height,
  25. );
  26. $result = Oembed::get_oembed_from_url($url, false, $options);
  27. return $result;
  28. }
  29. public function onBeforeWrite() {
  30. $changes = $this->getChangedFields();
  31. if (isset($changes['Width']) && $changes['Width']['before']) {
  32. $this->updateEmbedHTML();
  33. }
  34. if (isset($changes['Height']) && $changes['Height']['before']) {
  35. $this->updateEmbedHTML();
  36. }
  37. parent::onBeforeWrite();
  38. }
  39. public function updateEmbedHTML() {
  40. $options = array(
  41. 'width' => $this->Width,
  42. 'height' => $this->Height,
  43. );
  44. $info = Oembed::get_oembed_from_url($this->SourceURL, false, $options);
  45. if ($info && $info->exists()) {
  46. $this->EmbedHTML = $info->forTemplate();
  47. }
  48. }
  49. public function forTemplate() {
  50. switch($this->Type) {
  51. case 'video':
  52. case 'rich':
  53. if($this->ExtraClass) {
  54. return "<div class='$this->ExtraClass'>$this->EmbedHTML</div>";
  55. } else {
  56. return $this->EmbedHTML;
  57. }
  58. break;
  59. case 'link':
  60. return '<a class="' . $this->ExtraClass . '" href="' . $this->SourceURL . '">' . $this->Title . '</a>';
  61. break;
  62. case 'photo':
  63. return "<img src='$this->SourceURL' width='$this->Width' height='$this->Height' class='$this->ExtraClass' />";
  64. break;
  65. }
  66. }
  67. }