• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Javascript error ?

Chris D

XenForo developer
Staff member
#2
There's a template error, according to that output.

It says: Invalid argument supplied for foreach().

It's a template error so the suspect code is likely to be:

<xen:foreach loop="$simpleText" value="$text">

This would indicate that either $simpleText is not an array, or that the $simpleText parameter isn't available in the template.
 

Zephyr

Well-known member
#3
Hmm, I'm not supposed to have variable SimpleText, I rename all, is it possible that this is a caching problem that stock a misnamed variable?
 

Marcus

Well-known member
#4
Hmm, I'm not supposed to have variable SimpleText, I rename all, is it possible that this is a caching problem that stock a misnamed variable?
In your error message xenforo tries to render a simpleText variable. I'd check your templates after this variable, just search in acp>templates>search templates for "simpleText". It is possible that you renamed it in your php files, but there is a template left with this variable.
 

Zephyr

Well-known member
#5
Thanks, this is corrected by cons I still have the same error and when I submit the form, it tells me:
Security error occurred. Please press back, refresh the page, and try again.
ControlerPublic:
PHP:
 <?php
// on étends XenForo_ControllerPublic_Abstract
class XenCrea_DownloadManager_ControllerPublic_DownloadManager extends XenForo_ControllerPublic_Abstract
{
    /**
    * Ecrire dans la base de données le texte que l'utilisateur à saisi dans la zone de texte.
    */
    public function actionWrite()
    {
        // On récupère le texte que l'utilisateur a écrit dans la boite de texte
        $text = $this->_input->filterSingle('download_text', XenForo_Input::STRING);
 
        //Create a instance of our DataWriter
        $dwSimpleText = XenForo_DataWriter::create('XenCrea_DownloadManager_DataWriter_DownloadManager');
 
        // Set the field with the data we filtered
        $dwSimpleText->set('download_text', $text);
 
        // Sauvegarde dans la base de donnée
        $dwSimpleText->save();
       
        // Envois une réponse à l'utilisateur, pour qu'il sache que tout s'est bien passé avec cette action
        return $this->responseRedirect(
                    XenForo_ControllerResponse_Redirect::SUCCESS,
                    $this->getDynamicRedirect()
                );
    }
 
    /**
    * Récupère tous le texte enregistré dans la table.
    */
    public function actionRead()
    {
        // Récupère tous le texte enregistré dans la table.
        $viewParams = array('downloadmanager' => $this->_getDownloadManagerModel()->getAllDownloads());
 
        //Send a response view, using a template, to show all the data that we get it.
        return $this->responseView('XenForo_ViewPublic_Base', 'downloadmanager', $viewParams);
 
    }
 
    /**
    * Get the simple text model.
    *
    * @return XenCrea_DownloadManager_Model_DownloadManager
    */
    protected function _getDownloadManagerModel()
    {
        return $this->getModelFromCache ( 'XenCrea_DownloadManager_Model_DownloadManager' );
    }
}
?>
DataWriter:
PHP:
 <?php
// On étends la class XenForo_DataWriter
class XenCrea_DownloadManager_DataWriter_DownloadManager extends XenForo_DataWriter
{
    /**
    * Obtient les champs qui sont définis pour la table.
    *
    * @return array
    */
    protected function _getFields() {
        // Les champs présents et leur type devra être mis à jour en cas j'ajout dans de champ à la table dans le Installer.php
        return array(
            'xf_dm_download' => array(
                'download_id'    => array(
                    'type' => self::TYPE_UINT,
                    'autoIncrement' => true
                ),
                'download_text'    => array(
                    'type' => self::TYPE_STRING, 'required' => true
                ),
                'download_date'    => array(
                    'type'            => self::TYPE_UINT,
                    'required'        => false,
                    'default'        => XenForo_Application::$time
                )
            )
        );
    }
 
    /**
    * Obtient les données réelles existantes déjà transmise afin de les mettres à jour.
    *
    * @param mixed
    *
    * @see XenForo_DataWriter::_getExistingData()
    *
    * @return array|false
    */
    protected function _getExistingData($data)
    {
        if (!$id = $this->_getExistingPrimaryKey($data, 'download_id'))
        {
            return false;
        }
 
        return array('xf_download_text' => $this->_getDownloadTextModel()->getDownloadTextById($id));
    }
 
