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 = $this->_aViewData["oxid"] = $this->getEditObjectId();
00032         $sArticleTable = getViewName('oxarticles');
00033 
00034         //create empty edit object
00035         $this->_aViewData["edit"] = oxNew( "oxselectlist" );
00036 
00037         if ( $sOxId != "-1" && isset( $sOxId)) {
00038             // generating category tree for select list
00039             // A. hack - passing language by post as lists uses only language passed by POST/GET/SESSION
00040             $_POST["language"] = $this->_iEditLang;
00041             $sChosenArtCat = $this->_getCategoryTree( "artcattree", $sChosenArtCat, $sOxId);
00042 
00043             // load object
00044             $oAttr = oxNew( "oxselectlist" );
00045             $oAttr->loadInLang( $this->_iEditLang, $sOxId );
00046 
00047             $aFieldList = $oAttr->getFieldList();
00048             if ( is_array( $aFieldList ) ) {
00049                 foreach ( $aFieldList as $key => $oField ) {
00050                     if ( $oField->priceUnit == '%' ) {
00051                         $oField->price = $oField->fprice;
00052                     }
00053                 }
00054             }
00055 
00056             $oOtherLang = $oAttr->getAvailableInLangs();
00057             if (!isset($oOtherLang[$this->_iEditLang])) {
00058                 // echo "language entry doesn't exist! using: ".key($oOtherLang);
00059                 $oAttr->loadInLang( key($oOtherLang), $sOxId );
00060             }
00061             $this->_aViewData["edit"] =  $oAttr;
00062 
00063 
00064             // remove already created languages
00065             $aLang = array_diff ( oxLang::getInstance()->getLanguageNames(), $oOtherLang);
00066             if ( count( $aLang))
00067                 $this->_aViewData["posslang"] = $aLang;
00068 
00069             foreach ( $oOtherLang as $id => $language) {
00070                 $oLang = new oxStdClass();
00071                 $oLang->sLangDesc = $language;
00072                 $oLang->selected = ($id == $this->_iEditLang);
00073                 $this->_aViewData["otherlang"][$id] = clone $oLang;
00074             }
00075 
00076             $iErr = oxSession::getVar( "iErrorCode");
00077             if (!$iErr)
00078                 $iErr = ERR_SUCCESS;
00079             $this->_aViewData["iErrorCode"] = $iErr;
00080             oxSession::setVar("iErrorCode", ERR_SUCCESS);
00081 
00082         }
00083         if ( oxConfig::getParameter("aoc") ) {
00084 
00085             $aColumns = array();
00086             include_once 'inc/'.strtolower(__CLASS__).'.inc.php';
00087             $this->_aViewData['oxajax'] = $aColumns;
00088 
00089             return "popups/selectlist_main.tpl";
00090         }
00091         return "selectlist_main.tpl";
00092     }
00093 
00099     public function save()
00100     {
00101 
00102         $sOxId = $this->getEditObjectId();
00103         $aParams    = oxConfig::getParameter( "editval");
00104 
00105             // shopid
00106             $sShopID = oxSession::getVar( "actshop");
00107             $aParams['oxselectlist__oxshopid'] = $sShopID;
00108         $oAttr = oxNew( "oxselectlist" );
00109 
00110         if ( $sOxId != "-1") {
00111             $oAttr->loadInLang( $this->_iEditLang, $sOxId );
00112         } else {
00113             $aParams['oxselectlist__oxid'] = null;
00114         }
00115 
00116 
00117         //$aParams = $oAttr->ConvertNameArray2Idx( $aParams);
00118         $oAttr->setLanguage(0);
00119         $oAttr->assign( $aParams);
00120 
00121         //#708
00122         if ( !is_array( $this->aFieldArray)) {
00123             $this->aFieldArray = oxUtils::getInstance()->assignValuesFromText( $oAttr->oxselectlist__oxvaldesc->getRawValue() );
00124         }
00125         // build value
00126         $oAttr->oxselectlist__oxvaldesc = new oxField("", oxField::T_RAW);
00127         foreach ( $this->aFieldArray as $oField) {
00128             $oAttr->oxselectlist__oxvaldesc->setValue( $oAttr->oxselectlist__oxvaldesc->getRawValue() . $oField->name, oxField::T_RAW);
00129             if ( isset( $oField->price) && $oField->price) {
00130                 $oAttr->oxselectlist__oxvaldesc->setValue( $oAttr->oxselectlist__oxvaldesc->getRawValue() . "!P!" . trim(str_replace( ",", ".", $oField->price)), oxField::T_RAW);
00131                 if ($oField->priceUnit == '%')
00132                     $oAttr->oxselectlist__oxvaldesc->setValue( $oAttr->oxselectlist__oxvaldesc->getRawValue() . '%', oxField::T_RAW);
00133             }
00134             $oAttr->oxselectlist__oxvaldesc->setValue( $oAttr->oxselectlist__oxvaldesc->getRawValue() . "__@@", oxField::T_RAW);
00135         }
00136 
00137         $oAttr->setLanguage($this->_iEditLang);
00138         $oAttr->save();
00139 
00140         // set oxid if inserted
00141         $this->setEditObjectId( $oAttr->getId() );
00142     }
00143 
00149     public function saveinnlang()
00150     {
00151         $sOxId = $this->getEditObjectId();
00152         $aParams    = oxConfig::getParameter( "editval");
00153 
00154             // shopid
00155             $sShopID = oxSession::getVar( "actshop");
00156             $aParams['oxselectlist__oxshopid'] = $sShopID;
00157         $oObj = oxNew( "oxselectlist" );
00158 
00159         if ( $sOxId != "-1")
00160             $oObj->loadInLang( $this->_iEditLang, $sOxId );
00161         else
00162             $aParams['oxselectlist__oxid'] = null;
00163 
00164 
00165         //$aParams = $oObj->ConvertNameArray2Idx( $aParams);
00166         $oObj->setLanguage(0);
00167         $oObj->assign( $aParams);
00168 
00169         // apply new language
00170         $oObj->setLanguage( oxConfig::getParameter( "new_lang" ) );
00171         $oObj->save();
00172 
00173         // set oxid if inserted
00174         $this->setEditObjectId( $oObj->getId() );
00175     }
00176 
00182     public function delFields()
00183     {
00184         $oSelectlist = oxNew( "oxselectlist" );
00185         if ( $oSelectlist->loadInLang( $this->_iEditLang, $this->getEditObjectId() ) ) {
00186 
00187             $aDelFields = oxConfig::getParameter( "aFields" );
00188             $this->aFieldArray = oxUtils::getInstance()->assignValuesFromText( $oSelectlist->oxselectlist__oxvaldesc->getRawValue() );
00189 
00190             if ( is_array( $aDelFields ) && count( $aDelFields ) ) {
00191                 foreach ( $aDelFields as $sDelField ) {
00192                     $sDel = $this->parseFieldName( $sDelField );
00193                     foreach ( $this->aFieldArray as $sKey => $oField ) {
00194                         if ( $oField->name == $sDel ) {
00195                             unset(  $this->aFieldArray[$sKey]);
00196                             break;
00197                         }
00198                     }
00199                 }
00200                 $this->save();
00201             }
00202         }
00203     }
00204 
00210     public function addField()
00211     {
00212         $oSelectlist = oxNew( "oxselectlist" );
00213         if ( $oSelectlist->loadInLang( $this->_iEditLang, $this->getEditObjectId() ) ) {
00214 
00215 
00216             $sAddField = oxConfig::getParameter("sAddField");
00217             if ( empty( $sAddField ) ) {
00218                 oxSession::setVar( "iErrorCode", ERR_REQUIREDMISSING );
00219                 return;
00220             }
00221 
00222             $this->aFieldArray = oxUtils::getInstance()->assignValuesFromText( $oSelectlist->oxselectlist__oxvaldesc->getRawValue() );
00223 
00224             $oField = new stdClass();
00225             $oField->name      = $sAddField;
00226             $oField->price     = oxConfig::getParameter( "sAddFieldPriceMod" );
00227             $oField->priceUnit = oxConfig::getParameter( "sAddFieldPriceModUnit" );
00228 
00229             $this->aFieldArray[] = $oField;
00230             if ( $iPos = oxConfig::getParameter( "sAddFieldPos" ) ) {
00231                 if ( $this->_rearrangeFields( $oField, $iPos-1 ) ) {
00232                     return;
00233                 }
00234             }
00235 
00236             $this->save();
00237         }
00238     }
00239 
00245     public function changeField()
00246     {
00247         $sAddField = oxConfig::getParameter( "sAddField" );
00248         if ( empty( $sAddField ) ) {
00249             oxSession::setVar("iErrorCode", ERR_REQUIREDMISSING );
00250             return;
00251         }
00252 
00253         $aChangeFields = oxConfig::getParameter( "aFields" );
00254         if ( is_array( $aChangeFields ) && count( $aChangeFields ) ) {
00255 
00256             $oSelectlist = oxNew( "oxselectlist" );
00257             if ( $oSelectlist->loadInLang( $this->_iEditLang, $this->getEditObjectId() ) ) {
00258 
00259                 $this->aFieldArray = oxUtils::getInstance()->assignValuesFromText( $oSelectlist->oxselectlist__oxvaldesc->getRawValue() );
00260                 $sChangeFieldName = $this->parseFieldName( $aChangeFields[0] );
00261 
00262                 foreach ( $this->aFieldArray as $sKey => $oField ) {
00263                     if ( $oField->name == $sChangeFieldName ) {
00264                         $this->aFieldArray[$sKey]->name      = $sAddField;
00265                         $this->aFieldArray[$sKey]->price     = oxConfig::getParameter( "sAddFieldPriceMod" );
00266                         $this->aFieldArray[$sKey]->priceUnit = oxConfig::getParameter( "sAddFieldPriceModUnit" );
00267                         if ( $iPos = oxConfig::getParameter( "sAddFieldPos" ) ) {
00268                             if ( $this->_rearrangeFields( $this->aFieldArray[$sKey], $iPos-1 ) ) {
00269                                 return;
00270                             }
00271                         }
00272                         break;
00273                     }
00274                 }
00275                 $this->save();
00276             }
00277         }
00278     }
00279 
00289     protected function _rearrangeFields( $oField, $iPos )
00290     {
00291         if ( !isset( $this->aFieldArray ) || !is_array( $this->aFieldArray ) ) {
00292            return true;
00293         }
00294 
00295         $iFieldCount = count( $this->aFieldArray );
00296         if ( $iPos < 0 || $iPos >= $iFieldCount ) {
00297             oxSession::setVar( "iErrorCode", ERR_POSOUTOFBOUNDS );
00298             return true;
00299         }
00300 
00301         $iCurrentPos = -1;
00302         for ( $i = 0; $i < $iFieldCount; $i++ ) {
00303             if ( $this->aFieldArray[$i] == $oField ) {
00304                 $iCurrentPos = $i;
00305                 break;
00306             }
00307         }
00308 
00309         if ( $iCurrentPos == -1 ) {
00310             return true;
00311         }
00312 
00313         if ( $iCurrentPos == $iPos ) {
00314             return false;
00315         }
00316 
00317         $sField = $this->aFieldArray[$iCurrentPos];
00318         if ( $iCurrentPos < $iPos ) {
00319             for ( $i = $iCurrentPos; $i < $iPos; $i++ ) {
00320                 $this->aFieldArray[$i] = $this->aFieldArray[$i+1];
00321             }
00322             $this->aFieldArray[$iPos] = $sField;
00323             return false;
00324         } else {
00325             for ( $i = $iCurrentPos; $i > $iPos; $i-- ) {
00326                 $this->aFieldArray[$i] = $this->aFieldArray[$i-1];
00327             }
00328             $this->aFieldArray[$iPos] = $sField;
00329             return false;
00330         }
00331     }
00332 
00341     function parseFieldName($sInput)
00342     {
00343         $aInput = explode('__@@', $sInput, 3);
00344         return $aInput[1];
00345     }
00346 }