Jump to content

Gestion d'une table BDD en B.O ?


Recommended Posts

Bonjour,

 

Je suis actuellement bloqué sur quelques chose d'assez simple.

En fait je dois juste gérer une table de ma base de donnée dans le back office (a placer dans la gestion de mon module ou autre part ce n'est pas important). Ce n'est pas une table PS, c'est une table que j'ai crée moi même.

Il me faut quelque chose du genre : 1433250135-cat037-carac1-fr.png

Avec des options d'ajout, de suppression, de modiftcation etc...

Il me faut 1 champ id et 5 champs texte (dont un oû on va gérer l'upload d'une image et y affecter son emplacement).

 

C'est assez simple à faire en PHP classique, mais là sur PS je ne sais pas trop comment m'y prendre :mellow: .

 

Est-ce-que quelqu'un aurais des pistes à me proposer ou des exemples de code d'autre module?

 

PS : j'ai rechercher avant de poster mais sans grand résultat.

 

Merci d'avance pour votre aide.

Edited by salfai (see edit history)
Link to comment
Share on other sites

Salut,
 
Alors déjà encore merci ça m'as bien aidé.
Je suis dessus depuis hier. Après pas mal de problème et de recherche, j'arrive à afficher les éléments de ma table et à en ajouter de nouveau.
Par contre impossible de les supprimer (ça ne fait rien), et quand je modifie un élément il s'ajoute au lieu de remplacer l'ancien. J'aurais besoin d'aide là-dessus.
 
Autre soucis, il me mettait une erreur par rapport à la position. En commentant un morceau de code je n'ai plus l'erreur. D'ailleurs je n'ai pas très bien compris à quoi la position sert (si c'est un attribut du modèle ou autre chose)
 
(Note : je n'utilise pas de système de multi-langue, je l'ai donc mis sur false un peu partout)
 
Voilà mon code ci dessous. Je n'ai pas modifié de nom de classe ou autre pour que ce soit plus simple.
 
ExampleData.php (Ajout d'attribut, et multilang en false)

 

// ...

class ExampleData extends ObjectModel
{
/** @var string Name */
public $name;
public $prenom;
public $statut;
public $img;
public $lien_presentation;

/**
* @see ObjectModel::$definition
*/
public static $definition = array(
'table' => 'example_data',
'primary' => 'id_example_data',
'multilang' => false,
'fields' => array(
/* Lang fields */
'name' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName','required' => true, 'size' => 50),
'prenom' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => false, 'size' => 50),
'statut' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => false, 'size' => 50),
'img' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => false, 'size' => 250),
'lien_presentation' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => false, 'size' => 250),
),
);
}

 



AdminExampleController.php

 

 

 

<?php
// ...

class AdminExampleController extends ModuleAdminController
{
	public function __construct()
	{
		$this->table = 'example_data';
		$this->className = 'ExampleData';
		$this->lang = true;
		$this->deleted = false;
		$this->colorOnBackground = false;
		$this->bulk_actions = array('delete' => array('text' => 'Supprimer la séléction', 'confirm' => $this->l('Delete selected items?')));
		$this->context = Context::getContext();
		$this->bootstrap=true;
		// définition de l'upload, chemin par défaut _PS_IMG_DIR_
		$this->fieldImageSettings = array('name' => 'image', 'dir' => 'example');

		parent::__construct();
	}

	/**
	 * Function used to render the list to display for this controller
	 */
	public function renderList()
	{
		$this->addRowAction('edit');
		$this->addRowAction('delete');
		$this->addRowAction('details');

		$this->bulk_actions = array(
			'delete' => array(
				'text' => 'Supprimer la séléction',
				'confirm' => $this->l('Delete selected items?')
				)
			);

		$this->fields_list = array(
			'id_example_data' => array(
				'title' => $this->l('ID'),
				'align' => 'center',
				'width' => 25
			),
			'name' => array(
				'title' => $this->l('Name'),
				'width' => 'auto',
			),
			'prenom' => array(
				'title' => $this->l('prenom'),
				'width' => 'auto',
			),
			'statut' => array(
				'title' => $this->l('statut'),
				'width' => 'auto',
			),
			'img' => array(
				'title' => $this->l('img'),
				'width' => 'auto',
			),
			'lien_presentation' => array(
				'title' => $this->l('lien_presentation'),
				'width' => 'auto',
			),
		);

		// Gère les positions
		//Mis en commentaire sinon erreur
		/*$this->fields_list['position'] = array(
			'title' => $this->l('Position'),
			'width' => 70,
			'align' => 'center',
			'position' => 'position'
		);*/

		$lists = parent::renderList();

		parent::initToolbar();

		return $lists;
	}