    /**
    * Gets SQL condition to update the existing record.
    *
    * @see XenForo_DataWriter::_getUpdateCondition()
    *
    * @return string
    */
    protected function _getUpdateCondition($tableName)
    {
        // Z Retourne du texte avec l'id
        return 'download_id = ' . $this->_db->quote($this->getExisting('download_id'));
    }
 
    /**
    * Get the simple text model.
    *
    * @return XenCrea_DownloadManager_Model_DownloadManager
    */
    protected function _getDownloadManagerModel()
    {
        return $this->getModelFromCache ( 'XenCrea_DownloadManager_Model_DownloadManager' );
    }
}
?>
Model
PHP:
 <?php
// On extend la class Xenforo_Model étant donné que l'on ets dans un modèle.
class XenCrea_DownloadManager_Model_DownloadManager extends XenForo_Model
{
 
    public function getDownloadById($downloadId)
    {
        return $this->_getDb()->fetchRow('
            SELECT * FROM xf_dm_download WHERE download_id = ?', $downloadId);
    }
 
    public function getAllDownloads()
    {
        return $this->fetchAllKeyed('SELECT * FROM xf_dm_download ORDER BY download_date DESC', 'download_Id');
    }
 
}
?>
Route
PHP:
 <?php
// implements XenForo_Route_Interface.
    /**
    * Method to build a link to the specified page/action with the provided
    * data and params.
    *
    * @see XenForo_Route_BuilderInterface
    */
class XenCrea_DownloadManager_Route_Prefix_DownloadManager implements XenForo_Route_Interface
{
    /**
    * Match a specific route for an already matched prefix.
    *
    * @see XenForo_Route_Interface::match()
    */
    public function match($routePath, Zend_Controller_Request_Http $request, XenForo_Router $router)
    {
        //Please, discover what action the user wants to call!
        $action = $router->resolveActionWithIntegerParam($routePath, $request, 'download_id');
        //Call the action in the controller SimpleText_ControllerPublic_SimpleText!
        return $router->getRouteMatch('XenCrea_DownloadManager_ControllerPublic_DownloadManager', $action, 'downloadmanager');
    }
 
    /**
    * Method to build a link to the specified page/action with the provided
    * data and params.
    *
    * @see XenForo_Route_BuilderInterface
    */
    public function buildLink($originalPrefix, $outputPrefix, $action, $extension, $data, array &$extraParams)
    {
        return XenForo_Link::buildBasicLinkWithIntegerParam($outputPrefix, $action, $extension, $data, 'download_id');
    }
}
?>
Template:
HTML:
<xen:foreach loop="$downloadManager" value="$text">
<fieldset>
    <li>
        <dl class="ctrlUnit">
            <dt><xen:datetime time="$text.download_date" /></dt>
            <dd>
                {$text.download_text}
            </dd>
        </dl>
        <dl class="ctrlUnit"></dl>
    </li>
</fieldset>
<br />
</xen:foreach>
</ul>
Html:
HTML:
<ul class="tabs Tabs" data-panes="#downloadManagerPanes > li">
    <li class="active"><a href="{$requestPaths.requestUri}#write">Write</a></li>
    <li><a href="{$requestPaths.requestUri}#read">Read</a></li>
</ul>
<ul id="downloadManagerPanes">
    <li id="write">
        <form action="{xen:link 'downloadmanager/write'}" method="post" class="xenForm">
            <dl class="ctrlUnit">
                <dt><label for="ctrl_message">{xen:phrase message}:</label></dt>
                <dd><textarea name="download_text" class="textCtrl Elastic" id="ctrl_message" rows="5"></textarea></dd>
            </dl>
            <dl class="ctrlUnit submitUnit">
                <dt></dt>
                <dd><input type="submit" value="{xen:phrase send_message}" accesskey="s" class="button primary" /></dd>
            </dl>
        </form>
    </li>
 
    <li id="read" class="profileContent" data-loadUrl="{xen:link downloadmanager/read}">
        <span class="jsOnly">{xen:phrase loading}...</span>
    </li>
</ul>
Is it normal XenForo gives no indication about the error?
 

Zephyr

Well-known member
#8
Thanks, submit is ok :)



But I have also the error from page read:



It'll take me to training, learning frameworks it is a big advantage to encode in XenForo ? :)
 

Zephyr

Well-known member
#9
It's good, I have rename

<xen:foreach loop="$downloadManager" value="$text">
in
<xen:foreach loop="$downloadmanager" value="$text">

thank you for your help.
Parcontre have tips or tricks you need to give me please? I started completely, this is my first addon that uses POO. :)