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

Trying to create an admin page, having trouble

Matthew Hawley

Well-known member
#1
I am trying to create an admin page like @Jeremy has in his Help Manager addon. I have been using his addon as a guide and i can't get the admin page to show.

Here is the code for /ControllerAdmin/Directory.php
Code:
<?php

class CF_Directory_ControllerAdmin_Directory extends XenForo_ControllerAdmin_Abstract

{

    protected function _preDispatch($action)

    {

        $this->assertAdminPermission('cfDirectory');

    }



    /**

     *

     * @return    XenForo_ControllerResponse_View

     */

    public function actionIndex()

    {

        $pageModel = $this->_getPageModel();

      

        $viewParams = array(

            'pages'    => $pageModel->getAllPages()

        );



        return $this->responseView('CF_Directory_ViewAdmin_Page_List', 'cf_directory_pages_list', $viewParams);

    }



    /**

     * Presents form for editing a page.

     *

     * @return    XenForo_ControllerResponse_Error|XenForo_ControllerResponse_View

     */

    public function actionEdit()

    {

        $pageId = $this->_input->filterSingle('page_id', XenForo_Input::UINT);



        $viewParams = array(

                'page' => XenForo_Model::create('CF_Directory_Model_Page')->getPageById($pageId)

            );



        return $this->responseView('CF_Directory_ViewAdmin_Page_Add', 'cf_directory_page_add', $viewParams);

    }



    /**

     * Presents form for adding a new page.

     *

     * @return    XenForo_ControllerResponse_View

     */

    public function actionAdd()

    {



        $viewParams = array(

                'page' => array(

                    'title'                 => '',

                    'description'             => '',

                    'category_id'             => 0,

                    'display_order' => 1,

                    'route'                 => ''

                )

            );

        return $this->responseView('CF_Directory_ViewAdmin_Page_Add', 'cf_directory_page_add', $viewParams);

    }

  

    public function actionSave()

    {

        $this->_assertPostOnly();

      

        $input = $this->_input->filter(array(

            'route'                    => XenForo_Input::STRING,

            'display_order' => XenForo_Input::UINT

        ));

      

        $pageId = $this->_input->filterSingle('page_id', XenForo_Input::UINT);

        $pageWriter = XenForo_DataWriter::create('CF_Directory_DataWriter_Page');



        if ($pageId) {

            $pageWriter->setExistingData($pageId);

        }

      

        $pageWriter->bulkSet($input);

        $pageWriter->save();

      

        $lastPage = $pageWriter->getMergedData();

      

        $data = array(

            'title'              => 'cf_directory_page_' . $lastPage['page_id'],

            'language_id'  => 0,

            'global_cache' => 0,

            'addon_id'          => 'CFDirectory'

        );

      

        $writer = XenForo_DataWriter::create('XenForo_DataWriter_Phrase');



        if ($pageId) {

            $phrases = XenForo_Model::create('XenForo_Model_Phrase')->getPhraseIdInLanguagesByTitle($data['title']);

            $writer->setExistingData($phrases[0], true);

        }



        $writer->set('phrase_text', $this->_input->filterSingle('title', XenForo_Input::STRING));



        if (!$pageId) {

            $writer->bulkSet($data);

        }



        $writer->save();

      

        $writer = XenForo_DataWriter::create('XenForo_DataWriter_Template');



        if ($pageId) {

            $template_id = XenForo_Model::create('XenForo_Model_Template')->getTemplateInStyleByTitle('cf_directory_page.' . $lastPage['page_id']);

            $writer->setExistingData($template_id);

        } else {

            $writer->set('title', 'cf_directory_page.' . $lastPage['page_id']);

            $writer->set('style_id', 0);

            $writer->set('addon_id', 'CFDirectory');

        }



        $writer->set('template', $this->_input->filterSingle('template', XenForo_Input::STRING));

        $writer->save();



        return $this->responseRedirect(

            XenForo_ControllerResponse_Redirect::SUCCESS,

            XenForo_Link::buildAdminLink('directory')

        );

    }



    /**

     * Deletes a page.

     *

     * @return    XenForo_ControllerResponse_Redirect|XenForo_ControllerResponse_View

     */

    public function actionDelete()

    {

        $pageId = $this->_input->filterSingle('page_id', XenForo_Input::INT);



        if ($this->isConfirmedPost()) {

            $pageWriter = XenForo_DataWriter::create('CF_Directory_DataWriter_Page');

            $pageWriter->setExistingData($pageId);

            $pageWriter->delete();



            return $this->responseRedirect(

                XenForo_ControllerResponse_Redirect::SUCCESS,

                XenForo_Link::buildAdminLink('directory')

            );

        } else {

            return $this->responseReroute('CF_Directory_ControllerAdmin_Directory', 'deleteConfirm');

        }

    }



    public function actionDeleteConfirm() {

        $pageId = $this->_input->filterSingle('page_id', XenForo_Input::INT);



        $viewParams = array(

                'page' => $this->_getPageModel()->getPageById($pageId)

            );



        return $this->responseView('XenForo_ViewAdmin_Page_Delete', 'cf_directory_page_delete', $viewParams);

    }



