/dataobject_manager/code/HasManyFileDataObjectManager.php

https://github.com/ordinarywebguy/silverstripe_workshop · PHP · 188 lines · 178 code · 6 blank · 4 comment · 2 complexity · c0625fbb3cbe6ad0f3b8fb93e92f8b7f MD5 · raw file

  1. <?php
  2. class HasManyFileDataObjectManager extends FileDataObjectManager
  3. {
  4. public $joinField;
  5. public $addTitle;
  6. public $RelationType = "HasMany";
  7. protected $htmlListEndName = 'CheckedList';
  8. protected $htmlListField = 'selected';
  9. public $template = 'RelationFileDataObjectManager';
  10. public $itemClass = 'HasManyFileDataObjectManager_Item';
  11. protected $relationAutoSetting = false;
  12. protected $markingPermission;
  13. /**
  14. * Most of the code below was copied from HasManyComplexTableField.
  15. * Painful, but necessary, until PHP supports multiple inheritance.
  16. */
  17. function __construct($controller, $name, $sourceClass, $fileFieldName = null, $fieldList = null, $detailFormFields = null, $sourceFilter = "", $sourceSort = "", $sourceJoin = "")
  18. {
  19. parent::__construct($controller, $name, $sourceClass, $fileFieldName, $fieldList, $detailFormFields, $sourceFilter, $sourceSort, $sourceJoin);
  20. $this->Markable = true;
  21. if($controllerClass = $this->controllerClass()) {
  22. $this->joinField = $this->getParentIdName($controllerClass, $this->sourceClass);
  23. } else {
  24. user_error("Can't figure out the data class of $controller", E_USER_WARNING);
  25. }
  26. }
  27. /**
  28. * Try to determine the DataObject that this field is built on top of
  29. */
  30. function controllerClass() {
  31. if($this->controller instanceof DataObject) return $this->controller->class;
  32. elseif($this->controller instanceof ContentController) return $this->controller->data()->class;
  33. }
  34. public function setMarkingPermission($perm)
  35. {
  36. $this->markingPermission = $perm;
  37. }
  38. public function hasMarkingPermission()
  39. {
  40. if(is_bool($this->markingPermission))
  41. return $this->markingPermission;
  42. elseif($this->markingPermission)
  43. return Permission::check($this->markingPermission);
  44. return true;
  45. }
  46. function getQuery($limitClause = null) {
  47. if($this->customQuery) {
  48. $query = $this->customQuery;
  49. $query->select[] = "\"{$this->sourceClass}\".\"ID\" AS \"ID\"";
  50. $query->select[] = "\"{$this->sourceClass}\".\"ClassName\" AS \"ClassName\"";
  51. $query->select[] = "\"{$this->sourceClass}\".\"ClassName\" AS \"RecordClassName\"";
  52. }
  53. else {
  54. $query = singleton($this->sourceClass)->extendedSQL($this->sourceFilter, $this->sourceSort, $limitClause, $this->sourceJoin);
  55. // Add more selected fields if they are from joined table.
  56. $SNG = singleton($this->sourceClass);
  57. foreach($this->FieldList() as $k => $title) {
  58. if(! $SNG->hasField($k) && ! $SNG->hasMethod('get' . $k))
  59. $query->select[] = $k;
  60. }
  61. }
  62. return clone $query;
  63. }
  64. public function setParentClass($class)
  65. {
  66. parent::setParentClass($class);
  67. $this->joinField = $this->getParentIdName($class, $this->sourceClass);
  68. }
  69. function sourceItems() {
  70. if($this->sourceItems)
  71. return $this->sourceItems;
  72. $limitClause = '';
  73. if(isset($_REQUEST[ 'ctf' ][ $this->Name() ][ 'start' ]) && is_numeric($_REQUEST[ 'ctf' ][ $this->Name() ][ 'start' ]))
  74. $limitClause = $_REQUEST[ 'ctf' ][ $this->Name() ][ 'start' ] . ", $this->pageSize";
  75. else
  76. $limitClause = "0, $this->pageSize";
  77. $dataQuery = $this->getQuery($limitClause);
  78. $records = $dataQuery->execute();
  79. $items = new DataObjectSet();
  80. foreach($records as $record) {
  81. if(! is_object($record)) {
  82. $class = $this->sourceClass;
  83. $record = new $class($record);
  84. }
  85. $items->push($record);
  86. }
  87. $dataQuery = $this->getQuery();
  88. $records = $dataQuery->execute();
  89. $unpagedItems = new DataObjectSet();
  90. foreach($records as $record) {
  91. if(! is_object($record))
  92. $record = new DataObject($record);
  93. $unpagedItems->push($record);
  94. }
  95. $this->unpagedSourceItems = $unpagedItems;
  96. $this->totalCount = ($this->unpagedSourceItems) ? $this->unpagedSourceItems->TotalItems() : null;
  97. return $items;
  98. }
  99. function getControllerID() {
  100. return $this->controller->ID;
  101. }
  102. public function SortableClass()
  103. {
  104. return $this->sourceClass();
  105. }
  106. function saveInto(DataObject $record) {
  107. $fieldName = $this->name;
  108. $saveDest = $record->$fieldName();
  109. if(! $saveDest)
  110. user_error("HasManyDataObjectManager::saveInto() Field '$fieldName' not found on $record->class.$record->ID", E_USER_ERROR);
  111. $items = array();
  112. if($list = $this->value[ $this->htmlListField ]) {
  113. if($list != 'undefined')
  114. $items = explode(',', trim($list,","));
  115. }
  116. $saveDest->setByIDList($items);
  117. }
  118. function ExtraData() {
  119. $items = array();
  120. foreach($this->unpagedSourceItems as $item) {
  121. if($item->{$this->joinField} == $this->controller->ID)
  122. $items[] = $item->ID;
  123. }
  124. $list = implode(',', $items);
  125. $value = ",";
  126. $value .= !empty($list) ? $list."," : "";
  127. $inputId = $this->id() . '_' . $this->htmlListEndName;
  128. return <<<HTML
  129. <input id="$inputId" name="{$this->name}[{$this->htmlListField}]" type="hidden" value="{$value}"/>
  130. HTML;
  131. }
  132. }
  133. class HasManyFileDataObjectManager_Item extends FileDataObjectManager_Item {
  134. function MarkingCheckbox() {
  135. $name = $this->parent->Name() . '[]';
  136. $joinVal = $this->item->{$this->parent->joinField};
  137. $parentID = $this->parent->getControllerID();
  138. $disabled = $this->parent->hasMarkingPermission() ? "" : "disabled='disabled'";
  139. if($this->parent->IsReadOnly || ($joinVal > 0 && $joinVal != $parentID))
  140. return "<input class=\"checkbox\" type=\"checkbox\" name=\"$name\" value=\"{$this->item->ID}\" disabled=\"disabled\"/>";
  141. else if($joinVal == $parentID)
  142. return "<input class=\"checkbox\" type=\"checkbox\" name=\"$name\" value=\"{$this->item->ID}\" checked=\"checked\" $disabled />";
  143. else
  144. return "<input class=\"checkbox\" type=\"checkbox\" name=\"$name\" value=\"{$this->item->ID}\" $disabled />";
  145. }
  146. }
  147. ?>