File Upload in repeat group

nine007

Member
Good Day

I have a group with a few elements and a file upload element set to repeat.

The file upload element does not want to open the dialog to add files when I press the add button, it only does so on the first, but not when I want to add more data.

Any help

thank you
 
Good Day

The file upload with ajax is still not working in repeat group, any idea when it will be fix, really need it to work.

thank you
 
Dears, bumping this one please, is it possibly too difficult? If so, please let us know. Thanks.
 
I am too invested in fabrik and in a current project in which I'd worked over 2 months having this working was essential. So I hacked the following together which seems to be working quite decently. Please share if you find any issues.

Make sure to add the following code somewhere on client side, right before form is submitted:

Code:
normalizeAttachmentsWithinRepeatableGroups:function() {
                var containers = document.getElements('.fabrikSubGroup .plupload_container'), toNormalize = {}, key, keys = [];
              
                for (var i = 0, n = containers.length; i < n; i++) {
                        key = containers[i].getParent('.fabrikSubGroupElements').getElement('.plg-internalid input').get('name');
                        key = key.split('[')[0];
                        if (typeof toNormalize[key] == 'undefined') {
                                toNormalize[key] = [];
                                keys.push(key);
                        }
                        toNormalize[key].push(containers[i]);
                }
              
                var els, name, j, k, ji, ki;
              
                for (var i = 0, n = keys.length; i < n; i++) {
                        for (j = 0, k = toNormalize[keys[i]].length; j < k; j++) {
                                els = toNormalize[keys[i]][j].getParent('.fabrikSubElementContainer').getElements('input[type=hidden]');
                                for (ji = 0, ki = els.length; ji < ki; ji++) {
                                        name = els[ji].get('name').replace('[]', '['+j+']');
                                        els[ji].set('name', name);
                                }
                        }
                }
        }

In your form plugin onAfterProcess event or in a PHP plugin onAfterProcess execute the following PHP code (naturally there're some assumptions below about what your attachment element is called, in this case its simply attachment):

Code:
        public function onAfterProcess() {
                $model = $this->getModel();
                $els = array();
                $tbls = array();
                $parents = array();
              
                foreach ($model->groups as $group) {
                        if ($group->canRepeat()) {
                                foreach ($group->elements as $element) {
                                        if ($element instanceof PlgFabrik_ElementFileupload) {
                                                $join = $group->getJoinModel()->getJoin()->table_join;
                                                $name = $join.'___'.$element->getElement()->name;
                                                $els[] = $name;
                                                $name = $join.'_repeat_'.$element->getElement()->name;
                                                $tbls[] = $name;
                                                $name = $join.'___id';
                                                $parents[] = $name;
                                        }
                                }
                        }
                }
              
                $db = JFactory::getDbo();
                $query = $db->getQuery(true);

                foreach ($parents as $i => $parent) {
                        if (!isset($_POST[$parent]) || !is_array($_POST[$parent])) {
                                continue;
                        }
                      
                        foreach ($_POST[$parent] as $repeatCounter => $parentId) {
                                $tbl = $tbls[$i];
                                $query->clear()->delete($tbl)->where('parent_id = '.$parentId);
                                $db->setQuery($query)->execute();
                              
                                if (!isset($_POST[$els[$i]]) || !isset($_POST[$els[$i]][$repeatCounter]) || !is_array($_POST[$els[$i]][$repeatCounter])) continue;
                              
                                $repeatInstance = $_POST[$els[$i]][$repeatCounter];
                              
                                if (empty($repeatInstance) || !is_array($repeatInstance)) continue;
                              
                                $repeatInstance = $repeatInstance['id'];
                              
                                if (empty($repeatInstance) || !is_array($repeatInstance)) continue;
                              
                                $files = array();
                              
                                foreach ($repeatInstance as $file => $val) {
                                        $files[] = $parentId.','.$db->quote($file);
                                }
                              
                                if (empty($files)) continue;
                              
                                $query->clear();
                              
                                $query->
                                        insert($tbl)->
                                        columns(array('parent_id', 'attachment'))->
                                        values($files)
                                        ;
                              
                                $db->setQuery($query)->execute();
                        }
                }
        }
 
Last edited:
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top