Bin will be removed on June 1, 2014 in favor of http://gist.github.com. Please save all your pastes before then. So long, and thanks for all the fish!×
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*******
 
- Using AD7Six generic upload behaviour
- Have a Product model (which holds information about cars)
- Product admin_add triggers admin_edit() with the same ID as per demo
 
 
Problem: Adding attachments works fine.
     However in my form I have multiple select box for HABTM relationships and
     and if I use them (select 1 or more items in any HABTM multiple select) and submit the form then
     the attachments are deleted from the attachment table.
     
     I've narrowed this down, its only HABTM data which causes attachments to be lost.
     
 
Complete code/SQL available on request if it could help *someone* solve my problem!
 
*******/
 
/********************  
*********************
    File: app/app_controller.php
*********************
**********************/
 
 
function beforeFilter() {
    $this->__initialiseSettings();
    if(!empty($this->params['admin']) || $this->here == '/admin') {
        $this->layout = 'admin';
    } else {
        Configure::write('debug', '1');
    }
 
}
function beforeRender() {
    /* Attachment Plugin code */
    if (!isset ($this->viewVars['data'])) {
        $this->set('data', $this->data);
    }
    if (!isset ($this->viewVars['modelClass'])) {
        $this->set('modelClass', $this->modelClass);
    }
    /* END Attachment Plugin code */
 
 
    //$this->set('menu', $this->Content->find('all', array('fields' => array('name', 'slug', 'lft', 'rght')), array('order' => 'lft ASC')));
    $this->set('menu', $this->Content->findAllThreaded(null, array('id', 'parent_id', 'slug', 'name', 'url'), "Content.lft ASC"));
    $this->set('sitename', 'FCS CMS ::');
}
 
 
function __initialiseSettings() {
    $settings = $this->Setting->find('all');
    foreach ($settings as $setting) {
        $this->set($setting['Setting']['key'], $setting['Setting']['value']);
    }
}
 
 
/********************  
*********************
    File: app/app_model.php
*********************
**********************/
 
<?php
class AppModel extends Model{
        var $hasMany = array(
                'Attachment' => array(
                        'foreignKey' => 'foreign_id',
                        'conditions' => array('Attachment.class' => 'To Be Filled'),
                        'dependent' => true
                )
        );
        var $hasOne = array(
                'Thumb' => array(
                        'className' => 'Attachment',
                        'foreignKey' => 'foreign_id',
                        'conditions' => array('Thumb.class' => 'To Be Filled', 'Thumb.thumb' => true),
                        'dependent' => true
                )
        );
 
        function __construct($id = false, $table = null, $ds = null) {
                parent::__construct($id, $table, $ds);
                if (isset($this->hasMany['Attachment'])) {
                        $this->hasMany['Attachment']['conditions']['Attachment.class'] = $this->name;
                }
                if (isset($this->hasOne['Thumb'])) {
                        $this->hasOne['Thumb']['conditions']['Thumb.class'] = $this->name;
                }
        }
}
?>
 
 
/********************  
*********************
    File: app/controllers/products_controller.php  
*********************
**********************/
 
function admin_add () {
    if ($this->data) {
        $this->{$this->modelClass}->save($this->data);
        $this->redirect(array('action'=>'index'));
    }
    $this->admin_edit($this->{$this->modelClass}->id);
}
 
 
function admin_edit($id) {
    if (!empty ($this->data)) {
        if ($this->{$this->modelClass}->save($this->data)) {
            $this->Session->setFlash('Car saved');
            $this->redirect(array ('action' => 'index'), null, true);
        } else {
            $this->Session->setFlash('Please correct the errors below.');
        }
    }
    if (empty($this->data)) {
        $this->data = $this->Product->read(null, $id);
    }
 
    if (isset($this->{$this->modelClass}->Thumb)) {
        $thumbs = $this->{$this->modelClass}->Thumb->findAll(array('class' => $this->modelClass, 'foreign_id' => $id));
        $this->set(compact('thumbs'));
    }
    $attributes = $this->Product->Attribute->find('list');
    $categories = $this->Product->Category->find('list');
    $contents = $this->Product->Content->find('list');
    $tags = $this->Product->Tag->find('list');
    $makes = $this->Product->Make->find('list');
    $makes = $this->Product->Make->generateTreeList(null, "{n}.Make.id", "{n}.Make.name", '--', 1);
    $types = $this->Product->Type->find('list');
    $fueltypes = $this->Product->Fueltype->find('list');
    $colours = $this->Product->Colour->find('list');
    $enginesizes = $this->Product->Enginesize->find('list');
    $transmissions = $this->Product->Transmission->find('list');
    $contacts = $this->Product->Contact->find('list');
    $statuses = $this->Product->Status->find('list');
    $this->set(compact('attributes','categories','contents','tags','makes','types','fueltypes','colours','enginesizes','transmissions','contacts','statuses'));
    $this->render('admin_edit');
}
 
