0

I am trying to upload an external list of "groups" to add to my custom Joomla 3 component. I have created a CSV file and written a few functions that I hope will do it. I have created a custom button to start the task in my "groups" view.

When I push the button I get an SQL error that has absoloutle nothing to do with the functions so I have tried debugging and when the button is pressed its not even getting to my controller task before the sql error. I am so confused as to why.

This is the code I have

view.html.php TestViewGroups

JToolBarHelper::custom('group.uploadsave', '', '', 'Upload and Save', false);

TestControllerGroup

protected function uploadsave() {



    $detail_headers = array(                
            'agm_date',
            'preferred_media'
    );



    $rows = array_map('str_getcsv', file('groupdata.csv'));
    $header = array_shift($rows);

    foreach ($rows as $row) {
        $entry = array_combine($header, $row);
        foreach ($entry as $key => $value) {
            if(in_array($key, $detail_headers)){
                $details[$key]= $value;
                unset($entry[$key]);
            }
        }
        $entry['details'] = $details;

        $this->saveUploaded($entry);
    }

    // Redirect to the list screen.
    $this->setRedirect(
            JRoute::_(
                    'index.php?option=' . $this->option . '&view=' . $this->view_list
                    . $this->getRedirectToListAppend(), false
            )
    );

}

protected function saveUploaded($dataIn = array()) {

    $app   = JFactory::getApplication();
    $lang  = JFactory::getLanguage();
    $model = $this->getModel();
    $table = $model->getTable();
    $data  = $dataIn;

    $checkin = property_exists($table, 'checked_out');
    // Determine the name of the primary key for the data.
    if (empty($key))
    {
        $key = $table->getKeyName();
    }

    // To avoid data collisions the urlVar may be different from the primary key.
    if (empty($urlVar))
    {
        $urlVar = $key;
    }

    $recordId = $this->input->getInt($urlVar);

    // Populate the row id from the session.
    $data[$key] = $recordId;

    if (!$model->save($validData))
    {

        // Redirect back to the edit screen.
        $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError()));
        $this->setMessage($this->getError(), 'error');  

    }
    if ($checkin && $model->checkin($validData[$key]) === false)
    {
        // Save the data in the session.
        $app->setUserState($context . '.data', $validData);

        // Check-in failed, so go back to the record and display a notice.
        $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()));
        $this->setMessage($this->getError(), 'error');      

    }
    $this->setMessage(
            JText::_(
                    ($lang->hasKey($this->text_prefix . ($recordId == 0 && $app->isSite() ? '_SUBMIT' : '') . '_SAVE_SUCCESS')
                            ? $this->text_prefix
                            : 'JLIB_APPLICATION') . ($recordId == 0 && $app->isSite() ? '_SUBMIT' : '') . '_SAVE_SUCCESS'
            )
    );
}

I am not using this as a regular function, its just a once off to upload the data initially.

The SQL error I am getting is like it is trying to load a list of groups?? not anything to do with the save function at all.

The saveUploaded is a similar function to the initial save function.

Thanks :-)

**** Edit *****

I have just followed the task through with debug and its getting to the execute task methotd of JControllerLegacy and because the task is not defined in the task map its defaulting to display, hence the SQL error trying to load a group when it doesn't have an ID. Do I need to now register a task in the task map before it will pick it up?

Lee Wiggins
  • 351
  • 3
  • 11
  • Hi! Since some time, Joomla has its own StackExchange site: [joomla.stackexchange.com](http://joomla.stackexchange.com). I recommend you asking your future [tag:joomla]-related questions there. – miroxlav Jan 24 '15 at 14:04
  • Thanks very much! I didn't know, I will do just that. Thanks again – Lee Wiggins Jan 24 '15 at 23:23

1 Answers1

1

I am officially an idiot! When I just logged back on to see if anyone had responded I saw that I had declared the function as a protected function!! dir! I just copied and pasted from another function and forgot to change its access. I also made a few other changes and now it works quite well!

public function uploadsave() {      
    // An array of headers that will need to be entered into a seperate array to allow entry as JSON
    $detail_headers = array(                
            'agm_date',
            'preferred_media'
    );

    $app   = JFactory::getApplication();
    $lang  = JFactory::getLanguage();
    $model = $this->getModel();     

    $path = JPATH_COMPONENT . '/controllers/groupdata.csv';     

    //Load the file and pass each line into an array.
    $rows = array_map('str_getcsv', file($path));
    //Take out the first line as it is the headers.
    $header = array_shift($rows);

    //turn each of the arrays into an entry
    foreach ($rows as $row) {
        $entry = array_combine($header, $row);
        foreach ($entry as $key => $value) {
            //separate each of the entries that need to be entered into an array to be stored as JSON
            if(in_array($key, $detail_headers)){
                $details[$key]= $value;
                unset($entry[$key]);
            }
        }
        $entry['details'] = $details;

        $recordId = 'id';

        // Populate the row id from the session.
        $entry[$key] = $recordId;

        //Save each one
        if (!$model->save($entry))
        {

            // Redirect back to the edit screen.
            $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError()));
            $this->setMessage($this->getError(), 'error');

            return false;

        }           
        $this->setMessage(
                JText::_(
                        ($lang->hasKey($this->text_prefix . ($recordId == 0 && $app->isSite() ? '_SUBMIT' : '') . '_SAVE_SUCCESS')
                                ? $this->text_prefix
                                : 'JLIB_APPLICATION') . ($recordId == 0 && $app->isSite() ? '_SUBMIT' : '') . '_SAVE_SUCCESS'
                )
        );
        }

    // Redirect to the list screen.
    $this->setRedirect(
            JRoute::_(
                    'index.php?option=' . $this->option . '&view=' . $this->view_list
                    . $this->getRedirectToListAppend(), false
            )
    );

}
Lee Wiggins
  • 351
  • 3
  • 11
  • Almost six years later and somehow I made the same mistake and your answer is really helping me out. Thanks for posting! – Herman Nov 19 '20 at 08:37