View difference between Paste ID: TG62vypy and
SHOW:
|
|
- or go back to the newest paste.
1 | - | |
1 | + | diff --git a/uploadify/code/MultipleFileUploadField.php b/uploadify/code/MultipleFileUploadField.php |
2 | index a225715..4d8c191 100644 | |
3 | --- a/uploadify/code/MultipleFileUploadField.php | |
4 | +++ b/uploadify/code/MultipleFileUploadField.php | |
5 | @@ -21,7 +21,8 @@ class MultipleFileUploadField extends UploadifyField | |
6 | * @var array Overrides the "multi" setting | |
7 | */ | |
8 | static $defaults = array ( | |
9 | - 'multi' => true | |
10 | + 'multi' => true, | |
11 | + 'deleteEnabled' => false | |
12 | ); | |
13 | ||
14 | ||
15 | @@ -65,7 +66,7 @@ class MultipleFileUploadField extends UploadifyField | |
16 | public function importlist(SS_HTTPRequest $request) { | |
17 | if($id = $request->requestVar('FolderID')) { | |
18 | if(is_numeric($id)) { | |
19 | - $files = DataObject::get("File", "ParentID = $id AND ClassName != 'Folder'"); | |
20 | + $files = DataObject::get("File", "\"ParentID\" = $id AND \"ClassName\" != 'Folder'"); | |
21 | if($files && $this->form) { | |
22 | if($record = $this->form->getRecord()) { | |
23 | if($relation_name = $this->getForeignRelationName($record)) { | |
24 | @@ -115,20 +116,17 @@ class MultipleFileUploadField extends UploadifyField | |
25 | * @return null | |
26 | */ | |
27 | public function removefile() { | |
28 | - if($form = $this->form) { | |
29 | - if($rec = $form->getRecord()) { | |
30 | - if($key = $this->getForeignRelationName($rec)) { | |
31 | - if(isset($_REQUEST['FileID'])) { | |
32 | - if($file_class = $rec->has_many($this->name)) { | |
33 | - if($file = DataObject::get_by_id($file_class, (int) $_REQUEST['FileID'])) { | |
34 | - $file->{$key} = 0; | |
35 | - $file->write(); | |
36 | - return; | |
37 | - } | |
38 | - } | |
39 | - } | |
40 | - } | |
41 | - } | |
42 | + if((isset($_REQUEST['FileID'])) | |
43 | + && ($form = $this->form) | |
44 | + && ($rec = $form->getRecord()) | |
45 | + && ($key = $this->getForeignRelationName($rec)) | |
46 | + && ($file_class = $this->getFileClass($rec)) | |
47 | + && ($file = DataObject::get_by_id($file_class, (int) $_REQUEST['FileID']))) { | |
48 | + | |
49 | + $currentComponentSet = $rec->{$this->name}(); | |
50 | + $currentComponentSet->remove($file); | |
51 | + $currentComponentSet->write(); | |
52 | + return; | |
53 | } | |
54 | } | |
55 | ||
56 | @@ -160,7 +158,7 @@ class MultipleFileUploadField extends UploadifyField | |
57 | if(is_array($val)) { | |
58 | $list = implode(',', $val); | |
59 | $class = $this->baseFileClass; | |
60 | - if($files = DataObject::get($class, "`{$class}`.ID IN (".Convert::raw2sql($list).")")) { | |
61 | + if($files = DataObject::get($class, "\"{$class}\".\"ID\" IN (".Convert::raw2sql($list).")")) { | |
62 | $ret = new DataObjectSet(); | |
63 | foreach($files as $file) { | |
64 | if(is_subclass_of($file->ClassName, "Image") || $file->ClassName == "Image") { | |
65 | @@ -204,39 +202,56 @@ class MultipleFileUploadField extends UploadifyField | |
66 | if(!$record->isInDB()) { | |
67 | $record->write(); | |
68 | } | |
69 | - if(!$file_class = $record->has_many($this->name)) { | |
70 | + if(!$file_class = $this->getFileClass($record)) { | |
71 | return false; | |
72 | } | |
73 | if(isset($_REQUEST[$this->name]) && is_array($_REQUEST[$this->name])) { | |
74 | if($relation_name = $this->getForeignRelationName($record)) { | |
75 | // Null out all the existing relations and reset. | |
76 | - if($current = $record->{$this->name}()) { | |
77 | - foreach($current as $rec) { | |
78 | - $rec->$relation_name = 0; | |
79 | - $rec->write(); | |
80 | - } | |
81 | - } | |
82 | + $currentComponentSet = $record->{$this->name}(); | |
83 | + $currentComponentSet->removeAll(); | |
84 | // Assign all the new relations (may have already existed) | |
85 | foreach($_REQUEST[$this->name] as $id) { | |
86 | if($file = DataObject::get_by_id($this->baseFileClass, $id)) { | |
87 | $new = ($file_class != $this->baseFileClass) ? $file->newClassInstance($file_class) : $file; | |
88 | - $new->$relation_name = $record->ID; | |
89 | $new->write(); | |
90 | + $currentComponentSet->add($new); | |
91 | } | |
92 | } | |
93 | } | |
94 | } | |
95 | } | |
96 | ||
97 | + /** | |
98 | + * Returns file class for the $record for field $this->name if has_many or many_many | |
99 | + * | |
100 | + * @param DataObject $record The record to search | |
101 | + * @return string|boolean File class or false | |
102 | + */ | |
103 | + public function getFileClass (DataObject $record) { | |
104 | + if(!$file_class = $record->has_many($this->name)) { | |
105 | + if (!$many_class = $record->many_many($this->name)) { | |
106 | + return false; | |
107 | + } | |
108 | + // set child class. | |
109 | + $file_class = $many_class[1]; | |
110 | + } | |
111 | + return $file_class; | |
112 | + } | |
113 | ||
114 | /** | |
115 | - * Gets the foreign key from the child File class that relates to the $has_many on the parent record | |
116 | + * Gets the foreign key from the child File class that relates to the $has_many or $many_many | |
117 | + * on the parent record | |
118 | * | |
119 | * @param DataObject $record The record to search | |
120 | * @return string | |
121 | */ | |
122 | public function getForeignRelationName(DataObject $record) { | |
123 | - if($file_class = $record->has_many($this->name)) { | |
124 | + | |
125 | + if ($many_info = $record->many_many($this->name)) { | |
126 | + // return parent field | |
127 | + return $many_info[2]; | |
128 | + } elseif ($file_class = $record->has_many($this->name)) { | |
129 | $class = $record->class; | |
130 | $relation_name = false; | |
131 | while($class != "DataObject") { | |
132 | @@ -246,7 +261,7 @@ class MultipleFileUploadField extends UploadifyField | |
133 | $class = get_parent_class($class); | |
134 | } | |
135 | if(!$relation_name) { | |
136 | - user_error("Could not find has_one relation ship on $file_class", E_USER_ERROR); | |
137 | + user_error("Could not find has_one or belongs many_many relation ship on $file_class", E_USER_ERROR); | |
138 | } | |
139 | ||
140 | return $relation_name .= "ID"; | |
141 | diff --git a/uploadify/code/UploadifyField.php b/uploadify/code/UploadifyField.php | |
142 | index c03152b..4687a92 100644 | |
143 | --- a/uploadify/code/UploadifyField.php | |
144 | +++ b/uploadify/code/UploadifyField.php | |
145 | @@ -350,7 +350,7 @@ abstract class UploadifyField extends FormField | |
146 | public function importlist(SS_HTTPRequest $request) { | |
147 | if($id = $request->requestVar('FolderID')) { | |
148 | if(is_numeric($id)) { | |
149 | - $files = DataObject::get("File", "ParentID = $id AND ClassName != 'Folder'"); | |
150 | + $files = DataObject::get("File", "\"ParentID\" = $id AND \"ClassName\" != 'Folder'"); | |
151 | return $this->customise(array( | |
152 | 'Files' => $files | |
153 | ))->renderWith('ImportList'); | |
154 | @@ -484,6 +484,14 @@ abstract class UploadifyField extends FormField | |
155 | return $this->getSetting('multi'); | |
156 | } | |
157 | ||
158 | + /** | |
159 | + * A quick template accessor to determine if delete link is enabled | |
160 | + * | |
161 | + * @return boolean | |
162 | + */ | |
163 | + public function DeleteEnabled() { | |
164 | + return $this->getSetting('deleteEnabled'); | |
165 | + } | |
166 | ||
167 | /** | |
168 | * A template accessor to determine if we're in the backend. | |
169 | diff --git a/uploadify/templates/Includes/AttachedFiles.ss b/uploadify/templates/Includes/AttachedFiles.ss | |
170 | index eedcd94..4f560de 100644 | |
171 | --- a/uploadify/templates/Includes/AttachedFiles.ss | |
172 | +++ b/uploadify/templates/Includes/AttachedFiles.ss | |
173 | @@ -9,7 +9,7 @@ | |
174 | <div class="delete"> | |
175 | <% if Top.Backend %> | |
176 | <a class="remove" title="<% _t('Uploadify.REMOVE','Remove') %>" rel="$ID" title="<% _t('Uploadify.REMOVEANDDELETE','Remove') %>" href="<% control Top %>$Link(removefile)<% end_control %>"><img src="uploadify/images/remove.png" height="16" width="16" alt="<% _t('Uploadify.REMOVEANDDELETE','Remove') %>" /></a> | |
177 | - <a class="delete" title="<% _t('Uploadify.REMOVEANDDELETE','Remove and delete') %>" rel="$ID" title="<% _t('Uploadify.REMOVEANDDELETE','Remove and delete') %>" href="<% control Top %>$Link(deletefile)<% end_control %>"><img src="uploadify/images/delete.png" height="16" width="16" alt="<% _t('Uploadify.REMOVEANDDELETE','Remove and delete') %>" /></a> | |
178 | + <% if Top.DeleteEnabled %> <a class="delete" title="<% _t('Uploadify.REMOVEANDDELETE','Remove and delete') %>" rel="$ID" title="<% _t('Uploadify.REMOVEANDDELETE','Remove and delete') %>" href="<% control Top %>$Link(deletefile)<% end_control %>"><img src="uploadify/images/delete.png" height="16" width="16" alt="<% _t('Uploadify.REMOVEANDDELETE','Remove and delete') %>" /></a><% end_if %> | |
179 | <% else %> | |
180 | <a class="delete" title="<% _t('Uploadify.REMOVE','Remove') %>" rel="$ID" title="<% _t('Uploadify.REMOVEANDDELETE','Remove') %>" href="<% control Top %>$Link(removefile)<% end_control %>"><img src="uploadify/images/delete.png" height="16" width="16" alt="<% _t('Uploadify.REMOVEANDDELETE','Remove') %>" /></a> | |
181 | <% end_if %> |