/********************  
*********************
    File: app/views/products/admin_edit.ctp  
*********************
**********************/
 
<div class="products form">
<?php echo $form->create('Product');?>
 
        <fieldset>
                <legend><?php __('Car add/edit');?></legend>
 
        <fieldset>
                <legend><?php __('Pictures');?></legend>
        <?php
                echo $this->renderElement('attachments');
                //echo $form->submit('Save car details');
        ?>
        </fieldset>
 
 
        <fieldset>
                <legend><?php __('Edit Product');?></legend>
        <?php
                echo $form->input('id');
                echo $form->input('registration');
                echo $form->input('name');
                echo $form->input('make_id', array('empty' => true));
                echo $form->input('type_id', array('empty' => true));
                echo $form->input('fueltype_id', array('empty' => true));
                echo $form->input('colour_id', array('empty' => true));
                echo $form->input('enginesize_id', array('empty' => true));
                echo $form->input('transmission_id', array('empty' => true));
                echo $form->input('contact_id', array('empty' => true));
                echo $form->input('status_id', array('empty' => true));
                echo $form->input('price');
                echo $form->input('year');
                echo $form->input('miles');
                echo $form->input('owners');
                echo $form->input('description');
                echo $form->input('expires');
                echo $form->input('Attribute');
                echo $form->input('Category');
                echo $form->input('Content');
                echo $form->input('Tag');
        ?>
        </fieldset>
        </fieldset>
</div>
        <? echo $form->end('Save car details'); ?>
 
<? e($this->element('tinymce', array('preset' => 'basic'))); ?>
 
 
/********************  
*********************
    File: app/views/elements/attachments.ctp  
*********************
**********************/
 
<div id='thumbs'>
<?php
if (isset($thumbs) && is_array($thumbs)) {
        foreach ($thumbs as $thumb) {
                //extract($thumb['Attachment']);
                extract($thumb['Thumb']);
                echo '<div class=\'thumb\'>';
                echo $html->image('50x50/' . $dir . '/' . $filename, array('id' => 'thumb-' . $id, 'alt' =>  $description, 'title' =>  $description));
                echo '<ul class=\'menu\'>';
                echo '<li>' . $html->link('Delete', array('controller' => 'attachments', 'action' => 'delete', $id), null,
                                                        'Are you sure you want to delete '. $filename . '?') . '</li>';
                echo "<li>". $filename ."</li>";
                echo "<li>". $description ."</li>";
                echo "<li>". $number->toReadableSize($filesize) ."</li>";
                echo "<li>Created ". $time->timeAgoInWords($created) ."</li>";
 
                if ($thumb) {
                        echo '<li>' . $html->link('quit thumb', array('controller' => 'attachments', 'action' => 'set_thumb',  $id, 0)) . '</li>';
                } else {
                        echo '<li>' . $html->link('make thumb', array('controller' => 'attachments', 'action' => 'set_thumb',  $id, 1)) . '</li>';
                }
 
                echo '</ul>';
                echo '</div>';
        }
}
?>
</div>
<?php
if ($data[$modelClass]['id']) {
        echo $html->link('Add new attachment',
                        array('controller' => 'attachments', 'action' => 'add', $modelClass, $data[$modelClass]['id'], 'admin' => true));
} else {
        echo '<p>First you must save ' . $modelClass . ' and then edit to upload files/images.</p>';
}
?>