selectlist_main.php

Go to the documentation of this file.
00001 <?php
00002 
00003 DEFINE("ERR_SUCCESS", 1);
00004 DEFINE("ERR_REQUIREDMISSING", -1);
00005 DEFINE("ERR_POSOUTOFBOUNDS", -2);
00006 
00011 class SelectList_Main extends oxAdminDetails
00012 {
00016      public $aFieldArray = null;
00017 
00025     public function render()
00026     {
00027         $myConfig = $this->getConfig();
00028         parent::render();
00029 
00030 
00031         $sOxId = oxConfig::getParameter( "oxid");
00032         // check if we right now saved a new entry
00033         $sSavedId = oxConfig::getParameter( "saved_oxid");
00034         if ( ($sOxId == "-1" || !isset( $sOxId)) && isset( $sSavedId) ) {
00035             $sOxId = $sSavedId;
00036             oxSession::deleteVar( "saved_oxid");
00037             $this->_aViewData["oxid"] =  $sOxId;
00038             // for reloading upper frame
00039             $this->_aViewData["updatelist"] =  "1";
00040         }
00041 
00042         $sArticleTable = getViewName('oxarticles');
00043 
00044         //create empty edit object
00045         $this->_aViewData["edit"] = oxNew( "oxselectlist" );
00046 
00047         if ( $sOxId != "-1" && isset( $sOxId)) {
00048             // generating category tree for select list
00049             // A. hack - passing language by post as lists uses only language passed by POST/GET/SESSION
00050             $_POST["language"] = $this->_iEditLang;
00051             $sChosenArtCat = $this->_getCategoryTree( "artcattree", $sChosenArtCat, $sOxId);
00052 
00053             // load object
00054             $oAttr = oxNew( "oxselectlist" );
00055             $oAttr->loadInLang( $this->_iEditLang, $sOxId );
00056 
00057             $aFieldList = $oAttr->getFieldList();
00058             if ( is_array( $aFieldList ) ) {
00059                 foreach ( $aFieldList as $key => $oField ) {
00060                     if ( $oField->priceUnit == '%' ) {
00061                         $oField->price = $oField->fprice;
00062                     }
00063                 }
00064             }
00065 
00066             $oOtherLang = $oAttr->getAvailableInLangs();
00067             if (!isset($oOtherLang[$this->_iEditLang])) {
00068                 // echo "language entry doesn't exist! using: ".key($oOtherLang);
00069                 $oAttr->loadInLang( key($oOtherLang), $sOxId );
00070             }
00071             $this->_aViewData["edit"] =  $oAttr;
00072 
00073 
00074             // remove already created languages
00075             $aLang = array_diff ( oxLang::getInstance()->getLanguageNames(), $oOtherLang);
00076             if ( count( $aLang))
00077                 $this->_aViewData["posslang"] = $aLang;
00078 
00079             foreach ( $oOtherLang as $id => $language) {
00080                 $oLang = new oxStdClass();
00081                 $oLang->sLangDesc = $language;
00082                 $oLang->selected = ($id == $this->_iEditLang);
00083                 $this->_aViewData["otherlang"][$id] = clone $oLang;
00084             }
00085 
00086             $iErr = oxConfig::getParameter( "iErrorCode");
00087             if (!$iErr)
00088                 $iErr = ERR_SUCCESS;
00089             $this->_aViewData["iErrorCode"] = $iErr;
00090             oxSession::setVar("iErrorCode", ERR_SUCCESS);
00091 
00092         }
00093         if ( oxConfig::getParameter("aoc") ) {
00094 
00095             $aColumns = array();
00096             include_once 'inc/'.strtolower(__CLASS__).'.inc.php';
00097             $this->_aViewData['oxajax'] = $aColumns;
00098 
00099             return "popups/selectlist_main.tpl";
00100         }
00101         return "selectlist_main.tpl";
00102     }
00103 
00109     public function save()
00110     {
00111 
00112         $sOxId      = oxConfig::getParameter( "oxid");
00113         $aParams    = oxConfig::getParameter( "editval");
00114 
00115             // shopid
00116             $sShopID = oxSession::getVar( "actshop");
00117             $aParams['oxselectlist__oxshopid'] = $sShopID;
00118         $oAttr = oxNew( "oxselectlist" );
00119 
00120         if ( $sOxId != "-1") {
00121             $oAttr->loadInLang( $this->_iEditLang, $sOxId );
00122         } else {
00123             $aParams['oxselectlist__oxid'] = null;
00124         }
00125 
00126 
00127         //$aParams = $oAttr->ConvertNameArray2Idx( $aParams);
00128         $oAttr->setLanguage(0);
00129         $oAttr->assign( $aParams);
00130 
00131         //#708
00132         if ( !is_array( $this->aFieldArray)) {
00133             $this->aFieldArray = oxUtils::getInstance()->assignValuesFromText( $oAttr->oxselectlist__oxvaldesc->getRawValue() );
00134         }
00135         // build value
00136         $oAttr->oxselectlist__oxvaldesc = new oxField("", oxField::T_RAW);
00137         foreach ( $this->aFieldArray as $oField) {
00138             $oAttr->oxselectlist__oxvaldesc->setValue( $oAttr->oxselectlist__oxvaldesc->getRawValue() . $oField->name, oxField::T_RAW);
00139             if ( isset( $oField->price) && $oField->price) {
00140                 $oAttr->oxselectlist__oxvaldesc->setValue( $oAttr->oxselectlist__oxvaldesc->getRawValue() . "!P!" . trim(str_replace( ",", ".", $oField->price)), oxField::T_RAW);
00141                 if ($oField->priceUnit == '%')
00142                     $oAttr->oxselectlist__oxvaldesc->setValue( $oAttr->oxselectlist__oxvaldesc->getRawValue() . '%', oxField::T_RAW);
00143             }
00144             $oAttr->oxselectlist__oxvaldesc->setValue( $oAttr->oxselectlist__oxvaldesc->getRawValue() . "__@@", oxField::T_RAW);
00145         }
00146 
00147         $oAttr->setLanguage($this->_iEditLang);
00148         $oAttr->save();
00149         $this->_aViewData["updatelist"] = "1";
00150 
00151         // set oxid if inserted
00152         if ( $sOxId == "-1") {
00153             oxSession::setVar( "saved_oxid", $oAttr->oxselectlist__oxid->value);
00154         }
00155     }
00156 
00162     public function saveinnlang()
00163     {
00164         $sOxId      = oxConfig::getParameter( "oxid");
00165         $aParams    = oxConfig::getParameter( "editval");
00166 
00167             // shopid
00168             $sShopID = oxSession::getVar( "actshop");
00169             $aParams['oxselectlist__oxshopid'] = $sShopID;
00170         $oObj = oxNew( "oxselectlist" );
00171 
00172         if ( $sOxId != "-1")
00173             $oObj->loadInLang( $this->_iEditLang, $sOxId );
00174         else
00175             $aParams['oxselectlist__oxid'] = null;
00176 
00177 
00178         //$aParams = $oObj->ConvertNameArray2Idx( $aParams);
00179         $oObj->setLanguage(0);
00180         $oObj->assign( $aParams);
00181 
00182         // apply new language
00183         $sNewLanguage = oxConfig::getParameter( "new_lang");
00184         $oObj->setLanguage( $sNewLanguage);
00185         $oObj->save();
00186         $this->_aViewData["updatelist"] = "1";
00187 
00188         // set for reload
00189         oxSession::setVar( "new_lang", $sNewLanguage);
00190 
00191         // set oxid if inserted
00192         if ( $sOxId == "-1")
00193             oxSession::setVar( "saved_oxid", $oObj->oxselectlist__oxid->value);
00194     }
00195 
00201     public function delFields()
00202     {
00203         $oSelectlist = oxNew( "oxselectlist" );
00204         if ( $oSelectlist->loadInLang( $this->_iEditLang, oxConfig::getParameter( "oxid" ) ) ) {
00205 
00206             $aDelFields = oxConfig::getParameter( "aFields" );
00207             $this->aFieldArray = oxUtils::getInstance()->assignValuesFromText( $oSelectlist->oxselectlist__oxvaldesc->getRawValue() );
00208 
00209             if ( is_array( $aDelFields ) && count( $aDelFields ) ) {
00210                 foreach ( $aDelFields as $sDelField ) {
00211                     $sDel = $this->parseFieldName( $sDelField );
00212                     foreach ( $this->aFieldArray as $sKey => $oField ) {
00213                         if ( $oField->name == $sDel ) {
00214                             unset(  $this->aFieldArray[$sKey]);
00215                             break;
00216                         }
00217                     }
00218                 }
00219                 $this->save();
00220             }
00221         }
00222     }
00223 
00229     public function addField()
00230     {
00231         $oSelectlist = oxNew( "oxselectlist" );
00232         if ( $oSelectlist->loadInLang( $this->_iEditLang, oxConfig::getParameter( "oxid" ) ) ) {
00233 
00234 
00235             $sAddField = oxConfig::getParameter("sAddField");
00236             if ( empty( $sAddField ) ) {
00237                 oxSession::setVar( "iErrorCode", ERR_REQUIREDMISSING );
00238                 return;
00239             }
00240 
00241             $this->aFieldArray = oxUtils::getInstance()->assignValuesFromText( $oSelectlist->oxselectlist__oxvaldesc->getRawValue() );
00242 
00243             $oField = new stdClass();
00244             $oField->name      = $sAddField;
00245             $oField->price     = oxConfig::getParameter( "sAddFieldPriceMod" );
00246             $oField->priceUnit = oxConfig::getParameter( "sAddFieldPriceModUnit" );
00247 
00248             $this->aFieldArray[] = $oField;
00249             if ( $iPos = oxConfig::getParameter( "sAddFieldPos" ) ) {
00250                 if ( $this->_rearrangeFields( $oField, $iPos-1 ) ) {
00251                     return;
00252                 }
00253             }
00254 
00255             $this->save();
00256         }
00257     }
00258 
00264     public function changeField()
00265     {
00266         $sAddField = oxConfig::getParameter( "sAddField" );
00267         if ( empty( $sAddField ) ) {
00268             oxSession::setVar("iErrorCode", ERR_REQUIREDMISSING );
00269             return;
00270         }
00271 
00272         $aChangeFields = oxConfig::getParameter( "aFields" );
00273         if ( is_array( $aChangeFields ) && count( $aChangeFields ) ) {
00274 
00275             $oSelectlist = oxNew( "oxselectlist" );
00276             if ( $oSelectlist->loadInLang( $this->_iEditLang, oxConfig::getParameter( "oxid" ) ) ) {
00277 
00278                 $this->aFieldArray = oxUtils::getInstance()->assignValuesFromText( $oSelectlist->oxselectlist__oxvaldesc->getRawValue() );
00279                 $sChangeFieldName = $this->parseFieldName( $aChangeFields[0] );
00280 
00281                 foreach ( $this->aFieldArray as $sKey => $oField ) {
00282                     if ( $oField->name == $sChangeFieldName ) {
00283                         $this->aFieldArray[$sKey]->name      = $sAddField;
00284                         $this->aFieldArray[$sKey]->price     = oxConfig::getParameter( "sAddFieldPriceMod" );
00285                         $this->aFieldArray[$sKey]->priceUnit = oxConfig::getParameter( "sAddFieldPriceModUnit" );
00286                         if ( $iPos = oxConfig::getParameter( "sAddFieldPos" ) ) {
00287                             if ( $this->_rearrangeFields( $this->aFieldArray[$sKey], $iPos-1 ) ) {
00288                                 return;
00289                             }
00290                         }
00291                         break;
00292                     }
00293                 }
00294                 $this->save();
00295             }
00296         }
00297     }
00298 
00308     protected function _rearrangeFields( $oField, $iPos )
00309     {
00310         if ( !isset( $this->aFieldArray ) || !is_array( $this->aFieldArray ) ) {
00311            return true;
00312         }
00313 
00314         $iFieldCount = count( $this->aFieldArray );
00315         if ( $iPos < 0 || $iPos >= $iFieldCount ) {
00316             oxSession::setVar( "iErrorCode", ERR_POSOUTOFBOUNDS );
00317             return true;
00318         }
00319 
00320         $iCurrentPos = -1;
00321         for ( $i = 0; $i < $iFieldCount; $i++ ) {
00322             if ( $this->aFieldArray[$i] == $oField ) {
00323                 $iCurrentPos = $i;
00324                 break;
00325             }
00326         }
00327 
00328         if ( $iCurrentPos == -1 ) {
00329             return true;
00330         }
00331 
00332         if ( $iCurrentPos == $iPos ) {
00333             return false;
00334         }
00335 
00336         $sField = $this->aFieldArray[$iCurrentPos];
00337         if ( $iCurrentPos < $iPos ) {
00338             for ( $i = $iCurrentPos; $i < $iPos; $i++ ) {
00339                 $this->aFieldArray[$i] = $this->aFieldArray[$i+1];
00340             }
00341             $this->aFieldArray[$iPos] = $sField;
00342             return false;
00343         } else {
00344             for ( $i = $iCurrentPos; $i > $iPos; $i-- ) {
00345                 $this->aFieldArray[$i] = $this->aFieldArray[$i-1];
00346             }
00347             $this->aFieldArray[$iPos] = $sField;
00348             return false;
00349         }
00350     }
00351 
00360     function parseFieldName($sInput)
00361     {
00362         $aInput = explode('__@@', $sInput, 3);
00363         return $aInput[1];
00364     }
00365 }

Generated by  doxygen 1.6.2