	/**
	 * method call when ajax request is made with the details row action
	 * @see AdminController::postProcess()
	 */
	public function ajaxProcessDetails()
	{
		if (($id = Tools::getValue('id')))
		{
			// override attributes
			$this->display = 'list';
			$this->lang = false;

			$this->addRowAction('edit');
			$this->addRowAction('delete');

			$this->_select = 'b.*';
			$this->_join = 'LEFT JOIN `'._DB_PREFIX_.'tab_lang` b ON (b.`id_tab` = a.`id_tab` AND b.`id_lang` = '.$this->context->language->id.')';
			$this->_where = 'AND a.`id_parent` = '.(int)$id;
			$this->_orderBy = 'position';

			// get list and force no limit clause in the request
			$this->getList($this->context->language->id);

			// Render list
			$helper = new HelperList();
			$helper->actions = $this->actions;
			$helper->list_skip_actions = $this->list_skip_actions;
			$helper->no_link = true;
			$helper->shopLinkType = '';
			$helper->identifier = $this->identifier;
			$helper->imageType = $this->imageType;
			$helper->toolbar_scroll = false;
			$helper->show_toolbar = false;
			$helper->orderBy = 'position';
			$helper->orderWay = 'ASC';
			$helper->currentIndex = self::$currentIndex;
			$helper->token = $this->token;
			$helper->table = $this->table;
			$helper->position_identifier = $this->position_identifier;
			// Force render - no filter, form, js, sorting ...
			$helper->simple_header = true;
			$content = $helper->generateList($this->_list, $this->fields_list);

			echo Tools::jsonEncode(array('use_parent_structure' => false, 'data' => $content));
		}

		die;
	}

	public function renderForm()
	{
		$this->fields_form = array(
			'tinymce' => true,
			'legend' => array(
				'title' => $this->l('Example'),
				'image' => '../img/admin/cog.gif'
			),
			'input' => array(
				array(
					'type' => 'text',
					'lang' => false,
					'label' => 'Nom : ',
					'name' => 'name',
					'size' => 50
				),
				array(
					'type' => 'text',
					'lang' => false,
					'label' => 'Prénom : ',
					'name' => 'prenom',
					'size' => 50
				),
				array(
					'type' => 'text',
					'lang' => false,
					'label' => 'Statut : ',
					'name' => 'statut',
					'size' => 50
				),
				array(
					'type' => 'text',
					'lang' => false,
					'label' => 'Image : ',
					'name' => 'img',
					'size' => 50
				),
				array(
					'type' => 'text',
					'lang' => false,
					'label' => 'URL présentation : ',
					'name' => 'lien_presentation',
					'size' => 50
				),
				/*array(
					'type' => 'file',
					'label' => $this->l('Logo:'),
					'name' => 'image',
					'display_image' => true,
					'desc' => $this->l('Upload Example image from your computer')
				)*/
			),
			'submit' => array(
				'title' => $this->l('Save'),
				'class' => 'button'
			)
		);

		if (!($obj = $this->loadObject(true)))
			return;

		/* Thumbnail
		 * @todo Error, deletion of the image
		*/
		$image = ImageManager::thumbnail(_PS_IMG_DIR_.'region/'.$obj->id.'.jpg', $this->table.'_'.(int)$obj->id.'.'.$this->imageType, 350, $this->imageType, true);

		$this->fields_value = array(
			'image' => $image ? $image : false,
			'size' => $image ? filesize(_PS_IMG_DIR_.'example/'.$obj->id.'.jpg') / 1000 : false,

		);

		return parent::renderForm();
	}

	public function postProcess()
	{
		if (Tools::isSubmit('submitAdd'.$this->table))
		{
			// Create Object ExampleData
			$exemple_data = new ExampleData();
			$exemple_data->name = Tools::getValue('name');
			$exemple_data->prenom = Tools::getValue('prenom');
			$exemple_data->statut = Tools::getValue('statut');
			$exemple_data->img = Tools::getValue('img');
			$exemple_data->lien_presentation = Tools::getValue('lien_presentation');




			// Plus besoin car plus de multilang.
			/*
			$languages = Language::getLanguages(false);
				foreach ($languages as $language)
					$exemple_data->name[$language['id_lang']] = Tools::getValue('name_'.$language['id_lang']);
			*/


			// Save object
			if (!$exemple_data->save())
				$this->errors[] = Tools::displayError('An error has occurred: Can\'t save the current object');
			else
				Tools::redirectAdmin(self::$currentIndex.'&conf=4&token='.$this->token);
		}
	}
}

 

 

 

Pour le champ img je verrais plus tard pour gérer l'upload directement.
 
 
Si tu pouvais m'aidez là dessus s'il te plaît.
 
 
Merci d'avance.
PS : si quelqu'un vois des erreurs qu'il n'hésite pas à me corriger.

