[Joomla] VirtueMart: caricare immagine per i produttori.

Ecco il mio primo tentativo di hack per Joomla e VM.

Cosa

Attualmente (VM 1.0.5), nella gestione dei produttori, non è possibile allegare un'immagine.

L'hack si propone di sanare questa mancanza.

Teoria

Innanzi tutto si deve modificare il database.

Poi si deve aggiungere nel form di modifica e creazione i campi che riguardano le immagini.

In fine si devono rintracciare le classi preposte alla verifica, convalida e scrittura dei file uplodati.

Cosa ho fatto

Ho giustamente modificato il database.

Ho modificato il form (credo senza errori).

Ho rintracciato tutti i file coinvolti nell'operazione.

Cosa manca

Lo ignoro.

Se inserisco a mano nel database il percorso all'immagine questa si visualizza sia nel form che dove si vuole.

Ma pur seguendo i vari passaggi non riesco a far caricare le immagini ne fisicamente ne simbolicamente nel database.

inviato 10 anni fa
arjuna
X 0 X

Modifica del database

Questa operazione è molto semplice.

Sull'esempio di jos_vm_category applichiamo la seguente query:

ALTER TABLE `jos_vm_manufacturer` ADD `mf_thumb_image` VARCHAR( 255 ) NULL DEFAULT NULL AFTER `mf_desc` ,
ADD `mf_full_image` VARCHAR( 255 ) NULL DEFAULT NULL AFTER `mf_thumb_image`;
risposto 10 anni fa
arjuna
X 0 X

Aggiunta dei campi necessari al caricamento delle immagini

Sull'esempio di product.product_category_form.php ho aggiunto il pannello con i tab e il tab inerente le immagini.

Ho poi sostituito le occorenze di 'category' con 'mf_' o 'manufacturer'.

\administrator\components\com_virtuemart\html\manufacturer.manufacturer_form.php:

find:

global $ps_manufacturer_category;

replace:

global $ps_manufacturer_category, $ps_product;

(serve per gestire le immagini)

find:

$formObj->startForm();

replace:

$formObj->startForm( 'adminForm', 'enctype="multipart/form-data"');

$tabs = new mShopTabs(0, 1, "_main");
$tabs->startPane("manufacturer-pane");
$tabs->startTab( "<img src=\"". IMAGEURL ."ps_image/edit.png\" align=\"center\" width=\"16\" height=\"16\" border=\"0\" />&nbsp;".$VM_LANG->_PHPSHOP_MANUFACTURER_FORM_LBL, "info-page");

