[ Index ]

PHP Cross Reference of osCMax 2.0.4

title

Body

[close]

/admin/attributeManager/classes/ -> attributeManager.class.php (source)

   1  <?php
   2  /*
   3    $Id: attributeManager.class.php,v 1.0 21/02/06 Sam West$
   4  
   5    osCommerce, Open Source E-Commerce Solutions
   6    http://www.oscommerce.com
   7  
   8    Released under the GNU General Public License
   9    
  10    Copyright  2006 Kangaroo Partners
  11    http://kangaroopartners.com
  12    [email protected]
  13  */
  14  
  15  
  16  /*
  17  
  18  interface attributeManagerInterface {
  19      function getAllProductOptionsAndValues();
  20      function removeOptionFromProduct();
  21      function addAttributeToProduct();
  22  }
  23  */
  24  
  25  /*abstract*/ class attributeManager /*implements attributeManagerInterface*/ {
  26      
  27      /**
  28       * Holds all of the options in the database
  29       * @access private
  30       */
  31      var $arrAllOptions = array();
  32      
  33      /**
  34       * Holds all of the option values in the database
  35       * @access private
  36       */
  37      var $arrAllOptionValues = array();
  38      
  39      /**
  40       * Holds all of the options and their values where they are releated to each other
  41       * @access private
  42       */
  43      var $arrAllOptionsAndValues = array();
  44      
  45      /**
  46       * Holds all of the current products options and option values
  47       * @access protected
  48       */
  49      var $arrAllProductOptionsAndValues = array();
  50      
  51      /**
  52       * Currently selected language id
  53       * @todo make multilingual
  54       * @access private
  55       */
  56      var $intLanguageId;
  57      
  58      /**
  59       * Page actions
  60       * @var $arrPageActions Array
  61       * @access private
  62       */
  63      var $arrPageActions = array();
  64      
  65      /**
  66       * All templates
  67       * @var $arrAllTemplatesAndAttributes Array
  68       */
  69      var $arrAllTemplatesAndAttributes = array();
  70      
  71      /**
  72       * __construct()- 
  73       * Sets up page actions and sets the interface language
  74       * @access protected
  75       * @author Sam West aka Nimmit - [email protected]
  76       * @return void
  77       */
  78  	function attributeManager() {
  79          
  80          $this->setInterfaceLanguage();
  81          $this->setTemplateOrder();
  82          
  83          $this->registerPageAction('addOption','addOption');
  84          $this->registerPageAction('addOptionValue','addOptionValue');
  85          $this->registerPageAction('updateNewOptionValue','updateNewOptionValue');
  86  
  87          $this->registerPageAction('setTemplateOrder','setTemplateOrder');
  88  
  89          $this->registerPageAction('deleteTemplate','deleteTemplate');
  90          $this->registerPageAction('saveTemplate','saveTemplate');
  91          $this->registerPageAction('renameTemplate','renameTemplate');
  92          $this->registerPageAction('loadTemplate','loadTemplate');
  93          $this->registerPageAction('setInterfaceLanguage','setInterfaceLanguage');
  94      }
  95      
  96      
  97      
  98      
  99      //---------------------------------------------------------------------------------------------- core
 100      
 101      /**
 102       * Sets the interface language
 103       * @param $get $_GET (optional)
 104       * @access public
 105       * @author Sam West aka Nimmit - [email protected]
 106       * @return void
 107       */
 108  	function setInterfaceLanguage($get=array()) {
 109          // setting new interface language
 110          if(count($get) > 0) {
 111              if(is_numeric($get['language_id'])) {
 112                  amSessionRegister(AM_SESSION_CURRENT_LANG_VAR_NAME);
 113                  amSetSessionVariable(AM_SESSION_CURRENT_LANG_VAR_NAME,$get['language_id']);
 114                  $this->intLanguageId = $get['language_id'];
 115              }
 116          }
 117          // not called from the page. ie costruct
 118          else {
 119              $langId = amGetSesssionVariable(AM_SESSION_CURRENT_LANG_VAR_NAME);
 120              if(false !== $langId)
 121                  $this->intLanguageId = $langId;
 122              else 
 123                  $this->intLanguageId = AM_DEFAULT_LANGUAGE_ID;
 124          }
 125      }
 126      
 127      /**
 128       * Returns the currently selected language id
 129       * @access public
 130       * @author Sam West aka Nimmit - [email protected]
 131       * @return int language id
 132       */
 133  	function getSelectedLanaguage() {
 134          return $this->intLanguageId;
 135      }
 136      
 137      /**
 138       * Sets Template order
 139       * @param $get $_GET (optional)
 140       * @access public
 141       * @author Tomasz Iwanow aka TomaszBG - [email protected]
 142       * @return void
 143       */
 144  	function setTemplateOrder($get=array()){
 145          if(count($get) > 0) {
 146              if(isset($get['templateOrder'])) {
 147                  amSessionRegister(AM_SESSION_CURRENT_TEMPLATE_ORDER);
 148                  amSetSessionVariable(AM_SESSION_CURRENT_TEMPLATE_ORDER,$get['templateOrder']);
 149                  $this->strTemplateOrder = $get['templateOrder'];
 150              }
 151          }else{
 152              $templateOrder = amGetSesssionVariable(AM_SESSION_CURRENT_TEMPLATE_ORDER);
 153              if(false !== $templateOrder)
 154                  $this->strTemplateOrder = $templateOrder;
 155              else 
 156                  $this->strTemplateOrder = AM_DEFAULT_TEMPLATE_ORDER;
 157          }
 158      }
 159      
 160      /**
 161       * Returns the currently selected Template order
 162       * @access public
 163       * @author Tomasz Iwanow aka TomaszBG - [email protected]
 164       * @return str Template order
 165       */
 166  	function getTemplateOrder() {
 167          return $this->strTemplateOrder;
 168      }
 169      
 170      /**
 171       * Gets all of the options in the database
 172       * @access protected
 173       * @author Sam West aka Nimmit - [email protected]
 174       * @return array all options
 175       */
 176  	function getAllOptions() {
 177          if(0 === count($this->arrAllOptions)) {
 178              $queryString = "select * from ".TABLE_PRODUCTS_OPTIONS." where language_id='".amDB::input($this->intLanguageId)."' order by ";
 179              $queryString .= !AM_USE_SORT_ORDER ?  "products_options_name" : AM_FIELD_OPTION_SORT_ORDER;
 180              $query = amDB::query($queryString);
 181  
 182              while($res = amDB::fetchArray($query))
 183                  $this->arrAllOptions[$res['products_options_id']] = $res['products_options_name'];
 184          }
 185          
 186          return $this->arrAllOptions;
 187      }
 188      
 189      /**
 190       * Gets all of the option values in the database
 191       * @access protected
 192       * @author Sam West aka Nimmit - [email protected]
 193       * @return array all option values
 194       */
 195  	function getAllOptionValues() {
 196          if(0 === count($this->arrAllOptionValues)) {
 197              $query = amDB::query("select * from ".TABLE_PRODUCTS_OPTIONS_VALUES." where language_id='".amDB::input($this->intLanguageId)."'");
 198              while($res = amDB::fetchArray($query))
 199                  $this->arrAllOptionValues[$res['products_options_values_id']] = $res['products_options_values_name'];
 200          }
 201          return $this->arrAllOptionValues;
 202      }
 203      
 204      /**
 205       * Returns an array of options with their related option values
 206       * @access protected
 207       * @author Sam West aka Nimmit - [email protected]
 208       * @return array All options and option values
 209       */
 210  	function getAllOptionsAndValues() {
 211          if(0 === count($this->arrAllOptionsAndValues)){
 212          
 213              $allOptions = $this->getAllOptions();
 214              $allOptionValues = $this->getAllOptionValues();
 215          
 216              $query = amDB::query("select * from ".TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS);
 217          
 218              $optionsId = null;
 219              while($res = amDB::fetchArray($query)) {
 220                  if($res['products_options_id'] != $optionsId) {
 221                      $optionsId = $res['products_options_id'];
 222                      $this->arrAllOptionsAndValues[$optionsId]['name'] = $allOptions[$optionsId];
 223                  }
 224                  $this->arrAllOptionsAndValues[$optionsId]['values'][$res['products_options_values_id']] = $allOptionValues[$res['products_options_values_id']];
 225              }
 226              
 227              // add any options that are not yet assigned to the tpovtpo table
 228              foreach($allOptions as $optionId => $option)
 229                  if(!array_key_exists($optionId, $this->arrAllOptionsAndValues)) 
 230                      $this->arrAllOptionsAndValues[$optionId]['name'] = $allOptions[$optionId];
 231                  
 232          }
 233          
 234          return $this->arrAllOptionsAndValues;
 235      }
 236      
 237      //---------------------------------------------------------------------------------------------- page actions
 238      
 239      /**
 240       * Adds a new option to the database
 241       * @access public
 242       * @author Sam West aka Nimmit - [email protected]
 243       * @param $get $_GET
 244       * @return array global variables to be set
 245       */
 246  	function addOption($get) {
 247          
 248          $this->getAndPrepare('options', $get, $options);
 249          if (AM_USE_SORT_ORDER) {
 250              $this->getAndPrepare('optionSort', $get, $optionSort);
 251          }
 252          if (AM_USE_QT_PRO) {
 253              $this->getAndPrepare('optionTrack', $get, $optionTrack);
 254          }
 255          
 256          if(strpos($options,'|'))
 257              $arrOptions = explode('|', $options);
 258          else 
 259              $arrOptions[] = $options;
 260          
 261          $id = amDB::getNextAutoValue(TABLE_PRODUCTS_OPTIONS,'products_options_id');
 262          
 263          foreach($arrOptions as $option) {
 264              
 265              list($langId,$name) = explode(':',$option);
 266  
 267        if ($id == 0){
 268          $id = 1;
 269        }
 270                    
 271              $arrData = array (
 272                  'products_options_id' => $id,
 273                  'language_id' => amDB::input($langId), 
 274                  'products_options_name' => amDB::input($name)
 275              );
 276              if (AM_USE_SORT_ORDER) {
 277                  $arrData[AM_FIELD_OPTION_SORT_ORDER] = amDB::input($optionSort);
 278              }
 279              if (AM_USE_QT_PRO) {
 280                  $arrData['products_options_track_stock'] = amDB::input($optionTrack);
 281              }
 282      
 283              amDB::perform(TABLE_PRODUCTS_OPTIONS,$arrData);
 284          }
 285          
 286          return array('selectedOption' => $id);
 287      }
 288      
 289      /**
 290       * Adds a new option values to the database
 291       * @access public
 292       * @author Sam West aka Nimmit - [email protected] - [email protected]
 293       * @param $get $_GET
 294       * @return array global variables to be set
 295       */
 296  	function addOptionValue($get) {
 297          
 298          $this->getAndPrepare('option_id', $get, $optionId);
 299          $this->getAndPrepare('option_values', $get, $optionValues);
 300          
 301          if(strpos($optionValues,'|'))
 302              $arrOptionValues = explode('|', $optionValues);
 303          else 
 304              $arrOptionValues[] = $optionValues;
 305          
 306          $newId = amDB::getNextAutoValue(TABLE_PRODUCTS_OPTIONS_VALUES,'products_options_values_id');
 307          
 308          foreach($arrOptionValues as $optionValue) {
 309              
 310              list($langId,$name) = explode(':',$optionValue);
 311              
 312              if ($newId == 0) {
 313                $newId = 1;
 314              }
 315              
 316              $ovData = array (
 317                  'products_options_values_id' => $newId,
 318                  'language_id' => amDB::input($langId), 
 319                  'products_options_values_name' => amDB::input($name)
 320              );
 321              
 322              amDB::perform(TABLE_PRODUCTS_OPTIONS_VALUES,$ovData);
 323          }
 324          
 325          $ov2oData = array(
 326                  'products_options_id' => $optionId,
 327                  'products_options_values_id' => $newId
 328              );
 329          
 330          amDB::perform(TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS,$ov2oData);
 331          
 332          return array('selectedOption' => $optionId, 'selectedOptionValue' => $newId);
 333      }
 334      
 335      /**
 336       * Sets the selected box id
 337       * @param $get $_GET
 338       * @return array global variables to be set
 339       */
 340  	function updateNewOptionValue($get) {
 341          $this->getAndPrepare('option_id', $get, $selectedOption);
 342          return array('selectedOption' => $selectedOption);
 343      }
 344      
 345      //---------------------------------------------------------------------------------------------- Template page actions
 346      
 347      /**
 348       * Takes a variable and prepares it for db
 349       * @access protected
 350       * @author Sam West aka Nimmit - [email protected]
 351       * @param $strIndex string array index
 352       * @param $array array array
 353       * @param Variable to set PASSED BY REF
 354       * @return void
 355       */
 356  	function getAndPrepare($strIndex,$array, &$variable) {
 357          $variable = amDB::input($array[$strIndex]);
 358      }
 359      
 360      /**
 361       * get all templates and their options and option values
 362       * @access protected
 363       * @author Sam West aka Nimmit - [email protected]
 364       * @return array 
 365       */
 366  	function getAllTemplatesAndAttributes() {
 367          if(0 === count($this->arrAllTemplatesAndAttributes)) {
 368          
 369          
 370              // changes by mytool
 371              $query = "select * from ".AM_TABLE_TEMPLATES." at left join ".AM_TABLE_ATTRIBUTES_TO_TEMPLATES." aa2t using(template_id) order by at.template_id";
 372              if (AM_USE_SORT_ORDER) {
 373                  $query .= ", aa2t.". AM_FIELD_OPTION_VALUE_SORT_ORDER ;
 374              }
 375              
 376              $ref = amDB::getAll($query);//, aa2t.options_id , aa2t.option_values_id
 377              // EOF changes by mytool
 378              
 379              $templateID = null;
 380              foreach($ref as $res) {
 381                  if($templateID != $res['template_id']) {
 382                      $templateID = $res['template_id'];
 383                      $this->arrAllTemplatesAndAttributes[$templateID]['name'] = $res['template_name'];
 384                  }
 385                  $this->arrAllTemplatesAndAttributes[$templateID][$res['options_id']][] = $res['option_values_id'];
 386  
 387                  if (AM_USE_SORT_ORDER) {
 388                        $this->arrAllTemplatesAndAttributes[$templateID]['sortOrder'][$res['option_values_id']] = $res[AM_FIELD_OPTION_VALUE_SORT_ORDER];
 389                    }
 390                    // Added by Red Earth Design, Inc. to populate price and prefix
 391                    $this->arrAllTemplatesAndAttributes[$templateID]['price_prefix'][$res['option_values_id']] = $res['price_prefix'];
 392                    $this->arrAllTemplatesAndAttributes[$templateID]['options_values_price'][$res['option_values_id']] = $res['options_values_price'];
 393              }
 394          }
 395          return $this->arrAllTemplatesAndAttributes;
 396      }
 397  
 398      /**
 399       * Build the templates drop down box
 400       * @access public
 401       * @author Sam West aka Nimmit - [email protected]
 402       * @return array 
 403       */
 404  	function buildAllTemplatesDropDown($order_by='id') {
 405          $allTemplates = $this->getAllTemplatesAndAttributes();
 406          $returnArray = array(array('id' => '0', 'text' => AM_AJAX_TEMPLATES));
 407  
 408          foreach ($allTemplates as $templateID => $values) {
 409              $returnArray[] = array('id' => $templateID, 'text' => $values['name']);
 410          }
 411          
 412          // Sort ABC if required
 413          if($order_by=='abc'){
 414              for($z1=0;$z1<count($returnArray);$z1++){
 415                  $last_z2=-1;
 416                  foreach($returnArray as $z2 => $values) {
 417                      if(($last_z2>0)){
 418                          if(strcmp($returnArray[$last_z2]['text'],$returnArray[$z2]['text'])>0){
 419                              $tempArr=$returnArray[$last_z2];
 420                              $returnArray[$last_z2]=$returnArray[$z2];
 421                              $returnArray[$z2]=$tempArr;
 422                              $fch=true;
 423                          }
 424                      }
 425                      $last_z2=$z2;
 426                  }
 427              }
 428          }
 429          
 430          return $returnArray;
 431      }
 432      
 433      /**
 434       * Deletes a given template
 435       * @param get $_GET
 436       * @author Sam West aka Nimmit - [email protected]
 437       * @return void
 438       */
 439  	function deleteTemplate($get) {
 440          $this->getAndPrepare('template_id',$get,$templateId);
 441          amDB::query("delete from ".AM_TABLE_TEMPLATES." where template_id = '$templateId'");
 442          amDB::query("delete from ".AM_TABLE_ATTRIBUTES_TO_TEMPLATES." where template_id = '$templateId'");
 443      }
 444      
 445      /**
 446       * Saves the current products attributes as a template
 447       * @param $get $_GET
 448       * @author Sam West aka Nimmit - [email protected]
 449       * @return array selected template
 450       */
 451  	function saveTemplate($get) {
 452          $this->getAndPrepare('template_name', $get, $templateName);
 453          $this->getAndPrepare('new_template_id', $get, $newTemplateId);
 454          
 455          
 456          
 457          $allProdOptionsAndValues = $this->getAllProductOptionsAndValues();
 458          
 459          if(strpos($templateName,':')) {
 460              $templateNameBits = explode(':',$templateName);
 461              $templateName = $templateNameBits[1];
 462          }
 463          
 464          if(0 !== count($allProdOptionsAndValues)) {
 465              if(0 != $newTemplateId) {
 466                  amDB::query("delete from ".AM_TABLE_ATTRIBUTES_TO_TEMPLATES." where template_id='$newTemplateId'");
 467              }
 468              else {
 469                  $data = array(
 470                      'template_name' => $templateName
 471                  );
 472                  amDB::perform(AM_TABLE_TEMPLATES,$data);
 473                  $newTemplateId = amDB::insertId();
 474              }
 475                //echo '<br><br>Array ALLPRODOPTSAVALS:: <br><br>';
 476              //print_r($allProdOptionsAndValues);
 477  
 478              foreach($allProdOptionsAndValues as $optionId => $values) {
 479                  if(is_array($values['values'])) {
 480                      foreach ($values['values'] as $optionValuesId => $allOptionValues){
 481                          if (!AM_USE_SORT_ORDER) {
 482                                $data = array(
 483                                    'template_id' => $newTemplateId,
 484                                    'options_id' => $optionId,
 485                                    'option_values_id' => $optionValuesId,
 486                                    'price_prefix' => $values['values'][$optionValuesId]['prefix'],
 487                                    'options_values_price' => $values['values'][$optionValuesId]['price']
 488                                );
 489                            } else {
 490                                $data = array(
 491                                    'template_id' => $newTemplateId,
 492                                    'options_id' => $optionId,
 493                                    'option_values_id' => $optionValuesId,
 494                                    'price_prefix' => $values['values'][$optionValuesId]['prefix'],
 495                                    'options_values_price' => $values['values'][$optionValuesId]['price'],
 496                                    'products_options_sort_order' => $values['values'][$optionValuesId]['sortOrder']
 497                                );
 498                            }
 499                            //echo '<br><br>Array DATA:: <br><br>';
 500                            //print_r($data);
 501                          amDB::perform(AM_TABLE_ATTRIBUTES_TO_TEMPLATES,$data);
 502                      }
 503                  }
 504              }
 505          }
 506          return array('selectedTemplate' => $newTemplateId);
 507      }
 508      
 509      /**
 510       * renames the specified template
 511       * @param $get $_GET
 512       * @author Sam West aka Nimmit - [email protected]
 513       * @return array renamed template
 514       */
 515  	function renameTemplate($get) {
 516          
 517          $this->getAndPrepare('template_name', $get, $templateName);
 518          $this->getAndPrepare('template_id', $get, $templateId);
 519          
 520          if(strpos($templateName,':')) {
 521              $templateNameBits = explode(':',$templateName);
 522              $templateName = $templateNameBits[1];
 523          }
 524          $data = array(
 525              'template_name' => $templateName
 526          );
 527          
 528          amDB::perform(AM_TABLE_TEMPLATES,$data,'update',"template_id = '$templateId'");
 529          
 530          return array('selectedTemplate' => $templateId);
 531      }
 532      
 533      /**
 534       * Loads the selected template
 535       * @param $get $_GET
 536       * @author Sam West aka Nimmit - [email protected]
 537       * @return array selected template
 538       */
 539  	function loadTemplate($get) {
 540          
 541          $this->getAndPrepare('template_id', $get, $templateId);
 542          
 543          $allProductsOptionsAndValues = $this->getAllProductOptionsAndValues();
 544          
 545          // used for checking the option still actualy exists in the database
 546          $allOptions = array_keys($this->getAllOptions());
 547          $allOptionValues = array_keys($this->getAllOptionValues());
 548          
 549          // first delete all the products existing options
 550          foreach($allProductsOptionsAndValues as $optionId => $dontNeed)
 551              $this->removeOptionFromProduct(array('option_id' => $optionId));
 552          
 553          // now add the ones in the template
 554          $allTemplatesAttributes = $this->getAllTemplatesAndAttributes();
 555          //echo '<br><br>Array ALLTEMPANDATTRIBS:: <br><br>';
 556            //print_r($allTemplatesAttributes);
 557  
 558          $actionTaken = false;
 559          
 560          if(is_array($allTemplatesAttributes[$templateId])) {
 561              foreach ($allTemplatesAttributes[$templateId] as $optionsId => $values) {
 562                  // check that the option id in the template is still in the database 
 563                  if(in_array($optionsId,$allOptions)) {
 564                      if(is_array($values)) {
 565                          foreach($values as $optionValuesId) {
 566                                            
 567                              // check that the option values id still exists in the database
 568                              if(in_array($optionValuesId,$allOptionValues)) {
 569                                if (!AM_USE_SORT_ORDER && !AM_USE_QT_PRO) {
 570                                    $this->addAttributeToProduct(
 571                                        array(
 572                                            'option_id' => $optionsId,
 573                                            'option_value_id' => $optionValuesId,
 574                                            'price' => $allTemplatesAttributes[$templateId]['options_values_price'][$optionValuesId],
 575                                            'prefix' => $allTemplatesAttributes[$templateId]['price_prefix'][$optionValuesId]
 576                                        )
 577                                    );
 578                                }
 579                                  if (AM_USE_SORT_ORDER && !AM_USE_QT_PRO) {
 580  //                                  echo 'TempID '.$templateId . ' OPTvalID ' . $optionValuesId . ' ';
 581                                      $this->addAttributeToProduct(
 582                                        array(
 583                                            'option_id' => $optionsId,
 584                                            'option_value_id' => $optionValuesId,
 585                                            'price' => $allTemplatesAttributes[$templateId]['options_values_price'][$optionValuesId],
 586                                            'prefix' => $allTemplatesAttributes[$templateId]['price_prefix'][$optionValuesId],
 587                                          'sortOrder' => $allTemplatesAttributes[$templateId]['sortOrder'][$optionValuesId]
 588                                        )
 589                                      );    
 590                                  }
 591                                  if (AM_USE_QT_PRO && !AM_USE_SORT_ORDER) {
 592                                      $this->addAttributeToProduct(
 593                                        array(
 594                                            'option_id' => $optionsId,
 595                                            'option_value_id' => $optionValuesId,
 596                                            'price' => $allTemplatesAttributes[$templateId]['options_values_price'][$optionValuesId],
 597                                            'prefix' => $allTemplatesAttributes[$templateId]['price_prefix'][$optionValuesId],
 598                                          'stockTracking' => '0'
 599                                        )
 600                                      );    
 601                                  }
 602                                  if (AM_USE_QT_PRO && AM_USE_SORT_ORDER) {
 603                                      $this->addAttributeToProduct(
 604                                        array(
 605                                            'option_id' => $optionsId,
 606                                            'option_value_id' => $optionValuesId,
 607                                            'price' => $allTemplatesAttributes[$templateId]['options_values_price'][$optionValuesId],
 608                                            'prefix' => $allTemplatesAttributes[$templateId]['price_prefix'][$optionValuesId],
 609                                          'sortOrder' => $allTemplatesAttributes[$templateId]['sortOrder'][$optionValuesId],
 610                                          'stockTracking' => '0'
 611                                        )
 612                                      );    
 613                                  }
 614                                  $actionTaken = true;
 615                              }
 616                              else {
 617                                  // the option value no longer exists in the databse, delete if from all templates
 618                                  amDB::query("delete from ".AM_TABLE_ATTRIBUTES_TO_TEMPLATES." where  option_values_id = '$optionValuesId'");
 619                              }
 620                          }
 621                      }
 622                  }
 623                  else {
 624                      // the option no longer exists in the databse, delete it from all templates
 625                      amDB::query("delete from ".AM_TABLE_ATTRIBUTES_TO_TEMPLATES." where  options_id = '$optionsId'");
 626                  }
 627              }
 628          }
 629          // if something has been loaded reset the options and values
 630          if($actionTaken)
 631              $this->getAllProductOptionsAndValues(true);
 632          // otherwise nothing in the template exists anymore so just restore the original values, no harm done
 633          else
 634              $this->arrAllProductOptionsAndValues = $allProductsOptionsAndValues;
 635          
 636          return array('selectedTemplate' => $templateId);
 637      }
 638      
 639      //---------------------------------------------------------------------------------------------- Drop downs
 640      
 641      /**
 642       * takes an array of key => value and formats them for the tep_draw_pull_down function
 643       * @access private
 644       * @author Sam West aka Nimmit - [email protected]
 645       * @param $array Array key value pair to be formated
 646       * @return array global variables to be set
 647       */
 648  	function formatArrayForDropDown($array) {
 649          $arrReturn = array();
 650          foreach($array as $key => $value)
 651              $arrReturn[] = array('id' => $key, 'text' => $value);
 652          if(0 === count($arrReturn))
 653              return array(array('id' => '0', 'text' => '----'));
 654              
 655          return $arrReturn;
 656      }
 657      
 658      /**
 659       * Builds an array for a drop down of available options
 660       * @access public
 661       * @author Sam West aka Nimmit - [email protected]
 662       * @param $subtract bol - if true it will subtract the options that are already assigned to the product
 663       * @return array formated for the osc dropdown box function array(array('id'=>$key,'text'=>$value))
 664       */
 665  	function buildOptionDropDown($subtract = true) {
 666          
 667          $allOptionsAndValues = $this->getAllOptionsAndValues();
 668          $allProductsOptionsAndValues = $this->getAllProductOptionsAndValues();
 669          
 670          $returnArray = array();
 671          
 672          foreach($allOptionsAndValues as $optionId => $optionValues)
 673              $returnArray[$optionId] = $optionValues['name'];
 674          
 675          // remove any already assigned
 676          if(true === $subtract) {
 677              
 678              if(0 !== count($allProductsOptionsAndValues)){
 679                  // get all of the option ids from the return array that arn't already assigned to the product
 680                  $nonAssignedIds = array_diff(array_keys($returnArray), array_keys($this->getAllProductOptionsAndValues()));
 681          
 682                  $tRetrurnArray = $returnArray;
 683              
 684                  $returnArray = array();
 685              
 686                  // rebuild the array
 687                  if(is_array($nonAssignedIds)) 
 688                      foreach($nonAssignedIds as $id)
 689                          $returnArray[$id] = $tRetrurnArray[$id];
 690              }
 691          }
 692          
 693          /**
 694           * Sort the keys of the array alpha
 695           * @todo make it case insensitive
 696           */
 697          if(1 == AM_DEFAULT_SORT_ORDER)
 698              asort($returnArray);
 699          
 700          return $this->formatArrayForDropDown($returnArray);
 701      }
 702      
 703      /**
 704       * Builds an array for a drop down of available option values
 705       * @access public
 706       * @author Sam West aka Nimmit - [email protected]
 707       * @param $optionId int - if defined, will limit the option values to only ones that are below an option. Otherwise it will return all
 708       * @param $subtract bol - if true it will subtract option values that are already assigned to this product with this option
 709       * @return array formated for the osc dropdown box function array(array('id'=>$key,'text'=>$value))
 710       */
 711  	function buildOptionValueDropDown($optionId = null, $subtract = true) {
 712          
 713          $allOptionsAndValues = $this->getAllOptionsAndValues();
 714          
 715          $returnArray = array();
 716          
 717          // get all the values
 718          if(null === $optionId) {
 719              foreach($allOptionsAndValues as $option) 
 720                  if(is_array($option['values'])) 
 721                      foreach($option['values'] as $optionValueId => $optionValueText)
 722                          $returnArray[$optionValueId] = $optionValueText;
 723          }
 724          // just get the values for the specified option id
 725          else {
 726              if(array_key_exists($optionId,$allOptionsAndValues))
 727                  if(is_array($allOptionsAndValues[$optionId]['values']))
 728                      foreach($allOptionsAndValues[$optionId]['values'] as $optionValueId => $optionValueText) 
 729                          $returnArray[$optionValueId] = $optionValueText;
 730          }
 731          
 732          // get rid of any already specified
 733          if(true === $subtract) {
 734              
 735              $allProductsOptionsAndValues = $this->getAllProductOptionsAndValues();
 736              
 737              // get all of the values
 738              if(null === $optionId) {
 739                  $tAll = array();
 740                  
 741                  foreach($allProductsOptionsAndValues as $optionId => $details)
 742                      if(is_array($details['values']))
 743                          foreach($details['values'] as $optionValueId => $optionValueText)
 744                              if(!array_key_exists($optionValueId,$tAll)) // stop duplicates - there shouldn't be any, but you never know
 745                                  $tAll[$optionValueId] = $optionValueText;
 746                              
 747                  $allProductsOptionsAndValues = $tAll;
 748              }
 749              // if an option id is specified only return the values for that option id to compare
 750              else {
 751                  $allProductsOptionsAndValues = $allProductsOptionsAndValues[$optionId]['values'];
 752              }
 753              
 754              // make sure that the product actually has one of the values for the current option to subtract, if not do 
 755              if(0 !== count($allProductsOptionsAndValues)){
 756              
 757                  // get all of the option value ids from the return array that arn't already assigned to the product
 758                  $nonAssignedIds = array_diff(array_keys($returnArray),array_keys($allProductsOptionsAndValues));
 759              
 760                  $tRetrurnArray = $returnArray;
 761              
 762                  $returnArray = array();
 763              
 764                  // rebuild the array
 765                  if(is_array($nonAssignedIds)) 
 766                      foreach($nonAssignedIds as $id)
 767                          $returnArray[$id] = $tRetrurnArray[$id];
 768              }
 769          }
 770          
 771          /**
 772           * Sort the keys of the array alpha
 773           * @todo make it case insensitive
 774           */
 775          if(1 == AM_DEFAULT_SORT_ORDER)
 776              asort($returnArray);
 777  
 778          return $this->formatArrayForDropDown($returnArray);
 779      }
 780      
 781      //---------------------------------------------------------------------------------------------- page action execution
 782      
 783      /**
 784       * Registers a page action
 785       * @author Sam West aka Nimmit - [email protected]
 786       * @param $strAction string action
 787       * @param $strFunction string function name
 788       * @return void
 789       */
 790  	function registerPageAction($strAction,$strFunction) {
 791          $this->arrPageActions[$strAction] = $strFunction;
 792      }
 793      
 794      /**
 795       * Unregisters a page action
 796       * @author Sam West aka Nimmit - [email protected]
 797       * @param $strAction string page action to be unregistered
 798       * @return void
 799       */
 800  	function unregisterPageAction($strAction) {
 801          unset($this->arrPageActions[$strAction]);
 802      }
 803      
 804      /**
 805       * Executes a page action
 806       * @access public
 807       * @author Sam West aka Nimmit - [email protected]
 808       * @param $get $_GET
 809       * @return array global variables to be set
 810       */
 811  	function executePageAction($get) {
 812          $results = array();
 813          if(array_key_exists(AM_ACTION_GET_VARIABLE,$get)) {
 814              $actionKey = $get[AM_ACTION_GET_VARIABLE];
 815              if(array_key_exists($actionKey,$this->arrPageActions)){
 816                  $functionName = $this->arrPageActions[$actionKey];
 817                  if(method_exists($this,$functionName)) {
 818                      $results = $this->$functionName($get);
 819                  }
 820              }
 821          }
 822          return $results;
 823      }
 824      
 825      //---------------------------------------------------------------------------------------------- misc
 826      
 827      /**
 828       * Nothing todo with the script just outputs stuff to browser used for debelopment
 829       * @access public
 830       * @author Sam West aka Nimmit - [email protected]
 831       * @return void
 832       */
 833  	function debugOutput($ent) {
 834          echo (is_array($ent) || is_object($ent)) ? '<pre style="text-align:left">'.print_r($ent, true).'</pre>' : $ent;
 835      }
 836      
 837      
 838      
 839  }
 840  
 841  
 842  
 843  
 844  
 845  
 846  ?>


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