Edited by salfai (see edit history)
Link to comment
Share on other sites

Hello,

 

 

Bonne nouvelle j'ai réussie à faire fonctionne l'ajout, la modif et la suppresion. J'ai pas totalement utilisé le code de l'exemple canvas.

Par contre je l'ai utilisé pour l'upload d'image et du coup j'ai un nouveau problème. L'image s'upload bien, je la retrouve bien. Par contre dans la BDD son chemin/nom ne s'enregistre pas. Quelqu'un pourrais m'aider svp?

 

Voilà mon code :

 

GestionExpert.php (contrôleur)

 

 

<?php

/* Loading Models */
require_once(_PS_MODULE_DIR_.'expertise/models/ExampleData.php');


class GestionExpert extends ModuleAdminController {

	public function __construct()
	{
		$this->bootstrap = true;
		$this->table 	= 'example_data';
		$this->className 	= 'ExampleData';
		$this->bulk_actions = array('delete' => array('text' => 'Supprimer la sélection', 'confirm' => $this->l('Voulez-vous vraiment supprimer la sélection?')));
		$this->lang = false;
		

		// définition de l'upload, chemin par défaut _PS_IMG_DIR_
		$this->fieldImageSettings = array('name' => 'img', 'dir' => 'example');
		parent :: __construct();

		$this->initList();
		$this->renderForm();
	}

	private function initList()
	{
		$this->bulk_actions = array(
			'delete' => array(
				'text' => 'Supprimer la sélection',
				'confirm' => $this->l('Voulez-vous vraiment supprimer la sélection ?')
				)
			);

		$this->fields_list = array(
			'id_example_data' => array(
				'title' => $this->l('ID'),
				'align' => 'center',
				'width' => 25
				),
			'name' => array(
				'title' => 'Nom',
				'width' => 'auto',
				),
			'prenom' => array(
				'title' => 'Prénom',
				'width' => 'auto',
				),
			'statut' => array(
				'title' => 'Statut',
				'width' => 'auto',
				),
			'img' => array(
				'title' => 'Photo',
				'width' => 'auto',
				),
			'lien_presentation' => array(
				'title' => 'URL Présentation',
				'width' => 'auto',
				),
			);
		$this->addRowAction('edit');
		$this->addRowAction('delete');
	}

	public function renderForm() {

		if (!($obj = $this->loadObject(true))){
			return;
		}

		$img = ImageManager::thumbnail(_PS_IMG_DIR_.'region/'.$obj->id.'.jpg', $this->table.'_'.(int)$obj->id.'.'.$this->imageType, 350, $this->imageType, true);


		$this->fields_form = array(

			'legend' => array(
				'title' => $this->l('Test')
				),
			'input' => array(
				array(
					'type' => 'text',
					'lang' => false,
					'label' => 'Nom : ',
					'name' => 'name',
					'size' => 50
					),
				array(
					'type' => 'text',
					'lang' => false,
					'label' => 'Prenom : ',
					'name' => 'prenom',
					'size' => 50
					),
				array(
					'type' => 'text',
					'lang' => false,
					'label' => 'Statut : ',
					'name' => 'statut',
					'size' => 50
					),
				array(
					'type' => 'file',
					'label' => $this->l('Logo:'),
					'name' => 'img',
					'image' => $img ? $img : false,
					'display_image' => true,
					'desc' => $this->l('Upload Example image from your computer')
					)
				),

			'submit' => array(
				'title' => $this->l('    Save   '),
				'class' => 'button'
				)
			);


		return parent::renderForm();

	}


}

 

 

 

ExampleData.php (modèle)

 

 

 

<?php


class ExampleData extends ObjectModel
{
	/** @var string Name */
	public $name;
	public $prenom;
	public $statut;
	public $img;
	public $lien_presentation;

	/**
	 * @see ObjectModel::$definition
	 */
	public static $definition = array(
		'table' => 'example_data',
		'primary' => 'id_example_data',
		'multilang' => false,
		'fields' => array(
			/* Lang fields */
			'name' => 		array('type' => self::TYPE_STRING, 'validate' => 'isGenericName','required' => true, 'size' => 50),
			'prenom' => 		array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => false, 'size' => 50),
			'statut' => 		array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => false, 'size' => 50),
			'img' => 		array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => false, 'size' => 250),
			'lien_presentation' => 		array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => false, 'size' => 250),
		),
	);


}

 

 

 

Il faudrais juste que dans le champ "img" de ma BDD je récupère le lien vers l'image du genre "example/ID.jpg".

 

Merci d'avance pour votre aide.

Edited by salfai (see edit history)
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...