    /**

     * @return    CF_Directory_Model_Page

     */

    protected function _getPageModel()

    {

        return $this->getModelFromCache('CF_Directory_Model_Page');

    }

}
Here is the code for /Route/PrefixAdmin/Directory.php
Code:
<?php



class CF_Directory_Route_PrefixAdmin_Directory implements XenForo_Route_Interface

{

    public function match($routePath, Zend_Controller_Request_Http $request, XenForo_Router $router)

    {

    $action = $router->resolveActionWithIntegerParam($routePath, $request, 'page_id');

    return $router->getRouteMatch('XI_HelpManager_ControllerAdmin_Help', $action, 'cfdirectoryPages');

    }



  public function buildLink($originalPrefix, $outputPrefix, $action, $extension, $data, array &$extraParams)

  {

    return XenForo_Link::buildBasicLinkWithIntegerParam($outputPrefix, $action, $extension, $data, 'page_id', 'title');

  }

}
I added the admin navigation and route prefixes and made the admin templates, but I get this when I try to view my admin page.

 

Matthew Hawley

Well-known member
#9
Your error shouldn't exist if you have done that.
Do you think its something wrong with the code for the route?

Code:
<?php

class CF_Directory_Route_PrefixAdmin_Directory implements XenForo_Route_Interface

{

    public function match($routePath, Zend_Controller_Request_Http $request, XenForo_Router $router)

    {

    $action = $router->resolveActionWithIntegerParam($routePath, $request, 'page_id');

    return $router->getRouteMatch('CF_Directory_ControllerAdmin_Directory', $action, 'cfdirectoryPages');

    }



  public function buildLink($originalPrefix, $outputPrefix, $action, $extension, $data, array &$extraParams)

  {

    return XenForo_Link::buildBasicLinkWithIntegerParam($outputPrefix, $action, $extension, $data, 'page_id', 'title');

  }

}
Or maybe you could take a look in my admin control panel?
 
#10
Okay I solved part of the problem

I had it CF_Directory_Route_PrefixAdmin_Directory when it should have been Codeforums_CFDirectory_Route_PrefixAdmin_Directory.

Fixed those problems. But now I get this.

 
#11
Having database troubles. @Jeremy what does this mean?

Mysqli statement execute error : Field 'page_id' doesn't have a default value
  1. Zend_Db_Statement_Mysqli->_execute() in Zend/Db/Statement.php at line 317
  2. Zend_Db_Statement->execute() in Zend/Db/Adapter/Abstract.php at line 479
  3. Zend_Db_Adapter_Abstract->query() in Zend/Db/Adapter/Abstract.php at line 574
  4. Zend_Db_Adapter_Abstract->insert() in XenForo/DataWriter.php at line 1612
  5. XenForo_DataWriter->_insert() in XenForo/DataWriter.php at line 1601
  6. XenForo_DataWriter->_save() in XenForo/DataWriter.php at line 1393
  7. XenForo_DataWriter->save() in CF/Directory/ControllerAdmin/Directory.php at line 155
  8. CF_Directory_ControllerAdmin_Directory->actionSave() in XenForo/FrontController.php at line 337
  9. XenForo_FrontController->dispatch() in XenForo/FrontController.php at line 134
  10. XenForo_FrontController->run() in /home3/xxx/public_html/xxx/admin.php at line 13
@Chris Deeming
 
Last edited:
#12
Having database troubles. @Jeremy what does this mean?

Mysqli statement execute error : Field 'page_id' doesn't have a default value
  1. Zend_Db_Statement_Mysqli->_execute() in Zend/Db/Statement.php at line 317
  2. Zend_Db_Statement->execute() in Zend/Db/Adapter/Abstract.php at line 479
  3. Zend_Db_Adapter_Abstract->query() in Zend/Db/Adapter/Abstract.php at line 574
  4. Zend_Db_Adapter_Abstract->insert() in XenForo/DataWriter.php at line 1612
  5. XenForo_DataWriter->_insert() in XenForo/DataWriter.php at line 1601
  6. XenForo_DataWriter->_save() in XenForo/DataWriter.php at line 1393
  7. XenForo_DataWriter->save() in CF/Directory/ControllerAdmin/Directory.php at line 155
  8. CF_Directory_ControllerAdmin_Directory->actionSave() in XenForo/FrontController.php at line 337
  9. XenForo_FrontController->dispatch() in XenForo/FrontController.php at line 134
  10. XenForo_FrontController->run() in /home3/xxx/public_html/xxx/admin.php at line 13
@Chris Deeming
That means that the field page_id has no default value set when that column was created. so when you try to insert a new record to the table and do not specify a value for page_id (any other than null) it throws that error. You'd probably need to recreate the column and specifying NOT NULL DEFAULT '0' (or what ever you need the default to be in this case since it's an id as an integer usually 0 is the default value) you can also specify it to be null by default but in most cases it shouldn't be.