Jump to content

[Prestashop 1.7.7.2] : Problème sur l'upload de vidéo pour un produit.


Recommended Posts

Bonjour à tous,

Dans le cadre d'un développement d'un module qui a pour but d'uploader une vidéo (voir image 1) ou d'insérer un lien d'une vidéo youtube (voir image 2) pour un produit donné.

Le problème rencontré se situe au niveau de l'upload de la vidéo.

J'obtiens le message d'erreur suivant :

Quote

La propriété VideoProduitCustomProduct->chemin est vide.


Ci-dessous le code récupéré directement dans le navigateur :

<form name="form" id="form" method="post" class="form-horizontal product-page row justify-content-md-center" novalidate="novalidate" action="#" enctype="multipart/form-data">

	..................................................................................

	<div id="module_videoproduit" class="module-render-container module-videoproduit" style="">
		<div>
			<div class="row test">
				<div class="col-md-12">
					<p class="subtitle">
						Veuillez remplir les informations concernant la vidéo de votre produit
					</p>
					<div class="row">
						<div class="form-group col-md-4">
							<select name="choix" class="form-control" id="choix">
								<option value="0">&nbsp;</option>
								<option value="1">
									Youtube
								</option>
								<option value="2">
									Télécharger via votre ordinateur
								</option>
							</select>	
						</div>
					</div>
				
					<div class="row" id="bloc-youtube" style="display: none;">
						<div class="form-group col-md-4">
							<input name="youtube" type="text" class="form-control" placeholder="Merci de saisir une url d'une vidéo youtube">
						</div>
					</div>
					<div class="row" id="bloc-upload" style="">
						<div class="form-group col-md-4">
							<input name="upload" type="file" class="form-control">
						</div>
					</div>
				</div>
			</div>
		
			<script>
				$('#bloc-youtube, #bloc-upload').hide();
				
				$("#choix").change(function() {
					if($(this).val()=="1") {
						$('#bloc-upload').hide();
						$('#bloc-youtube').show();
					} else if($(this).val()=="2") {
						$('#bloc-youtube').hide();
						$('#bloc-upload').show();
					} else {
						$('#bloc-youtube, #bloc-upload').hide();
					}
				});
			</script>
		</div>
	  </div>
	</div>
   
	..................................................................................

      <div class="js-spinner spinner hide btn-primary-reverse onclick mr-1 btn"></div>
		<div class="btn-group hide dropdown">
			<button class="btn btn-primary js-btn-save ml-3" type="submit">
				<span>Enregistrer</span>
			</button>
			<button class="btn btn-primary dropdown-toggle dropdown-toggle-split" type="button" id="dropdownMenu" data-toggle="dropdown" aria-expanded="false">
				<span class="sr-only">Toggle Dropdown</span>
			</button>
			
			..................................................................................
			
        </div>
      </div>
    </div>
</form>

Pour finir, voici le code PHP permettant l'enregistrement en base et du document :

public function hookActionProductUpdate($params) {
        $id_product = $params['id_product'];
        $choix = Tools::getValue('choix');
        $chemin = "";
		
        if($choix=="1") {
            $chemin = Tools::getValue('youtube');
        } else {
			require_once(_PS_CLASS_DIR_ . 'Uploader.php');
            $savePath = dirname(__FILE__) . '/views/img/';
			$uploader = new Uploader('upload');
			
			$file = $uploader->setSavePath($savePath)
							 ->setAcceptTypes(['.mp4'])
							 ->process();
							 
			// var_dump($file);
			$chemin = ltrim(str_replace($savePath, '', $file[0]['save_path']), '/');
        }        
        
		// var_dump($chemin);
		
		
        require_once _PS_MODULE_DIR_.'videoproduit/classes/VideoProduitCustomProduct.php';
        $customProduct = VideoProduitCustomProduct::getByIdProduct((int) $id_product);
        $customProduct->id_product = (int) $id_product;
        $customProduct->choix = $choix;
        $customProduct->chemin = $chemin;
        $customProduct->save();
    }

Voici la classe "VideoProduitCustomProduct" :

<?php
/**
 * 2007-2020 PrestaShop
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to [email protected] so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to http://www.prestashop.com for more information.
 *
 * @author    PrestaShop SA <[email protected]>
 * @copyright 2007-2020 PrestaShop SA
 * @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 *  International Registered Trademark & Property of PrestaShop SA
 */

/**
 * Class ColissimoCustomProduct
 */
class VideoProduitCustomProduct extends ObjectModel
{
    /** @var int $id_video */
//    public $id_video;
    
    /** @var int $id_product */
    public $id_product;

    /** @var int $choix */
    public $choix;

    /** @var string $chemin */
    public $chemin;

    /** @var array $definition */
    public static $definition = array(
        'table' => 'video',
        'primary' => 'id_video',
        'fields' => array(
            'id_product' => array('type' => self::TYPE_INT, 'required' => true),
            'choix' => array('type' => self::TYPE_INT, 'required' => true),
            'chemin' => array('type' => self::TYPE_STRING, 'required' => true, 'size' => 255),
        ),
    );

    /**
     * @param int $idProduct
     * @return VideoProduitCustomProduct
     * @throws PrestaShopDatabaseException
     * @throws PrestaShopException
     */
    public static function getByIdProduct($id_product)
    {
        $dbQuery = new DbQuery();
        $dbQuery->select('id_video')
                ->from('video')
                ->where('id_product = '.(int) $id_product);
        $id = Db::getInstance(_PS_USE_SQL_SLAVE_)
                ->getValue($dbQuery);

        return new self((int) $id);
    }
}


A noter, lorsque je modifie le code PHP de la méthode "hookActionProductUpdate" de la manière suivante [Ajout du var_dump($file)) :

public function hookActionProductUpdate($params) {
        $id_product = $params['id_product'];
        $choix = Tools::getValue('choix');
        $chemin = "";
		
        if($choix=="1") {
            $chemin = Tools::getValue('youtube');
        } else {
			require_once(_PS_CLASS_DIR_ . 'Uploader.php');
            $savePath = dirname(__FILE__) . '/views/img/';
			$uploader = new Uploader('upload');
			
			$file = $uploader->setSavePath($savePath)
							 ->setAcceptTypes(['.mp4'])
							 ->process();
							 
			var_dump($file);
			$chemin = ltrim(str_replace($savePath, '', $file[0]['save_path']), '/');
        }        
        
		// var_dump($chemin);
		
		
        require_once _PS_MODULE_DIR_.'videoproduit/classes/VideoProduitCustomProduct.php';
        $customProduct = VideoProduitCustomProduct::getByIdProduct((int) $id_product);
        $customProduct->id_product = (int) $id_product;
        $customProduct->choix = $choix;
        $customProduct->chemin = $chemin;
        $customProduct->save();
    }

Voici ce que j'obtiens :

Quote

SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 3 column 1 of the JSON data

array(0) {

}

{"error":["La propri\u00e9t\u00e9 VideoProduitCustomProduct-\u003Echemin est vide."


Quelqu'un a-t-il une idée pourquoi mon champ d'upload n'est pas pris en compte dans le code reçu lors de l'enregistrement ?

Merci par avance pour votre aide.
Cordialement,
Loïc V.

 

image 1.png

image 2.png

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...