JasonWang Posted September 19, 2013 Share Posted September 19, 2013 (edited) I have a problem when following Developer Guide. It was this part of the guide. 1. Add a new table to your PrestaShop data it two fields: id_test (INT 11) test (VARCHAR 32) 2. Create a blank file named Test.php in PrestaShop's /classes folder. 3. Add the following lines to that file: Test.php <?php class Test extends ObjectModel { /** @var string Name */ public $test; protected $fieldsRequired = array( 'test' ); protected $fieldsSize = array( 'test' => 64 ); protected $fieldsValidate = array( 'test' => 'isGenericName' ); protected $table = 'test'; protected $identifier = 'id_test'; public function getFields() { parent::validateFields(); $fields[ 'test' ] = pSQL( $this->test ); return $fields; } } ?> 1. Create a blank file named AdminTest.php in PrestaShop's /admin/tabs. 2. Add the following lines to that file: AdminTest.php <?php include_once( PS_ADMIN_DIR . '/../classes/AdminTab.php' ); class AdminTest extends AdminTab { public function __construct() { $this->table = 'test'; $this->className = 'Test'; $this->lang = false; $this->edit = true; $this->delete = true; $this->fieldsDisplay = array( 'id_test' => array( 'title' => $this->l( 'ID' ), 'align' => 'center', 'width' => 25 ), 'test' => array( 'title' => $this->l( 'Name' ), 'width' => 200 ) ); $this->identifier = 'id_test'; parent::__construct(); } public function displayForm() { global $currentIndex; $defaultLanguage = intval( Configuration::get( 'PS_LANG_DEFAULT' ) ); $languages = Language::getLanguages(); $obj = $this->loadObject( true ); echo ' <script type="text/javascript"> id_language = Number('.$defaultLanguage.'); </script>'; echo ' <form action="' . $currentIndex . '&submitAdd' . $this->table . '=1&token=' . $this->token . '" method="post" class="width3"> ' . ($obj->id ? '<input type="hidden" name="id_' . $this->table . '" value="' . $obj->id . '" />' : '').' <fieldset><legend><img src="../img/admin/profiles.png" />' . $this- >l( 'Profiles' ) . '</legend> <label>'.$this->l( 'Name:' ).' </label> <div class="margin-form">'; foreach ( $languages as $language ) echo ' <div id="name_' . $language['id_lang'|'id_lang'] . '" style="display: ' . ($language['id_lang'|'id_lang'] == $defaultLanguage ? 'block' : 'none') . '; float: left;"> <input size="33" type="text" name="name_' . $language['id_lang'|'id_lang'] . '" value="' . htmlentities( $this- >getFieldValue( $obj, 'name', intval( $language['id_lang'|'id_lang'] ) ), ENT_COMPAT, 'UTF-8' ) . '" /><sup>*</sup> </div>'; $this->displayFlags( $languages, $defaultLanguage, 'name', 'name' ); echo ' <div class="clear"></div> </div> <div class="margin-form"> <input type="submit" value="' . $this->l( ' Save ' ) . '" name="submitAdd' . $this->table . '" class="button" /> </div> <div class="small"><sup>*</sup> ' . $this->l( 'Required field' ) . '</div> </fieldset> </form> '; } } ?> Put the files online, then create the tab by going to the "Employee" tab, then its "Tabs" sub-tab. Click the "Add new" button, and fill-in the fields with the class' name, "AdminTest". Do not confuse "class" with "modules"! Choose an icon (like one from the FamFamFam pack), choose where the tab should go, and save. You're set! Now start customizing it to your needs! When I added Test Menu (from the guide) on the "Menus" Tab, It went well. Then I tried to click the Test Menu, It showed that The controller AdminTest is missing or invalid. I am using 1.5.6. I know there must be something different between guide and my version. It seems /admin/tabs has been deprecated. There is no other tab-related php file. I guess the main function is in /controller/admin and some method interfaces have been changed. So, how can i make the code (editing and deleting from ps_test) work? Edited September 19, 2013 by JasonWang (see edit history) Link to comment Share on other sites More sharing options...
JasonWang Posted September 19, 2013 Author Share Posted September 19, 2013 Somebody help me... Link to comment Share on other sites More sharing options...
tidjean Posted November 11, 2013 Share Posted November 11, 2013 Hello, first you must add a "install fonction" to you module.php's code. Then you can have into your module directory /modules/yourmodules/controllers/admin/foo.php Here my exemple code : class Routines extends Module{ public function __construct(){ $this->name = 'routines'; $this->tab = 'scripts'; $this->version = 1.0; $this->author = 'Tidev'; $this->need_instance = 0; parent::__construct(); $this->displayName = $this->l('Routines'); $this->description = $this->l('Scripts to manage orders and products.'); } public function install(){ if( (parent::install() == false)||(!$this->_createTab()) ) return false; return true; } public function uninstall(){ if (!parent::uninstall() || !$this->uninstallModuleTab('Stksupplier') || !$this->uninstallModuleTab('reorder') || !$this->uninstallModuleTab('AdminMainNewTab')) return false; return true; } private function _createTab(){ $parent_tab = new Tab(); $parent_tab->class_name = 'AdminMainNewTab'; $parent_tab->id_parent = 0; $parent_tab->module = $this->name; $parent_tab->name[(int)Configuration::get('PS_LANG_DEFAULT')] = 'Routines'; $parent_tab->add(); $this->installModuleTab('Stksupplier', array((int)(Configuration::get('PS_LANG_DEFAULT'))=>'STK'), $parent_tab->id); $this->installModuleTab('reorder', array((int)(Configuration::get('PS_LANG_DEFAULT'))=>$this->l('Re-Order')), $parent_tab->id); // if(!$test) return false; return true; } private function installModuleTab($tabClass, $tabName, $idTabParent){ // $idTab = Tab::getIdFromClassName($idTabParent); $idTab = $idTabParent; $pass = true ; @copy(_PS_MODULE_DIR_.$this->name.'/logo.gif', _PS_IMG_DIR_.'t/'.$tabClass.'.gif'); $tab = new Tab(); $tab->name = $tabName; $tab->class_name = $tabClass; $tab->module = $this->name; $tab->id_parent = $idTab; $pass = $tab->save(); return($pass); } private function uninstallModuleTab($tabClass){ $pass = true ; @unlink(_PS_IMG_DIR_.'t/'.$tabClass.'.gif'); $idTab = Tab::getIdFromClassName($tabClass); if($idTab != 0) { $tab = new Tab($idTab); $pass = $tab->delete(); } return($pass); } 1 Link to comment Share on other sites More sharing options...
JasonWang Posted November 12, 2013 Author Share Posted November 12, 2013 Hello, first you must add a "install fonction" to you module.php's code. Then you can have into your module directory /modules/yourmodules/controllers/admin/foo.php Here my exemple code : class Routines extends Module{ public function __construct(){ $this->name = 'routines'; $this->tab = 'scripts'; $this->version = 1.0; $this->author = 'Tidev'; $this->need_instance = 0; parent::__construct(); $this->displayName = $this->l('Routines'); $this->description = $this->l('Scripts to manage orders and products.'); } public function install(){ if( (parent::install() == false)||(!$this->_createTab()) ) return false; return true; } public function uninstall(){ if (!parent::uninstall() || !$this->uninstallModuleTab('Stksupplier') || !$this->uninstallModuleTab('reorder') || !$this->uninstallModuleTab('AdminMainNewTab')) return false; return true; } private function _createTab(){ $parent_tab = new Tab(); $parent_tab->class_name = 'AdminMainNewTab'; $parent_tab->id_parent = 0; $parent_tab->module = $this->name; $parent_tab->name[(int)Configuration::get('PS_LANG_DEFAULT')] = 'Routines'; $parent_tab->add(); $this->installModuleTab('Stksupplier', array((int)(Configuration::get('PS_LANG_DEFAULT'))=>'STK'), $parent_tab->id); $this->installModuleTab('reorder', array((int)(Configuration::get('PS_LANG_DEFAULT'))=>$this->l('Re-Order')), $parent_tab->id); // if(!$test) return false; return true; } private function installModuleTab($tabClass, $tabName, $idTabParent){ // $idTab = Tab::getIdFromClassName($idTabParent); $idTab = $idTabParent; $pass = true ; @copy(_PS_MODULE_DIR_.$this->name.'/logo.gif', _PS_IMG_DIR_.'t/'.$tabClass.'.gif'); $tab = new Tab(); $tab->name = $tabName; $tab->class_name = $tabClass; $tab->module = $this->name; $tab->id_parent = $idTab; $pass = $tab->save(); return($pass); } private function uninstallModuleTab($tabClass){ $pass = true ; @unlink(_PS_IMG_DIR_.'t/'.$tabClass.'.gif'); $idTab = Tab::getIdFromClassName($tabClass); if($idTab != 0) { $tab = new Tab($idTab); $pass = $tab->delete(); } return($pass); } Thank you so much. I will try this. 1 Link to comment Share on other sites More sharing options...
amavis Posted December 17, 2013 Share Posted December 17, 2013 Hi tidjean, Thank you very much for the code. I was looking for it, but could not find it in the documentation. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now