( cambiamo l'enctype perchè dobbiamo caricare dei file e creiamo i Tabs.)

find:

</table>
<?php

(It's approssimately at line 80, it's the end of the html template)

add after:

$tabs->endTab();
$tabs->startTab( "<img src=\"". IMAGEURL ."ps_image/image.png\" width=\"16\" height=\"16\" align=\"center\" border=\"0\" />&nbsp;"._E_IMAGES, "status-page");

if( !stristr( $db->f("mf_thumb_image"), "http") )
  echo "<input type=\"hidden\" name=\"mf_thumb_image_curr\" value=\"". $db->f("mf_thumb_image") ."\" />";

if( !stristr( $db->f("mf_full_image"), "http") )
  echo "<input type=\"hidden\" name=\"mf_full_image_curr\" value=\"". $db->f("mf_full_image") ."\" />";

   $ps_html->writableIndicator( array( IMAGEPATH."manufacturer") );

?>

  <table width="100%" border="0" cellspacing="0" cellpadding="2">
    <tr>
      <td valign="top" width="50%" style="border-right: 1px solid black;">
        <h2><?php echo $VM_LANG->_PHPSHOP_PRODUCT_FORM_FULL_IMAGE ?></h2>
        <table>
          <tr>
            <td colspan="2" ><?php
              if ($manufacturer_id) {
                echo $VM_LANG->_PHPSHOP_PRODUCT_FORM_IMAGE_UPDATE_LBL . "<br />"; } ?>
              <input type="file" class="inputbox" name="mf_full_image" size="50" maxlength="255" />
            </td>
          </tr>
          <tr>
            <td colspan="2" ><strong><?php echo $VM_LANG->_PHPSHOP_IMAGE_ACTION ?>:</strong><br/>
              <input type="radio" class="inputbox" name="mf_full_image_action" id="mf_full_image_action0" checked="checked" value="none" onchange="toggleDisable( document.adminForm.mf_full_image_action[1], document.adminForm.mf_thumb_image, true );toggleDisable( document.adminForm.mf_full_image_action[1], document.adminForm.mf_thumb_image_url, true );"/>
              <label for="mf_full_image_action0"><?php echo $VM_LANG->_PHPSHOP_NONE ?></label><br/>
              <?php
              if( function_exists('imagecreatefromjpeg')) {
                    ?>
                 <input type="radio" class="inputbox" name="mf_full_image_action" id="mf_full_image_action1" value="auto_resize" onchange="toggleDisable( document.adminForm.mf_full_image_action[1], document.adminForm.mf_thumb_image, true );toggleDisable( document.adminForm.mf_full_image_action[1], document.adminForm.mf_thumb_image_url, true );"/>
                 <label for="mf_full_image_action1"><?php echo $VM_LANG->_PHPSHOP_FILES_FORM_AUTO_THUMBNAIL . "</label><br />";
              }
              if ($manufacturer_id and $db->f("mf_full_image")) { ?>
                <input type="radio" class="inputbox" name="mf_full_image_action" id="mf_full_image_action2" value="delete" onchange="toggleDisable( document.adminForm.mf_full_image_action[1], document.adminForm.mf_thumb_image, true );toggleDisable( document.adminForm.mf_full_image_action[1], document.adminForm.mf_thumb_image_url, true );"/>
                <label for="mf_full_image_action2"><?php echo $VM_LANG->_PHPSHOP_PRODUCT_FORM_IMAGE_DELETE_LBL . "</label><br />";
              } ?>
            </td>
          </tr>
          <tr><td colspan="2">&nbsp;</td></tr>
          <tr>
            <td width="21%" ><?php echo _URL." ("._CMN_OPTIONAL."!)&nbsp;"; ?></td>
            <td width="79%" >
              <?php
              if( stristr($db->f("mf_full_image"), "http") )
                $manufacturer_full_image_url = $db->f("mf_full_image");
              else if(!empty($_REQUEST['mf_full_image_url']))
                $manufacturer_full_image_url = $_REQUEST['mf_full_image_url'];
              else
                $manufacturer_full_image_url = "";
              ?>
              <input type="text" class="inputbox" size="50" name="mf_full_image_url" value="<?php echo $manufacturer_full_image_url ?>" onchange="if( this.value.length>0) document.adminForm.auto_resize.checked=false; else document.adminForm.auto_resize.checked=true; toggleDisable( document.adminForm.auto_resize, document.adminForm.mf_thumb_image_url, true );toggleDisable( document.adminForm.auto_resize, document.adminForm.mf_thumb_image, true );" />
            </td>
          </tr>
          <tr><td colspan="2">&nbsp;</td></tr>
          <tr>
            <td colspan="2" >
              <div style="overflow:auto;">
                <?php echo $ps_product->image_tag($db->f("mf_full_image"), "", 0, "manufacturer") ?>
              </div>
            </td>
          </tr>
        </table>
      </td>

      <td valign="top" width="50%">
        <h2><?php echo $VM_LANG->_PHPSHOP_PRODUCT_FORM_THUMB_IMAGE ?></h2>
        <table>
          <tr>
            <td colspan="2" ><?php if ($manufacturer_id) {
                echo $VM_LANG->_PHPSHOP_PRODUCT_FORM_IMAGE_UPDATE_LBL . "<br>"; } ?>
              <input type="file" class="inputbox" name="mf_thumb_image" size="50" maxlength="255" onchange="if(document.adminForm.mf_thumb_image.value!='') document.adminForm.mf_thumb_image_url.value='';" />
            </td>
          </tr>
          <tr>
            <td colspan="2" ><strong><?php echo $VM_LANG->_PHPSHOP_IMAGE_ACTION ?>:</strong><br/>
              <input type="radio" class="inputbox" id="mf_thumb_image_action0" name="mf_thumb_image_action" checked="checked" value="none" onchange="toggleDisable( document.adminForm.image_action[1], document.adminForm.mf_thumb_image, true );toggleDisable( document.adminForm.image_action[1], document.adminForm.mf_thumb_image_url, true );"/>
              <label for="mf_thumb_image_action0"><?php echo $VM_LANG->_PHPSHOP_NONE ?></label><br/>
              <?php
              if ($manufacturer_id and $db->f("mf_thumb_image")) { ?>
                <input type="radio" class="inputbox" id="mf_thumb_image_action1" name="mf_thumb_image_action" value="delete" onchange="toggleDisable( document.adminForm.image_action[1], document.adminForm.mf_thumb_image, true );toggleDisable( document.adminForm.image_action[1], document.adminForm.mf_thumb_image_url, true );"/>
                <label for="mf_thumb_image_action1"><?php echo $VM_LANG->_PHPSHOP_PRODUCT_FORM_IMAGE_DELETE_LBL . "</label><br />";
              } ?>
            </td>
          </tr>
          <tr><td colspan="2">&nbsp;</td></tr>
          <tr>
            <td width="21%" ><?php echo _URL." ("._CMN_OPTIONAL.")&nbsp;"; ?></td>
            <td width="79%" >
              <?php
              if( stristr($db->f("mf_thumb_image"), "http") )
                $manufacturer_thumb_image_url = $db->f("mf_thumb_image");
              else if(!empty($_REQUEST['mf_thumb_image_url']))
                $manufacturer_thumb_image_url = $_REQUEST['mf_thumb_image_url'];
              else
                $manufacturer_thumb_image_url = "";
              ?>
              <input type="text" class="inputbox" size="50" name="mf_thumb_image_url" value="<?php echo $manufacturer_thumb_image_url ?>" />
            </td>
          </tr>
          <tr><td colspan="2">&nbsp;</td></tr>
          <tr>
            <td colspan="2" >
              <div style="overflow:auto;">
                <?php echo $ps_product->image_tag($db->f("mf_thumb_image"), "", 0, "manufacturer") ?>
              </div>
            </td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
<?php
$tabs->endTab();
$tabs->endPane();

(questa è la parte più interessante, si tratta di aggiungere tutto il Tab inerente le immagini e modificare i riferimenti alla categoria con quelli al produttore (manufacturer). Qui è possibile che ci sia qualche errore... ma dalle prove effettuate successivamente mi sembra di capire che i file vengono correttamente aquisiti dal Server nelle posizioni temporanee)

find the end of the file:

$formObj->finishForm( $funcname, $modulename.'.manufacturer_list', $option );
?>

add after:

<script language="javascript">
<!--
function toggleDisable( elementOnChecked, elementDisable, disableOnChecked ) {
  if( !disableOnChecked ) {
    if(elementOnChecked.checked==true) {
      elementDisable.disabled=false;
    }
    else {
      elementDisable.disabled=true;
    }
  }
  else {
    if(elementOnChecked.checked==true) {
      elementDisable.disabled=true;
    }
    else {
      elementDisable.disabled=false;
    }
  }
}

toggleDisable( document.adminForm.mf_full_image_action[1], document.adminForm.mf_thumb_image, true );
-->
</script>

(questo JS si occupa solo di abilitare o disabilitare gli input delle miniature in caso si volesse generarle in automatico)

Non credo ci siano errori qui!

risposto 10 anni fa
arjuna
X 0 X

Aggiungere la directory per le immagini

\components\com_virtuemart\shop_image\manufacturer

ma pare che lo script la crei da solo la prima volta che gli serve.

Classi per il controllo dei file

\administrator\components\com_virtuemart\classes\ps_manufacturer.php:

Vi posto l'intero file perchè sarebbe difficile indicarne le modifiche... metto qualche commento nei punti salienti.

<?php
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
/**
*
* @version $Id: ps_manufacturer.php,v 1.4.2.2 2006/03/14 18:42:11 soeren_nb Exp $
* @package VirtueMart
* @subpackage classes
* @copyright Copyright (C) 2004-2005 Soeren Eberhardt. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* VirtueMart is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
*
* http://virtuemart.net
*/

/****************************************************************************
*
* CLASS DESCRIPTION
*
* ps_manufacturer
*
* The class is is used to manage the manufacturers in your store.
*
* properties:
*
*       error - the error message returned by validation if any
* methods:
*       validate_add()
*   validate_delete()
*   validate_update()
*       add()
*       update()
*       delete()
*
*
*************************************************************************/
class ps_manufacturer {
   var $classname = "ps_manufacturer";
   var $error;

   /**************************************************************************
   ** name: validate_add()
   ** created by: soeren
   ** description:
   ** parameters:
   ** returns:
   ***************************************************************************/
   function validate_add($d) {

// $vmLogger non è usato per questa sezione, ve l'ho aggiunto io per monitorare alcuni passaggi.
// questo può aver creato qualche problema visto che precedentemente
// si usavano i Boleani e la variabile $valid.
// ma nonostante i cambiamenti e l'insuccesso relativo all'upload delle immagini, 
// si riesce comunque a inserire e modificare i produttori.
        global $vmLogger;

      $valid = true;
      $db = new ps_DB;

      if (!$d["mf_name"]) {
         $vmLogger->err( "ERROR:  You must enter a name for the manufacturer.");
         $valid = False;
      }
      else {
         $q = "SELECT count(*) as rowcnt from #__{vm}_manufacturer where";
         $q .= " mf_name='" .  $d["mf_name"] . "'";
         $db->setQuery($q);
         $db->query();
         $db->next_record();
         if ($db->f("rowcnt") > 0) {
            $vmLogger->err( "The given manufacturer name already exists.");
            $valid = False;
         }
      }

      /** Image Upload Validation **/

// Tutta questa parte seguente è aggiunta 


      // do we have an image URL or an image File Upload?
      if (!empty( $d['mf_thumb_image_url'] )) {
         // Image URL
         if (substr( $d['mf_thumb_image_url'], 0, 4) != "http") {
            $vmLogger->err( "Image URL must begin with http." );
            $valid =  false;
         }

         $d["mf_thumb_image"] = $d['mf_thumb_image_url'];
      }
      else {
         // File Upload

// la funzione validate_image è contenuta nel file della class ps_main.php (stessa directory)

         if (!validate_image( $d, "mf_thumb_image", "manufacturer")) {
            $valid = false;
         }
      }

      if (!empty( $d['mf_full_image_url'] )) {
         // Image URL
         if (substr( $d['mf_full_image_url'], 0, 4) != "http") {
            $vmLogger->err( "Image URL must begin with http." );
            return false;
         }
         $d["mf_full_image"] = $d['mf_full_image_url'];
      }
      else {
         // File Upload
         if (!validate_image( $d, "mf_full_image", "manufacturer")) {

// nei miei test ho sempre caricato l'immagine da locale
// quindi è questa parte che mi interessa.
// se l'immagine non fosse validata otterrei l'output immagine non validata ma non è così.
             $vmLogger->err( "immagine non validata." );
            $valid = false;
         }
      }
      return $valid;
        }

        /**************************************************************************
        ** name: validate_update
        ** created by: soeren
        ** description:
        ** parameters:
        ** returns:
        ***************************************************************************/
        function validate_update($d) {
            global $vmLogger;
          $valid = true;
                if (!$d["mf_name"]) {
                        $vmLogger->err( "ERROR:  You must enter a name for the manufacturer.");
                        $valid =  false;
                }

                $db =& new ps_DB;
              $q = "SELECT mf_thumb_image,mf_full_image FROM #__{vm}_manufacturer WHERE manufacturer_id='". $d["manufacturer_id"] . "'";
              $db->query( $q );
              $db->next_record();

              /** Image Upload Validation **/

              // do we have an image URL or an image File Upload?
              if (!empty( $d['mf_thumb_image_url'] )) {
                 // Image URL
                 if (substr( $d['mf_thumb_image_url'], 0, 4) != "http") {
                    $vmLogger->err( "An Image URL must begin with 'http'." );
                    $valid =  false;
                 }

                 // if we have an uploaded image file, prepare this one for deleting.
                 if( $db->f("mf_thumb_image") && substr( $db->f("mf_thumb_image"), 0, 4) != "http") {
                    $_REQUEST["mf_thumb_image_curr"] = $db->f("mf_thumb_image");
                    $d["mf_thumb_image_action"] = "delete";
                    if (!validate_image( $d, "mf_thumb_image", "manufacturer")) {
                       return false;
                    }
                 }
                 $d["mf_thumb_image"] = $d['mf_thumb_image_url'];
              }
              else {
                 // File Upload
                 if (!validate_image( $d, "mf_thumb_image", "manufacturer")) {
                    $valid = false;
                 }
              }

              if (!empty( $d['mf_full_image_url'] )) {
                 // Image URL
                 if (substr( $d['mf_full_image_url'], 0, 4) != "http") {
                    $vmLogger->err( "Image URL must begin with 'http'." );
                    return false;
                 }
                 // if we have an uploaded image file, prepare this one for deleting.
                 if( $db->f("mf_full_image") && substr( $db->f("mf_thumb_image"), 0, 4) != "http") {
                    $_REQUEST["mf_full_image_curr"] = $db->f("mf_full_image");
                    $d["mf_full_image_action"] = "delete";
                    if (!validate_image( $d, "mf_full_image", "manufacturer")) {
                        $vmLogger->err( "immagine2 non validata." );
                       return false;
                    }
                 }
                 $d["mf_full_image"] = $d['mf_full_image_url'];
              }
              else {
                 // File Upload
                 if (!validate_image( $d, "mf_full_image", "manufacturer")) {
                     $vmLogger->err( "immagine non validata3." );
                    $valid = false;
                 }
              }

              return $valid;
        }

        /**************************************************************************
        ** name: validate_delete()
        ** created by: soeren
        ** description:
        ** parameters:
        ** returns:
        ***************************************************************************/
        function validate_delete($manufacturer_id) {
                global $db, $vmLogger;

                if (empty( $manufacturer_id )) {
                        $vmLogger->err( "ERROR:  Please select a manufacturer to delete.");
                        return False;
                }
                $db->query( "SELECT jos_vm_product.product_id, manufacturer_id
                                                FROM jos_vm_product, jos_vm_product_mf_xref
                                                WHERE manufacturer_id =".intval($manufacturer_id)."
                                                AND jos_vm_product.product_id = jos_vm_product_mf_xref.product_id" );
                if( $db->num_rows() > 0 ) {
                        $vmLogger->err( "Error: This Manufacturer still has products assigned to it.");
                        return false;
                }
                $q = "SELECT mf_thumb_image, mf_full_image FROM #__{vm}_manufacturer WHERE manufacturer_id='$manufacturer_id'";
              $db->query( $q );
              $db->next_record();

              /* Prepare category_thumb_image for Deleting */
              if( !stristr( $db->f("mf_thumb_image"), "http") ) {
                 $_REQUEST["mf_thumb_image_curr"] = $db->f("mf_thumb_image");
                 $d["mf_thumb_image_action"] = "delete";
                 if (!validate_image($d,"mf_thumb_image","manufacturer")) {
                    $vmLogger->err( "Failed deleting Manufacturer Images!" );
                    return false;
                 }
              }
              /* Prepare product_full_image for Deleting */
              if( !stristr( $db->f("mf_full_image"), "http") ) {
                 $_REQUEST["mf_full_image_curr"] = $db->f("mf_full_image");
                 $d["mf_full_image_action"] = "delete";
                 if (!validate_image($d,"mf_full_image","manufacturer")) {
                     $vmLogger->err( "immagine non validata4." );
                    return false;
                 }
              }
                return True;

        }


   /**************************************************************************
   * name: add()
   * created by: soeren
   * description: creates a new manufacturer record
   * parameters:
   * returns:
   **************************************************************************/
   function add(&$d) {
        global $vmLogger;
        $db = new ps_DB;

        $GLOBALS['vmInputFilter']->safeSQL( $d );

      if ($this->validate_add($d)) {

         if (!process_images($d)) {
            return false;
         }

         while(list($key,$value)= each($d)) {
            if (!is_array($value))
            $d[$key] = addslashes($value);
         }

         $q = "INSERT into #__{vm}_manufacturer (mf_name, mf_email, mf_desc, mf_category_id, mf_url, mf_thumb_image, mf_full_image)";
         $q .= " VALUES ('";
          $q .= $d["mf_name"] . "','";
          $q .= $d["mf_email"] . "','";
          $q .= $d["mf_desc"] . "','";
          $q .= $d["mf_category_id"] . "','";
          $q .= $d["mf_url"] . "')";

         $q .= $d["mf_thumb_image"] . "','";
         $q .= $d["mf_full_image"] . "','";

         $db->setQuery($q);
         $db->query();

         $vmLogger->info( "Successfully added new manufacturer: ".$d['mf_name'].'.');
         return $manufacturer_id;
      }
      else {
         return False;
      }

   }

   /**************************************************************************
   * name: update()
   * created by: soeren
   * description: updates manufacturer information
   * parameters:
   * returns:
   **************************************************************************/
   function update(&$d) {

       global $vmLogger;
        $db = new ps_DB;
        $timestamp = time();

        $GLOBALS['vmInputFilter']->safeSQL( $d );

      foreach ($d as $key => $value) {
         if (!is_array($value))
         $d[$key] = addslashes($value);
      }
      if ($this->validate_update($d)) {
         if (!process_images($d)) {
            return false;
         }
         $q = "UPDATE #__{vm}_manufacturer set ";
         $q .= "mf_name='" . $d["mf_name"]."',";
          $q .= "mf_email='" .$d["mf_email"] . "',";
          $q .= "mf_desc='" .$d["mf_desc"] . "',";
          $q .= "mf_category_id='" .$d["mf_category_id"] . "',";
          $q .= "mf_url='" .$d["mf_url"] . "',";
          $q .= "mf_thumb_image='" . $d["mf_thumb_image"] . "',";
         $q .= "mf_full_image='" . $d["mf_full_image"] ."'";
          $q .= "WHERE manufacturer_id='".$d["manufacturer_id"]."'";
         $db->setQuery($q);
         $db->query();

         $vmLogger->info( "Successfully updated manufacturer: ".$d['mf_name'].'.' );

         return True;
      }
      else {
         return False;
      }
   }

   /**
   * Controller for Deleting Records.
   */
   function delete(&$d) {

      $record_id = $d["manufacturer_id"];

      if( is_array( $record_id)) {
         foreach( $record_id as $record) {
            if( !$this->delete_record( $record, $d ))
            return false;
         }
         return true;
      }
      else {
         return $this->delete_record( $record_id, $d );
      }
   }
   /**
   * Deletes one Record.
   */
   function delete_record( $record_id, &$d ) {
      global $db, $vmLogger;
      if (!$this->validate_delete($record_id)) {

                        return False;
                }
                $q = "DELETE from #__{vm}_manufacturer WHERE manufacturer_id='$record_id'";
                $db->query($q);

                /* Delete Image files */
      if (!process_images($d)) {
         return false;
      }
      $vmLogger->info( "Successfully deleted manufacturer ID: $record_id." );
      return True;

        }

}

?>

Le modifiche sono state effettuate sull'esempio del file: \administrator\components\com_virtuemart\classes\ps_product_category.php

Cosa manca?

risposto 10 anni fa
arjuna
X 0 X

RISPOSTA:

Mancava un riferimento &

Riposto il file corretto: ps_manufacturer.php

<?php
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
/**
*
* @version $Id: ps_manufacturer.php,v 1.4.2.2 2006/03/14 18:42:11 soeren_nb Exp $
* @package VirtueMart
* @subpackage classes
* @copyright Copyright (C) 2004-2005 Soeren Eberhardt. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* VirtueMart is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
*
* http://virtuemart.net
*/

/****************************************************************************
*
* CLASS DESCRIPTION
*
* ps_manufacturer
*
* The class is is used to manage the manufacturers in your store.
*
* properties:
*
*       error - the error message returned by validation if any
* methods:
*       validate_add()
*   validate_delete()
*   validate_update()
*       add()
*       update()
*       delete()
*
*
*************************************************************************/
class ps_manufacturer {
   var $classname = "ps_manufacturer";
   var $error;

   /**************************************************************************
   ** name: validate_add()
   ** created by: soeren
   ** description:
   ** parameters:
   ** returns:
   ***************************************************************************/
   function validate_add(&$d) {
        global $vmLogger;
      $valid = true;
      $db = new ps_DB;

      if (!$d["mf_name"]) {
         $vmLogger->err( "ERROR:  You must enter a name for the manufacturer.");
         $valid = False;
      }
      else {
         $q = "SELECT count(*) as rowcnt from #__{vm}_manufacturer where";
         $q .= " mf_name='" .  $d["mf_name"] . "'";
         $db->setQuery($q);
         $db->query();
         $db->next_record();
         if ($db->f("rowcnt") > 0) {
            $vmLogger->err( "The given manufacturer name already exists.");
            $valid = False;
         }
      }

      /** Image Upload Validation **/

      // do we have an image URL or an image File Upload?
      if (!empty( $d['mf_thumb_image_url'] )) {
         // Image URL
         if (substr( $d['mf_thumb_image_url'], 0, 4) != "http") {
            $vmLogger->err( "Image URL must begin with http." );
            $valid =  false;
         }

         $d["mf_thumb_image"] = $d['mf_thumb_image_url'];
      }
      else {
         // File Upload
         if (!validate_image( $d, "mf_thumb_image", "manufacturer")) {
            $valid = false;
         }
      }

      if (!empty( $d['mf_full_image_url'] )) {
         // Image URL
         if (substr( $d['mf_full_image_url'], 0, 4) != "http") {
            $vmLogger->err( "Image URL must begin with http." );
            return false;
         }
         $d["mf_full_image"] = $d['mf_full_image_url'];
      }
      else {
         // File Upload
         if (!validate_image( $d, "mf_full_image", "manufacturer")) {
             $vmLogger->err( "immagine non validata." );
            $valid = false;
         }
      }
      return $valid;
        }

        /**************************************************************************
        ** name: validate_update
        ** created by: soeren
        ** description:
        ** parameters:
        ** returns:
        ***************************************************************************/
        function validate_update(&$d) {
            global $vmLogger;
          $valid = true;
                if (!$d["mf_name"]) {
                    $vmLogger->err( "ERROR:  You must enter a name for the manufacturer.");
                    $valid =  false;
                }

                $db =& new ps_DB;
              $q = "SELECT mf_thumb_image, mf_full_image FROM #__{vm}_manufacturer WHERE manufacturer_id='". $d["manufacturer_id"] . "'";
              $db->query( $q );
              $db->next_record();

              /** Image Upload Validation **/

              // do we have an image URL or an image File Upload?
              if (!empty( $d['mf_thumb_image_url'] )) {

                 // Image URL
                 if (substr( $d['mf_thumb_image_url'], 0, 4) != "http") {
                    $vmLogger->err( "An Image URL must begin with 'http'." );
                    $valid =  false;
                 }

                 // if we have an uploaded image file, prepare this one for deleting.
                 if( $db->f("mf_thumb_image")
                      && substr( $db->f("mf_thumb_image"), 0, 4) != "http") {

                     $vmLogger->info( "URL: miniatura presente nel database." );

                    $_REQUEST["mf_thumb_image_curr"] = $db->f("mf_thumb_image");
                    $d["mf_thumb_image_action"] = "delete";

                    if (!validate_image( $d, "mf_thumb_image", "manufacturer")) {
                        $vmLogger->err( "URL: The thumb isn't validate." );
                       $valid =  false;
                    }

                 }
                 $d["mf_thumb_image"] = $d['mf_thumb_image_url'];

              } else {

                 // File Upload
                 if (!validate_image( $d, "mf_thumb_image", "manufacturer")) {
                     $vmLogger->err( "errore con la miniatura." );
                    $valid = false;
                 } else {
                     $vmLogger->info( "miniatura registrata." );
                 }
              }



              if (!empty( $d['mf_full_image_url'] )) {

                 // Image URL
                 if (substr( $d['mf_full_image_url'], 0, 4) != "http") {
                    $vmLogger->err( "Image URL must begin with 'http'." );
                    $valid =  false;
                 }

                 // if we have an uploaded image file, prepare this one for deleting.
                 if( $db->f("mf_full_image")
                      && substr( $db->f("mf_thumb_image"), 0, 4) != "http") {

                     $vmLogger->info( "URL: immagine presente nel database." );

                    $_REQUEST["mf_full_image_curr"] = $db->f("mf_full_image");
                    $d["mf_full_image_action"] = "delete";
                    if (!validate_image( $d, "mf_full_image", "manufacturer")) {
                        $vmLogger->err( "URL: The image isn't validate." );
                        $valid =  false;
                    }
                 }
                 $d["mf_full_image"] = $d['mf_full_image_url'];


              } else {


                 // File Upload
                 if (!validate_image( $d, "mf_full_image", "manufacturer")) {
                     $vmLogger->err( "errore con l'immagine." );
                    $valid = false;
                 }else {
                     $vmLogger->info( "immagine registrata." );
                 }
              }

              return $valid;
        }

        /**************************************************************************
        ** name: validate_delete()
        ** created by: soeren
        ** description:
        ** parameters:
        ** returns:
        ***************************************************************************/
        function validate_delete($manufacturer_id) {
                global $db, $vmLogger;

                if (empty( $manufacturer_id )) {
                        $vmLogger->err( "ERROR:  Please select a manufacturer to delete.");
                        return False;
                }
                $db->query( "SELECT jos_vm_product.product_id, manufacturer_id
                                                FROM jos_vm_product, jos_vm_product_mf_xref
                                                WHERE manufacturer_id =".intval($manufacturer_id)."
                                                AND jos_vm_product.product_id = jos_vm_product_mf_xref.product_id" );
                if( $db->num_rows() > 0 ) {
                        $vmLogger->err( "Error: This Manufacturer still has products assigned to it.");
                        return false;
                }
                $q = "SELECT mf_thumb_image, mf_full_image FROM #__{vm}_manufacturer WHERE manufacturer_id='$manufacturer_id'";
              $db->query( $q );
              $db->next_record();

              /* Prepare category_thumb_image for Deleting */
              if( !stristr( $db->f("mf_thumb_image"), "http") ) {
                 $_REQUEST["mf_thumb_image_curr"] = $db->f("mf_thumb_image");
                 $d["mf_thumb_image_action"] = "delete";
                 if (!validate_image($d,"mf_thumb_image","manufacturer")) {
                    $vmLogger->err( "Failed deleting Manufacturer Images!" );
                    return false;
                 }
              }
              /* Prepare product_full_image for Deleting */
              if( !stristr( $db->f("mf_full_image"), "http") ) {
                 $_REQUEST["mf_full_image_curr"] = $db->f("mf_full_image");
                 $d["mf_full_image_action"] = "delete";
                 if (!validate_image($d,"mf_full_image","manufacturer")) {
                     $vmLogger->err( "immagine non validata4." );
                    return false;
                 }
              }
                return True;

        }


   /**************************************************************************
   * name: add()
   * created by: soeren
   * description: creates a new manufacturer record
   * parameters:
   * returns:
   **************************************************************************/
   function add(&$d) {
        global $vmLogger;
        $db = new ps_DB;

        $GLOBALS['vmInputFilter']->safeSQL( $d );

      if ($this->validate_add($d)) {

         if (!process_images($d)) {
            return false;
         }

         while(list($key,$value)= each($d)) {
            if (!is_array($value))
            $d[$key] = addslashes($value);
         }

         $q = "INSERT into #__{vm}_manufacturer (mf_name, mf_email, mf_desc, mf_category_id, mf_url, mf_thumb_image, mf_full_image)";
         $q .= " VALUES ('";
          $q .= $d["mf_name"] . "','";
          $q .= $d["mf_email"] . "','";
          $q .= $d["mf_desc"] . "','";
          $q .= $d["mf_category_id"] . "','";
          $q .= $d["mf_url"] . "','";

         $q .= $d["mf_thumb_image"] . "','";
         $q .= $d["mf_full_image"] . "')";

         $db->setQuery($q);
         $db->query();

         $vmLogger->info( "Successfully added new manufacturer: ".$d['mf_name'].'.');
         return $manufacturer_id;
      }
      else {
         return False;
      }

   }

   /**************************************************************************
   * name: update()
   * created by: soeren
   * description: updates manufacturer information
   * parameters:
   * returns:
   **************************************************************************/
   function update(&$d) {
       global $vmLogger;
        $db = new ps_DB;
        $timestamp = time();

        $GLOBALS['vmInputFilter']->safeSQL( $d );

      foreach ($d as $key => $value) {
         if (!is_array($value))
         $d[$key] = addslashes($value);
      }
      if ($this->validate_update($d)) {
         if (!process_images($d)) {
            return false;
         }

         $q = "UPDATE #__{vm}_manufacturer set ";
         $q .= "mf_name='" . $d["mf_name"]."',";
          $q .= "mf_email='" .$d["mf_email"] . "',";
          $q .= "mf_desc='" .$d["mf_desc"] . "',";
          $q .= "mf_category_id='" .$d["mf_category_id"] . "',";
          $q .= "mf_url='" .$d["mf_url"] . "',";
          $q .= "mf_thumb_image='" . $d["mf_thumb_image"] . "',";
         $q .= "mf_full_image='" . $d["mf_full_image"] ."'";
          $q .= "WHERE manufacturer_id='".$d["manufacturer_id"]."'";
         $db->setQuery($q);
         $db->query();

         $vmLogger->info( "Successfully updated manufacturer: ".$d['mf_name'].'.' );

         return True;
      }
      else {
         return False;
      }
   }

   /**
   * Controller for Deleting Records.
   */
   function delete(&$d) {

      $record_id = $d["manufacturer_id"];

      if( is_array( $record_id)) {
         foreach( $record_id as $record) {
            if( !$this->delete_record( $record, $d ))
            return false;
         }
         return true;
      }
      else {
         return $this->delete_record( $record_id, $d );
      }
   }
   /**
   * Deletes one Record.
   */
   function delete_record( $record_id, &$d ) {
      global $db, $vmLogger;
      if (!$this->validate_delete($record_id)) {

                        return False;
                }
                $q = "DELETE from #__{vm}_manufacturer WHERE manufacturer_id='$record_id'";
                $db->query($q);

                /* Delete Image files */
      if (!process_images($d)) {
         return false;
      }
      $vmLogger->info( "Successfully deleted manufacturer ID: $record_id." );
      return True;

        }

}

?>
risposto 10 anni fa
arjuna
X 0 X

Complimenti per il lavoro svolto  O0

risposto 10 anni fa
Gianni Tomasicchio
X 0 X
Effettua l'accesso o registrati per rispondere a questa domanda