[ Index ]

PHP Cross Reference of osCMax 2.0.4

title

Body

[close]

/includes/classes/ -> pad_base.php (source)

   1  <?php
   2  /*

   3        QT Pro Version 4.1

   4    

   5        pad_base.php

   6    

   7        Contribution extension to:

   8          osCMax Power E-Commerce

   9          http://oscdox.com

  10       

  11        Copyright 2006 osCMax2004, 2005 Ralph Day

  12        Released under the GNU General Public License

  13    

  14        Based on prior works released under the GNU General Public License:

  15          QT Pro prior versions

  16            Ralph Day, October 2004

  17            Tom Wojcik aka TomThumb 2004/07/03 based on work by Michael Coffman aka coffman

  18            FREEZEHELL - 08/11/2003 [email protected] Copyright 2006 osCMax2003 IBWO

  19            Joseph Shain, January 2003

  20          osCommerce MS2

  21            Copyright 2006 osCMax

  22        Modifications made:

  23            11/2004 - Created

  24            12/2004 - Fix _draw_js_stock_array to prevent error when all attribute combinations are

  25                      out of stock.

  26    

  27  *******************************************************************************************

  28    

  29        QT Pro Product Attributes Display Plugin

  30    

  31        pad_base.php - Base Class

  32    

  33        Class Name: pad_base

  34    

  35        This base class, although functional, is not intended to be installed and used

  36        directly.  It is extended by other classes to provide different display options

  37        for product attributes on the product information page (product_info.php).

  38    

  39    

  40        Methods:

  41    

  42          pad_base                            constructor

  43          _SetConfigurationProperties         set local properties from DB config constants

  44          draw                                draw the product attributes

  45          _draw_table_start                   draw start of the table to enclose the attributes display

  46          _draw_stocked_attributes            draw attributes that stock is tracked for

  47          _draw_nonstocked_attributes         draw attributes that stock is not tracked for

  48          _draw_table_end                     draw end of the table to enclose the attributes display

  49          _draw_js_stock_array                draw a Javascript array of in stock attribute combinations

  50          _build_attributes_array             build an array of the attributes for the product

  51          _build_attributes_combinations      build an array of the attribute combinations for the product

  52    

  53        Properties:

  54    

  55          products_id                         the product id for attribute display

  56          products_tax_class_id               the products tax class id

  57          show_out_of_stock                   show out of stock attributes flag

  58          mark_out_of_stock                   mark out of stock attributes flag

  59          out_of_stock_msgline                show out of stock message line flag

  60          no_add_out_of_stock                 prevent add to cart of out of stock attributes combinations

  61    

  62    

  63  */
  64    class pad_base {
  65      var $products_id;
  66      var $products_tax_class_id;
  67      var $show_out_of_stock;
  68      var $mark_out_of_stock;
  69      var $out_of_stock_msgline;
  70      var $no_add_out_of_stock;
  71  
  72  
  73  /*

  74      Method: pad_base

  75    

  76      Class constructor

  77    

  78      Parameters:

  79    

  80        $products_id      integer     The product id of the product attributes are to be displayed for

  81    

  82      Returns:

  83    

  84        nothing

  85    

  86  */
  87      function pad_base($products_id=0) {
  88  
  89  
  90        $this->products_id  = $products_id;
  91        
  92        if ($this->products_id != 0) {
  93          $tax_class_query = tep_db_query('SELECT products_tax_class_id 
  94                                           FROM ' . TABLE_PRODUCTS . " 
  95                                           WHERE products_id = '" . (int)$products_id . "'");
  96          $tax_class_array = tep_db_fetch_array($tax_class_query);
  97          $this->products_tax_class_id = $tax_class_array['products_tax_class_id'];
  98        }
  99        
 100        $this->_SetConfigurationProperties('PRODINFO_ATTRIBUTE_');
 101  
 102      }
 103  
 104  
 105  /*

 106      Method: _SetConfigurationProperties

 107    

 108      Set local configuration properties from osCommerce configuration DB constants

 109    

 110      Parameters:

 111    

 112        $prefix      sting     Prefix for the osCommerce DB constants

 113    

 114      Returns:

 115    

 116        nothing

 117    

 118  */
 119      function _SetConfigurationProperties($prefix) {
 120  
 121        $this->show_out_of_stock    = constant($prefix . 'SHOW_OUT_OF_STOCK');
 122        $this->mark_out_of_stock    = constant($prefix . 'MARK_OUT_OF_STOCK');
 123        $this->out_of_stock_msgline = constant($prefix . 'OUT_OF_STOCK_MSGLINE');
 124        $this->no_add_out_of_stock  = constant($prefix . 'NO_ADD_OUT_OF_STOCK');
 125  
 126      }
 127  
 128  
 129  /*

 130      Method: draw

 131    

 132      Draws the product attributes.  This is the only method other than the constructor that is

 133      intended to be called by a user of this class.

 134    

 135      Attributes that stock is tracked for are grouped first and drawn with one dropdown list per

 136      attribute.  All attributes are drawn even if no stock is available for the attribute and no 

 137      indication is given that the attribute is out of stock.

 138    

 139      Attributes that stock is not tracked for are then drawn with one dropdown list per

 140      attribute.

 141    

 142      Parameters:

 143    

 144        none

 145    

 146      Returns:

 147    

 148        string:       HTML for displaying the product attributes

 149    

 150  */
 151      function draw() {
 152  
 153        $out=$this->_draw_table_start();
 154  
 155        $out.=$this->_draw_stocked_attributes();
 156        
 157        $out.=$this->_draw_nonstocked_attributes();
 158      
 159        $out.=$this->_draw_table_end();
 160        
 161        return $out;
 162        
 163      }
 164  
 165  
 166  /*

 167      Method: _draw_table_start

 168    

 169      Draws the start of a table to wrap the product attributes display.

 170      Intended for class internal use only.

 171    

 172      Parameters:

 173    

 174        none

 175    

 176      Returns:

 177    

 178        string:       HTML for start of table

 179    

 180  */
 181      function _draw_table_start() {
 182        $out ='           <table border="0" cellspacing="0" cellpadding="2">';
 183        $out.='            <tr>';
 184        $out.='             <td class="main" colspan="2"><b>' . TEXT_PRODUCT_OPTIONS . '</b></td>';
 185        $out.='            </tr>';
 186        return $out;
 187      }
 188  
 189  
 190  /*

 191      Method: _draw_stocked_attributes

 192    

 193      Draws the product attributes that stock is tracked for.

 194      Intended for class internal use only.

 195    

 196      Attributes that stock is tracked for are drawn with one dropdown list per attribute.

 197      All attributes are drawn even if no stock is available for the attribute and no 

 198      indication is given that the attribute is out of stock.

 199    

 200      Parameters:

 201    

 202        none

 203    

 204      Returns:

 205    

 206        string:       HTML for displaying the product attributes that stock is tracked for

 207    

 208  */
 209      function _draw_stocked_attributes() {
 210        $out = '';
 211      
 212        $attributes = $this->_build_attributes_array(true, false);
 213        if (sizeof($attributes)>0) {
 214          foreach ($attributes as $stocked) {
 215            $out .= '<tr><td align="right" class=main><b>' . $stocked['oname'] . ":</b></td><td class=main>" . tep_draw_pull_down_menu('id['.$stocked['oid'].']',array_values($stocked['ovals']),$stocked['default']) . "</td></tr>\n";
 216          }
 217        }
 218        return $out;
 219      }
 220  
 221  
 222  /*

 223      Method: _draw_nonstocked_attributes

 224    

 225      Draws the product attributes that stock is not tracked for.

 226      Intended for class internal use only.

 227    

 228      Attributes that stock is not tracked for are drawn with one dropdown list per attribute.

 229    

 230      Parameters:

 231    

 232        none

 233    

 234      Returns:

 235    

 236        string:       HTML for displaying the product attributes that stock is not tracked for

 237    

 238  */
 239      function _draw_nonstocked_attributes() {
 240        $out='';
 241        $nonstocked_attributes = $this->_build_attributes_array(false, true);
 242        foreach($nonstocked_attributes as $nonstocked)
 243        {
 244          $out.='<tr><td align="right" class=main><b>'.$nonstocked['oname'].":</b></td><td class=main>".tep_draw_pull_down_menu('id['.$nonstocked['oid'].']',$nonstocked['ovals'],$nonstocked['default'])."</td></tr>\n";
 245        }
 246        return $out;
 247      }
 248  
 249  
 250  /*

 251      Method: _draw_table_end

 252    

 253      Draws the end of a table to wrap the product attributes display.

 254      Intended for class internal use only.

 255    

 256      Parameters:

 257    

 258        none

 259    

 260      Returns:

 261    

 262        string:       HTML for end of table

 263    

 264  */
 265      function _draw_table_end() {
 266        return '           </table>';
 267      }
 268  
 269  
 270  /*

 271      Method: _build_attributes_array

 272    

 273      Build an array of the attributes for the product

 274    

 275      Parameters:

 276    

 277        $build_stocked        boolean   Flag indicating if stocked attributes should be built.

 278        $build_nonstocked     boolean   Flag indicating if non-stocked attribute should be built.

 279    

 280      Returns:

 281    

 282        array:                Array of attributes for the product of the form:

 283                                'oid'       => integer: products_options_id

 284                                'oname'     => string:  products_options_name

 285                                'ovals'     => array:   option values for the option id of the form

 286                                               'id'    => integer:  products_options_values_id

 287                                               'text'  => string:   products_options_values_name

 288                                'default'   => integer: products_options_values_id that the product id

 289                                                        contains for this option id and should be the

 290                                                        default selection when this attribute is drawn.

 291                                                        Set to zero if the product id did not contain

 292                                                        this option. 

 293    

 294  */
 295      function _build_attributes_array($build_stocked, $build_nonstocked) {
 296        global $languages_id;
 297        global $currencies;
 298        global $cart;
 299      
 300        if (!($build_stocked | $build_nonstocked)) return null;
 301        
 302        if ($build_stocked && $build_nonstocked) {
 303          $stocked_where='';
 304        }
 305        elseif ($build_stocked) {
 306          $stocked_where="and popt.products_options_track_stock = '1'";
 307        }
 308        elseif ($build_nonstocked) {
 309          $stocked_where="and popt.products_options_track_stock = '0'";
 310        }
 311        
 312        $products_options_name_query = tep_db_query("select distinct popt.products_options_id, popt.products_options_name, popt.products_options_track_stock from " . TABLE_PRODUCTS_OPTIONS . " popt, " . TABLE_PRODUCTS_ATTRIBUTES . " patrib where patrib.products_id='" . (int)$this->products_id . "' and patrib.options_id = popt.products_options_id and popt.language_id = '" . (int)$languages_id . "' " . $stocked_where . " order by popt.products_options_name");
 313        $attributes=array();
 314        while ($products_options_name = tep_db_fetch_array($products_options_name_query)) {
 315          $products_options_array = array();
 316          $products_options_query = tep_db_query("select pov.products_options_values_id, pov.products_options_values_name, pa.options_values_price, pa.price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov where pa.products_id = '" . (int)$this->products_id . "' and pa.options_id = '" . (int)$products_options_name['products_options_id'] . "' and pa.options_values_id = pov.products_options_values_id and pov.language_id = '" . (int)$languages_id . "'");
 317          while ($products_options = tep_db_fetch_array($products_options_query)) {
 318            $products_options_array[] = array('id' => $products_options['products_options_values_id'], 'text' => $products_options['products_options_values_name']);
 319            if ($products_options['options_values_price'] != '0') {
 320              $products_options_array[sizeof($products_options_array)-1]['text'] .= ' (' . $products_options['price_prefix'] . $currencies->display_price($products_options['options_values_price'], tep_get_tax_rate($this->products_tax_class_id)) .')';
 321            }
 322          }
 323          if (isset($cart->contents[$this->products_id]['attributes'][$products_options_name['products_options_id']]))
 324            $selected = $cart->contents[$this->products_id]['attributes'][$products_options_name['products_options_id']];
 325          else 
 326            $selected = 0;
 327          $attributes[]=array('oid'=>$products_options_name['products_options_id'],
 328                              'oname'=>$products_options_name['products_options_name'],
 329                              'ovals'=>$products_options_array,
 330                              'default'=>$selected);
 331        }
 332        return $attributes;
 333      }
 334  
 335  
 336  /*

 337      Method: _build_attributes_combinations

 338    

 339      A recursive method for building an array enumerating the attribute combinations for the product

 340    

 341      Parameters:

 342    

 343        $attributes             array     An array of the attributes that combinations will be built for.

 344                                          Format is as returned by _build_attributes_array.

 345        $showoos                boolean   Flag indicating if non-stocked attributes should be built.

 346        $markoos                string    'Left' if out of stock indication is to be appended in front of the

 347                                          attribute combination text.  'Right' if out of stock indication is

 348                                          to be appended at the end of the attribute combination text.

 349        $combinations           array     Array of the attribute combinations is returned in this parameter.

 350                                          Should be set to an empty array before an external call to this method. 

 351                                            'comb'        => array:   array of a single attribute combination

 352                                                                        options_id => options_value_id

 353                                            'id'          => string:  options/values string for this 

 354                                                                      combination in the form for the

 355                                                                      key of the products_stock table

 356                                                                        opt_id-val_id,opt_id-val_id,...

 357                                            'text'        => string:  Text for this combination.  Values text

 358                                                                      is as built by _build_attributes_array

 359                                                                       and contains the add/subtract price for

 360                                                                       the option value if applicable.  Form is:

 361                                                                         values_text, values_text

 362        $selected_combination   integer   Index into the $combinations array of the combination that should

 363                                          be the default selection when the combination is drawn is returned in

 364                                          this parameter.  Determined from product id.  Should be set to zero

 365                                          before an external call to this method.

 366    

 367      Parameters for internal recursion use only:

 368    

 369        $oidindex               integer   Index into the $attributes array of the option to operate on.

 370        $comb                   array     Array containing option id/values of combination built so far

 371                                            products_options_id => products_options_value_id

 372        $id                     string    Contains string of options/values built so far

 373        $text                   string    Text for the options values constructed so far.

 374        $isselected             boolean   Flag indicating if so far all option values in this combination

 375                                          were indicated to be defaults in the product id.

 376        

 377    

 378      Returns:

 379    

 380        see $combinations and $selected_combination parameters above

 381        no actual function return value.

 382    

 383  */
 384      function _build_attributes_combinations($attributes, $showoos, $markoos, &$combinations, &$selected_combination, $oidindex=0, $comb=array(), $id="", $text='', $isselected=true) {
 385        global $cart;
 386      
 387        foreach ($attributes[$oidindex]['ovals'] as $attrib) {
 388          $newcomb = $comb;
 389          $newcomb[$attributes[$oidindex]['oid']] = $attrib['id'];
 390          $newid=$id.','.$attributes[$oidindex]['oid'].'-'.$attrib['id'];
 391          $newtext = $text.", ".$attrib['text'];
 392          if (isset($cart->contents[$this->products_id]['attributes'][$attributes[$oidindex]['oid']]))
 393            $newisselected = ($cart->contents[$this->products_id]['attributes'][$attributes[$oidindex]['oid']] == $attrib['id']) ? $isselected : false;
 394          else 
 395            $newisselected = false;
 396          if (isset($attributes[$oidindex+1])) {
 397            $this->_build_attributes_combinations($attributes, $showoos, $markoos, $combinations, $selected_combination, $oidindex+1, $newcomb, $newid, $newtext, $newisselected);
 398          }
 399          else {
 400            $is_out_of_stock=tep_check_stock(tep_get_prid($this->products_id),1,$newcomb);
 401            if (!$is_out_of_stock | ($showoos == true)) {
 402              switch ($markoos) {
 403                case 'Left':   $newtext=($is_out_of_stock ? TEXT_OUT_OF_STOCK.' - ' : '').substr($newtext,2);
 404                               break;
 405                case 'Right':  $newtext=substr($newtext,2).($is_out_of_stock ? ' - '.TEXT_OUT_OF_STOCK : '');
 406                               break;
 407                default:       $newtext=substr($newtext,2);
 408                               break;
 409              }
 410              $combinations[] = array('comb'=>$newcomb, 'id'=>substr($newid,1), 'text'=>$newtext);
 411              if ($newisselected) $selected_combination = sizeof($combinations)-1;
 412            }
 413          }
 414        }
 415      }
 416  
 417  
 418  /*

 419      Method: _draw_js_stock_array

 420    

 421      Draw a Javascript array containing the given attribute combinations.

 422      Generally used to draw array of in-stock combinations for Javascript out of stock

 423      validation and messaging.

 424    

 425      Parameters:

 426    

 427        $combinations        array   Array of combinations to build the Javascript array for.

 428                                     Array must be of the form returned by _build_attributes_combinations

 429                                     Usually this array only contains in-stock combinations.

 430    

 431      Returns:

 432    

 433        string:                 Javacript array definition.  Excludes the "var xxx=" and terminating ";".  Form is:

 434                                {optval1:{optval2:{optval3:1,optval3:1}, optval2:{optval3:1}}, optval1:{optval2:{optval3:1}}}

 435                                For example if there are 3 options and the instock value combinations are:

 436                                  opt1   opt2   opt3

 437                                    1      5      4

 438                                    1      5      8

 439                                    1     10      4

 440                                    3      5      8

 441                                The string returned would be

 442                                  {1:{5:{4:1,8:1}, 10:{4:1}}, 3:{5:{8:1}}}

 443    

 444  */
 445      function _draw_js_stock_array($combinations) {
 446        if (!((isset($combinations)) && (is_array($combinations)) && (sizeof($combinations) > 0))){
 447          return '{}';
 448        }
 449        $out='';
 450        foreach ($combinations[0]['comb'] as $oid=>$ovid) {
 451          $out.='{'.$ovid.':';
 452          $opts[]=$oid;
 453        }
 454        $out.='1';
 455        
 456        for ($combindex=1; $combindex<sizeof($combinations); $combindex++) {
 457          $comb=$combinations[$combindex]['comb'];
 458          for ($i=0; $i<sizeof($opts)-1; $i++) {
 459            if ($comb[$opts[$i]]!=$combinations[$combindex-1]['comb'][$opts[$i]]) break;
 460          }
 461          $out.=str_repeat('}',sizeof($opts)-1-$i).',';
 462          if ($i<sizeof($opts)-1) {
 463            for ($j=$i; $j<sizeof($opts)-1; $j++)
 464              $out.=$comb[$opts[$j]].':{';
 465          }
 466          $out.=$comb[$opts[sizeof($opts)-1]].':1';
 467        }
 468        $out.=str_repeat('}',sizeof($opts));
 469        
 470        return $out;
 471      }
 472  
 473    }
 474  ?>


Generated: Fri Jan 1 13:43:16 2010 Cross-referenced by PHPXref 0.7