PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/code/forms/LinkField.php

https://github.com/Leapfrognz/silverstripe-linkable
PHP | 147 lines | 78 code | 31 blank | 38 comment | 7 complexity | da3c14e449b0d470b8d3d950668685ac MD5 | raw file
  1. <?php
  2. /**
  3. * LinkField
  4. *
  5. * @package silverstripe-linkable
  6. * @license BSD License http://www.silverstripe.org/bsd-license
  7. * @author <shea@silverstripe.com.au>
  8. **/
  9. class LinkField extends TextField{
  10. /**
  11. * @var Boolean
  12. **/
  13. protected $isFrontend = false;
  14. /**
  15. * @var Link
  16. **/
  17. protected $linkObject;
  18. public static $allowed_actions = array(
  19. 'LinkForm',
  20. 'LinkFormHTML',
  21. 'doSaveLink',
  22. 'doRemoveLink'
  23. );
  24. public function Field($properties = array()){
  25. Requirements::javascript(LINKABLE_PATH . '/javascript/linkfield.js');
  26. return parent::Field();
  27. }
  28. /**
  29. * The LinkForm for the dialog window
  30. *
  31. * @return Form
  32. **/
  33. public function LinkForm(){
  34. $link = $this->getLinkObject();
  35. $action = FormAction::create('doSaveLink', _t('Linkable.SAVE', 'Save'))->setUseButtonTag('true');
  36. if(!$this->isFrontend){
  37. $action->addExtraClass('ss-ui-action-constructive')->setAttribute('data-icon', 'accept');
  38. }
  39. $link = null;
  40. if($linkID = (int)$this->request->getVar('LinkID')){
  41. $link = Link::get()->byID($linkID);
  42. }
  43. $link = $link ? $link : singleton('Link');
  44. $fields = $link->getCMSFields();
  45. $title = $link ? _t('Linkable.EDITLINK', 'Edit Link') : _t('Linkable.ADDLINK', 'Add Link');
  46. $fields->insertBefore(HeaderField::create('LinkHeader', $title), _t('Linkable.TITLE', 'Title'));
  47. $actions = FieldList::create($action);
  48. $form = Form::create($this, 'LinkForm', $fields, $actions);
  49. if($link){
  50. $form->loadDataFrom($link);
  51. $fields->push(HiddenField::create('LinkID', 'LinkID', $link->ID));
  52. }
  53. $this->owner->extend('updateLinkForm', $form);
  54. return $form;
  55. }
  56. /**
  57. * Either updates the current link or creates a new one
  58. * Returns field template to update the interface
  59. * @return String
  60. **/
  61. public function doSaveLink($data, $form){
  62. $link = $this->getLinkObject() ? $this->getLinkObject() : Link::create();
  63. $form->saveInto($link);
  64. try {
  65. $link->write();
  66. } catch (ValidationException $e) {
  67. $form->sessionMessage($e->getMessage(), 'bad');
  68. return $form->forTemplate();
  69. }
  70. $this->setValue($link->ID);
  71. $this->setForm($form);
  72. return $this->FieldHolder();
  73. }
  74. /**
  75. * Delete link action - TODO
  76. *
  77. * @return String
  78. **/
  79. public function doRemoveLink(){
  80. $this->setValue('');
  81. return $this->FieldHolder();
  82. }
  83. /**
  84. * Returns the current link object
  85. *
  86. * @return Link
  87. **/
  88. public function getLinkObject(){
  89. $requestID = Controller::curr()->request->requestVar('LinkID');
  90. if($requestID == '0'){
  91. return;
  92. }
  93. if(!$this->linkObject){
  94. $id = $this->Value() ? $this->Value() : $requestID;
  95. if((int)$id){
  96. $this->linkObject = Link::get()->byID($id);
  97. }
  98. }
  99. return $this->linkObject;
  100. }
  101. /**
  102. * Returns the HTML of the LinkForm for the dialog
  103. *
  104. * @return String
  105. **/
  106. public function LinkFormHTML(){
  107. return $this->LinkForm()->forTemplate();
  108. }
  109. public function getIsFrontend(){
  110. return $this->isFrontend;
  111. }
  112. public function setIsFrontend($bool){
  113. $this->isFrontend = $bool;
  114. return $this->this;
  115. }
  116. }