Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
3 | class ImageGalleryPage extends Page |
||
|
|||
4 | { |
||
5 | private static $icon = "imagegallery_basic/images/treeicons/ImageGalleryPage"; |
||
6 | |||
7 | private static $allowed_children = array("ImageGalleryPage"); //can also be "none"; |
||
8 | |||
9 | private static $default_child = "ImageGalleryPage"; |
||
10 | |||
11 | private static $description = "Page used to display images in a gallery"; |
||
12 | |||
13 | private static $has_one = array( |
||
14 | "AutomaticallyIncludedFolder" => "Folder" |
||
15 | ); |
||
16 | |||
17 | private static $has_many = array( |
||
18 | "ImageGalleryEntries" => "ImageGalleryEntry" |
||
19 | ); |
||
20 | |||
21 | public function getCMSFields() |
||
22 | { |
||
23 | $fields = parent::getCMSFields(); |
||
24 | $fields->addFieldToTab("Root.Gallery", $treeDropdowField = TreeDropdownField::create($name = "AutomaticallyIncludedFolderID", $title = "Quick Add from Folder", $sourceObjectName = "Folder")); |
||
25 | $treeDropdowField->setRightTitle(' |
||
26 | Add a folder here to add a bunch of images all at once.<br /> |
||
27 | <a href="/admin/assets/show/'.$this->AutomaticallyIncludedFolderID.'">see files and images</a> section to add, remove and edit images in a folder before selecting one here. |
||
28 | <br /> |
||
29 | Once added, you can edit the images below as you see fit. |
||
30 | '); |
||
31 | $gridField = new GridField('images', 'Linked images', $this->ImageGalleryEntries(), GridFieldConfig_RelationEditor::create()); |
||
32 | $fields->addFieldToTab("Root.Gallery", $gridField); |
||
33 | if (class_exists("DataObjectSorterController")) { |
||
34 | $fields->addFieldToTab( |
||
35 | "Root.Gallery", |
||
36 | LiteralField::create( |
||
37 | "ImageGalleryEntrySorter", |
||
38 | DataObjectSorterController::popup_link( |
||
39 | "ImageGalleryEntry", |
||
40 | $filterField = "ParentID", |
||
41 | $filterValue = $this->ID, |
||
42 | $linkText = "Sort Items", |
||
43 | $titleField = "FullTitle" |
||
44 | ) |
||
45 | ) |
||
46 | ); |
||
47 | } else { |
||
48 | $fields->addFieldToTab("Root.Gallery", new NumericField($name = "Sort", "Sort index number (the lower the number, the earlier it shows up")); |
||
49 | } |
||
50 | return $fields; |
||
51 | } |
||
52 | |||
53 | public function onBeforeWrite() |
||
54 | { |
||
55 | parent::onBeforeWrite(); |
||
56 | $imageAdded = false; |
||
57 | if ($this->AutomaticallyIncludedFolderID) { |
||
58 | if ($folder = Folder::get()->byID($this->AutomaticallyIncludedFolderID)) { |
||
59 | if ($files = Image::get()->filter("ParentID", $folder->ID)) { |
||
60 | foreach ($files as $file) { |
||
61 | if (ImageGalleryEntry::get()->filter(array("ImageID" => $file->ID, "ParentID" => $this->ID))->count()) { |
||
62 | //do nothing |
||
63 | //debug::log("already exists"); |
||
64 | } else { |
||
65 | $ImageGalleryEntry = new ImageGalleryEntry(); |
||
66 | $ImageGalleryEntry->Title = $file->Title; |
||
67 | $ImageGalleryEntry->ImageID = $file->ID; |
||
68 | $ImageGalleryEntry->ParentID = $this->ID; |
||
69 | $ImageGalleryEntry->write(); |
||
70 | $imageAdded = true; |
||
71 | //debug::log("writing"); |
||
72 | } |
||
73 | } |
||
74 | } else { |
||
75 | //debug::log("D"); |
||
76 | } |
||
77 | } else { |
||
78 | //debug::log("C"); |
||
79 | } |
||
80 | } else { |
||
81 | //debug::log("B"); |
||
82 | } |
||
83 | if ($ImageGalleryEntries = ImageGalleryEntry::get()->filter(array("ParentID" => $this->ID))) { |
||
84 | foreach ($ImageGalleryEntries as $ImageGalleryEntry) { |
||
85 | $image = Image::get() |
||
86 | ->filter(array("ID" => $ImageGalleryEntry->ImageID)) |
||
87 | ->exclude(array("Title" => $ImageGalleryEntry->Title)) |
||
88 | ->First(); |
||
89 | if ($image) { |
||
90 | $image->Title = $image->Name = $ImageGalleryEntry->Title; |
||
91 | $image->write(); |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | if ($imageAdded) { |
||
96 | //LeftAndMain::force_reload(); |
||
97 | } |
||
98 | $this->AutomaticallyIncludedFolderID = 0; |
||
99 | } |
||
100 | |||
101 | View Code Duplication | public function NextGallery() |
|
114 | |||
115 | View Code Duplication | public function PreviousGallery() |
|
128 | } |
||
129 | |||
130 | class ImageGalleryPage_Controller extends Page_Controller |
||
150 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.