自定义 Joomla 3 按钮任务未在我的控制器中执行


Custom Joomla 3 Button task not executing in my controller

我正在尝试上传一个外部的"组"列表,以添加到我的自定义Joomla 3组件中。我创建了一个CSV文件并编写了一些函数,我希望可以做到这一点。我创建了一个自定义按钮来在我的"组"视图中启动任务。

当我按下按钮时,我收到一个与函数完全无关的 SQL 错误,所以我尝试调试,当按下按钮时,它甚至在 sql 错误之前都没有进入我的控制器任务。我很困惑为什么。

这是我的代码

视图.html.php 测试视图组

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

测试控制器组

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'
            )
    );
}

我不将其用作常规函数,它只是最初上传数据的一次性功能。

我得到的SQL错误就像它试图加载组列表一样??与保存功能完全无关。

saveUpload是一个类似于初始保存函数的函数。

谢谢 :-)

****编辑*****

我刚刚通过调试完成了任务,并进入了JControllerLegacy的执行任务,并且由于任务未在任务映射中定义,因此默认显示,因此SQL错误尝试加载组时没有ID。现在是否需要在任务图中注册任务,然后才能选取任务?

我正式是个白痴!当我刚刚重新登录以查看是否有人响应时,我看到我已将该功能声明为受保护的功能!!迪尔!我只是从另一个功能复制并粘贴,忘记更改其访问权限。我还做了一些其他更改,现在它运行良好!

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
            )
    );
}