Linkage.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Main Linkage object setup.
  4. *
  5. * Sets up the main Linkage object and create editable fields in the CMS.
  6. *
  7. * @author S.Dwayne Pivac, OMI Ltd.
  8. *
  9. */
  10. class Linkage extends DataObject
  11. {
  12. private static $db = array(
  13. 'Title' => 'Text',
  14. 'URL' => 'Text',
  15. 'External' => 'Boolean',
  16. 'Sorder' => 'Varchar',
  17. );
  18. private static $summary_fields = array('HTMLDescription');
  19. static $singular_name = 'Link';
  20. static $plural_name = 'Links';
  21. public static $default_sort = 'Sorder ASC';
  22. function fieldLabels($includerelations = true)
  23. {
  24. $labels = parent::fieldLabels($includerelations);
  25. $labels['HTMLDescription'] = 'Links';
  26. return $labels;
  27. }
  28. private static $casting = array('HTMLDescription' => 'HTMLText');
  29. function HTMLDescription()
  30. {
  31. return $this->getHTMLDescription();
  32. }
  33. public function getHTMLDescription()
  34. {
  35. $ShowExt = 'self';
  36. if ($this->External) {
  37. $ShowExt = 'blank';
  38. }
  39. $data = new ArrayData(array(
  40. 'ID' => $this->ID,
  41. 'Title' => $this->Title,
  42. 'URL' => $this->URL,
  43. 'ShowExt' => $ShowExt,
  44. ));
  45. $output = HTMLText::create();
  46. $output->setValue($data->renderWith('Links'));
  47. return $output;
  48. }
  49. public function getCMSfields(){
  50. $f=FieldList::create(TabSet::create('Root'));
  51. $f->addFieldToTab('Root.Main', TextField::create('Title', 'Link/button name'));
  52. $f->addFieldToTab('Root.Main', TextField::create('URL', 'URL/Link'));
  53. $f->addFieldToTab('Root.Main', CheckboxField::create('External', 'External linkage?')->setDescription('Open in a new window?'));
  54. $f->addFieldToTab('Root.Main', TextField::create('Sorder', 'Sort Order')->setDescription('Links are sorted by this field using alphanumeric sorting. You can use numbers or letters.'));
  55. return $f;
  56. }
  57. }
  58. /**
  59. * Create Linkage menu.
  60. *
  61. * Create the Linkage link on the 'Left' hand side of the ModelAdmin.
  62. *
  63. * @author S.Dwayne Pivac, OMI Ltd.
  64. *
  65. */
  66. class LinkageAdmin extends ModelAdmin
  67. {
  68. private static $menu_title = 'Linkage';
  69. private static $url_segment = 'linkage';
  70. private static $managed_models = array('Linkage');
  71. private static $menu_icon = 'linkage/img/linkage.gif';
  72. private static $page_length = 50;
  73. private static $menu_priority = 110;#2Do: can this be configurable?
  74. private static $url_priority = 30;
  75. public function init()
  76. {
  77. parent::init();
  78. Requirements::css('linkage/css/linkage.css');
  79. }
  80. public function getEditForm($id=null, $fields=null)
  81. {
  82. $gridFieldTitle = 'Your Linkage';
  83. $listField=GridField::create(
  84. $this->sanitiseClassName($this->modelClass), $gridFieldTitle, $this->getList(),
  85. $fieldConfig=GridFieldConfig_RecordViewer::create($this->stat('page_length'))
  86. ->removeComponentsByType('GridFieldPageCount')
  87. ->removeComponentsByType('GridFieldPaginator')
  88. ->removeComponentsByType('GridFieldSortableHeader')
  89. ->removeComponentsByType('GridFieldViewButton')
  90. ->removeComponentsByType('GridFieldToolbarHeader')
  91. ->addComponent(new GridFieldEditLinkage())
  92. ->addComponent(new GridFieldAddNewButton())
  93. );
  94. return CMSForm::create(
  95. $this,
  96. 'EditForm',
  97. new FieldList($listField),
  98. new FieldList()
  99. )->setHTMLID('Form_EditForm')->addExtraClass('cms-edit-form cms-panel-padded center');
  100. }
  101. }
  102. /**
  103. * Linkage Edit Buttons.
  104. *
  105. * Custom Edit Buttons to avoid the default behaviour of linking via the GridField.
  106. *
  107. * @author S.Dwayne Pivac, OMI Ltd.
  108. *
  109. */
  110. class GridFieldEditLinkage implements GridField_ColumnProvider
  111. {
  112. public function augmentColumns($gridField, &$columns) {
  113. if(!in_array('Actions', $columns)) {
  114. $columns[] = 'Actions';
  115. }
  116. }
  117. public function getColumnAttributes($gridField, $record, $columnName) {
  118. return array('class' => 'col-buttons');
  119. }
  120. public function getColumnMetadata($gridField, $columnName) {
  121. if($columnName == 'Actions') {
  122. return array('title' => '');
  123. }
  124. }
  125. public function getColumnsHandled($gridField) {
  126. return array('Actions');
  127. }
  128. public function getColumnContent($gridField, $record, $columnName) {
  129. $data = new ArrayData(array(
  130. 'Link' => Controller::join_links($gridField->Link('item'), $record->ID, 'edit')
  131. ));
  132. return $data->renderWith('LinkageEditButton');
  133. }
  134. }
  135. #~/linkage/code/Linkage.php