/**
  * get all templates and their options and option values
  * @access protected
  * @author Sam West aka Nimmit - osc@kangaroopartners.com
  * @return array 
  */
 function getAllTemplatesAndAttributes()
 {
     if (0 === count($this->arrAllTemplatesAndAttributes)) {
         // changes by mytool
         $query = "select * from " . AM_TABLE_TEMPLATES . " at left join " . AM_TABLE_ATTRIBUTES_TO_TEMPLATES . " aa2t using(template_id) order by at.template_id";
         if (AM_USE_SORT_ORDER) {
             $query .= ", aa2t." . AM_FIELD_OPTION_VALUE_SORT_ORDER;
         }
         $ref = amDB::getAll($query);
         //, aa2t.options_id , aa2t.option_values_id
         // EOF changes by mytool
         $templateID = null;
         foreach ($ref as $res) {
             if ($templateID != $res['template_id']) {
                 $templateID = $res['template_id'];
                 $this->arrAllTemplatesAndAttributes[$templateID]['name'] = $res['template_name'];
             }
             $this->arrAllTemplatesAndAttributes[$templateID][$res['options_id']][] = $res['option_values_id'];
             if (AM_USE_SORT_ORDER) {
                 $this->arrAllTemplatesAndAttributes[$templateID]['sortOrder'][$res['option_values_id']] = $res[AM_FIELD_OPTION_VALUE_SORT_ORDER];
             }
             // Added by Red Earth Design, Inc. to populate price and prefix
             $this->arrAllTemplatesAndAttributes[$templateID]['price_prefix'][$res['option_values_id']] = $res['price_prefix'];
             $this->arrAllTemplatesAndAttributes[$templateID]['options_values_price'][$res['option_values_id']] = $res['options_values_price'];
             if (AM_USE_MPW) {
                 $this->arrAllTemplatesAndAttributes[$templateID]['weight_prefix'][$res['option_values_id']] = $res['price_weight'];
                 $this->arrAllTemplatesAndAttributes[$templateID]['options_values_weight'][$res['option_values_id']] = $res['options_values_weight'];
             }
         }
     }
     return $this->arrAllTemplatesAndAttributes;
 }
 function getSortedProductAttributes($sortfield)
 {
     $sortedArray = array();
     $queryString = "select products_attributes_id, options_id, sortorder" . " from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id=" . $this->intPID;
     /*		if( $optionsID > -1){			
     			$queryString .=	" AND options_id=".$optionsID;
     		}
     */
     $queryString .= " ORDER BY " . $sortfield . " asc, options_id asc";
     $result = amDB::getAll($queryString);
     //$i = (int)$result[0][$sortfield];
     $i = 0;
     while (list($key, $val) = each($result)) {
         // set the sorting new
         $val[AM_FIELD_OPTION_VALUE_SORT_ORDER] = $i;
         $sortedArray[$i] = $val;
         $i++;
     }
     return $sortedArray;
 }
 function installSortOrder()
 {
     if ($this->getValue('AM_USE_SORT_ORDER') && !amSessionIsRegistered($this->getValue('AM_SESSION_SORT_ORDER_INSTALL_CHECKED'))) {
         // check that the fields are in the attributes table
         $attributeFields = amDB::query("SHOW COLUMNS FROM " . TABLE_PRODUCTS_ATTRIBUTES);
         while ($field = amDB::fetchArray($attributeFields)) {
             $attrfields[] = $field['Field'];
         }
         $attributeFields = amDB::query("SHOW COLUMNS FROM " . TABLE_PRODUCTS_OPTIONS);
         while ($field = amDB::fetchArray($attributeFields)) {
             $optionsFields[] = $field['Field'];
         }
         $attributeFields = amDB::query("SHOW COLUMNS FROM " . $this->getValue('AM_TABLE_ATTRIBUTES_TO_TEMPLATES'));
         while ($field = amDB::fetchArray($attributeFields)) {
             $soptionsFields[] = $field['Field'];
         }
         $oInstalled = in_array($this->getValue('AM_FIELD_OPTION_SORT_ORDER'), $optionsFields);
         $ovInstalled = in_array($this->getValue('AM_FIELD_OPTION_VALUE_SORT_ORDER'), $attrfields);
         $soInstalled = in_array($this->getValue('AM_FIELD_OPTION_SORT_ORDER'), $soptionsFields);
         // if not add them
         if (!$oInstalled) {
             amDB::query("ALTER TABLE " . TABLE_PRODUCTS_OPTIONS . " ADD COLUMN " . $this->getValue('AM_FIELD_OPTION_SORT_ORDER') . " INT UNSIGNED NOT NULL DEFAULT '0'");
         }
         if (!$ovInstalled) {
             amDB::query("ALTER TABLE " . TABLE_PRODUCTS_ATTRIBUTES . " ADD COLUMN " . $this->getValue('AM_FIELD_OPTION_VALUE_SORT_ORDER') . " INT UNSIGNED NOT NULL DEFAULT '0'");
         }
         if (!$soInstalled && $this->getValue('AM_USE_SORT_ORDER')) {
             amDB::query("ALTER TABLE " . $this->getValue('AM_TABLE_ATTRIBUTES_TO_TEMPLATES') . " ADD COLUMN " . $this->getValue('AM_FIELD_OPTION_SORT_ORDER') . " INT UNSIGNED NOT NULL DEFAULT '0'");
         }
         // now reset all of the sort orders
         if (!$oInstalled || !$ovInstalled) {
             $allAttributes = amDB::getAll("select * from " . TABLE_PRODUCTS_ATTRIBUTES . " order by products_id, options_id, options_values_id");
             $productId = $optionId = null;
             $oCount = $ovCount = 1;
             $updateValues = array();
             if (is_array($allAttributes)) {
                 foreach ($allAttributes as $attrib) {
                     if ($productId != $attrib['products_id']) {
                         $oCount = $ovCount = 0;
                     }
                     if ($optionId != $attrib['options_id']) {
                         $oCount++;
                         $ovCount = 0;
                     }
                     /** for dev only 
                     				$updateValues[$attrib['products_attributes_id']]['prdoucts_id'] = $attrib['products_id'];
                     				$updateValues[$attrib['products_attributes_id']]['options_id'] = $attrib['options_id'];
                     				$updateValues[$attrib['products_attributes_id']]['options_values_id'] = $attrib['options_values_id'];
                     				**/
                     $updateValues[$attrib['products_attributes_id']]['option_sort'] = $oCount;
                     $updateValues[$attrib['products_attributes_id']]['option_value_sort'] = ++$ovCount;
                     $productId = $attrib['products_id'];
                     $optionId = $attrib['options_id'];
                 }
                 foreach ($updateValues as $attributeId => $sorts) {
                     amDB::query("update " . TABLE_PRODUCTS_ATTRIBUTES . " set " . $this->getValue('AM_FIELD_OPTION_SORT_ORDER') . " = '{$sorts['option_sort']}', " . $this->getValue('AM_FIELD_OPTION_VALUE_SORT_ORDER') . " = '{$sorts['option_value_sort']}' where products_attributes_id = '{$attributeId}' limit 1");
                 }
             }
             //echo '<pre style="text-align:left">'.print_r($updateValues,true);
         }
         // register the checked session so that this check is only done once per session
         amSessionRegister($this->getValue('AM_SESSION_SORT_ORDER_INSTALL_CHECKED'), true);
     }
 }
 /**
  * get all templates and their options and option values
  * @access protected
  * @author Sam West aka Nimmit - osc@kangaroopartners.com
  * @return array 
  */
 function getAllTemplatesAndAttributes()
 {
     if (0 === count($this->arrAllTemplatesAndAttributes)) {
         $ref = amDB::getAll("select * from " . AM_TABLE_TEMPLATES . " at left join " . AM_TABLE_ATTRIBUTES_TO_TEMPLATES . " aa2t using(template_id) order by at.template_id, aa2t.options_id, aa2t.option_values_id");
         $templateID = null;
         foreach ($ref as $res) {
             if ($templateID != $res['template_id']) {
                 $templateID = $res['template_id'];
                 $this->arrAllTemplatesAndAttributes[$templateID]['name'] = $res['template_name'];
             }
             $this->arrAllTemplatesAndAttributes[$templateID][$res['options_id']][] = $res['option_values_id'];
         }
     }
     return $this->arrAllTemplatesAndAttributes;
 }