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         parent::save();
00102 
00103         $sOxId = $this->getEditObjectId();
00104         $aParams    = oxConfig::getParameter( "editval");
00105 
00106             // shopid
00107             $sShopID = oxSession::getVar( "actshop");
00108             $aParams['oxselectlist__oxshopid'] = $sShopID;
00109         $oAttr = oxNew( "oxselectlist" );
00110 
00111         if ( $sOxId != "-1") {
00112             $oAttr->loadInLang( $this->_iEditLang, $sOxId );
00113         } else {
00114             $aParams['oxselectlist__oxid'] = null;
00115         }
00116 
00117 
00118         //$aParams = $oAttr->ConvertNameArray2Idx( $aParams);
00119         $oAttr->setLanguage(0);
00120         $oAttr->assign( $aParams);
00121 
00122         //#708
00123         if ( !is_array( $this->aFieldArray)) {
00124             $this->aFieldArray = oxUtils::getInstance()->assignValuesFromText( $oAttr->oxselectlist__oxvaldesc->getRawValue() );
00125         }
00126         // build value
00127         $oAttr->oxselectlist__oxvaldesc = new oxField("", oxField::T_RAW);
00128         foreach ( $this->aFieldArray as $oField) {
00129             $oAttr->oxselectlist__oxvaldesc->setValue( $oAttr->oxselectlist__oxvaldesc->getRawValue() . $oField->name, oxField::T_RAW);
00130             if ( isset( $oField->price) && $oField->price) {
00131                 $oAttr->oxselectlist__oxvaldesc->setValue( $oAttr->oxselectlist__oxvaldesc->getRawValue() . "!P!" . trim(str_replace( ",", ".", $oField->price)), oxField::T_RAW);
00132                 if ($oField->priceUnit == '%')
00133                     $oAttr->oxselectlist__oxvaldesc->setValue( $oAttr->oxselectlist__oxvaldesc->getRawValue() . '%', oxField::T_RAW);
00134             }
00135             $oAttr->oxselectlist__oxvaldesc->setValue( $oAttr->oxselectlist__oxvaldesc->getRawValue() . "__@@", oxField::T_RAW);
00136         }
00137 
00138         $oAttr->setLanguage($this->_iEditLang);
00139         $oAttr->save();
00140 
00141         // set oxid if inserted
00142         $this->setEditObjectId( $oAttr->getId() );
00143     }
00144 
00150     public function saveinnlang()
00151     {
00152         $sOxId = $this->getEditObjectId();
00153         $aParams    = oxConfig::getParameter( "editval");
00154 
00155             // shopid
00156             $sShopID = oxSession::getVar( "actshop");
00157             $aParams['oxselectlist__oxshopid'] = $sShopID;
00158         $oObj = oxNew( "oxselectlist" );
00159 
00160         if ( $sOxId != "-1")
00161             $oObj->loadInLang( $this->_iEditLang, $sOxId );
00162         else
00163             $aParams['oxselectlist__oxid'] = null;
00164 
00165 
00166         parent::save();
00167 
00168         //$aParams = $oObj->ConvertNameArray2Idx( $aParams);
00169         $oObj->setLanguage(0);
00170         $oObj->assign( $aParams);
00171 
00172         // apply new language
00173         $oObj->setLanguage( oxConfig::getParameter( "new_lang" ) );
00174         $oObj->save();
00175 
00176         // set oxid if inserted
00177         $this->setEditObjectId( $oObj->getId() );
00178     }
00179 
00185     public function delFields()
00186     {
00187         $oSelectlist = oxNew( "oxselectlist" );
00188         if ( $oSelectlist->loadInLang( $this->_iEditLang, $this->getEditObjectId() ) ) {
00189 
00190             $aDelFields = oxConfig::getParameter( "aFields" );
00191             $this->aFieldArray = oxUtils::getInstance()->assignValuesFromText( $oSelectlist->oxselectlist__oxvaldesc->getRawValue() );
00192 
00193             if ( is_array( $aDelFields ) && count( $aDelFields ) ) {
00194                 foreach ( $aDelFields as $sDelField ) {
00195                     $sDel = $this->parseFieldName( $sDelField );
00196                     foreach ( $this->aFieldArray as $sKey => $oField ) {
00197                         if ( $oField->name == $sDel ) {
00198                             unset(  $this->aFieldArray[$sKey]);
00199                             break;
00200                         }
00201                     }
00202                 }
00203                 $this->save();
00204             }
00205         }
00206     }
00207 
00213     public function addField()
00214     {
00215         $oSelectlist = oxNew( "oxselectlist" );
00216         if ( $oSelectlist->loadInLang( $this->_iEditLang, $this->getEditObjectId() ) ) {
00217 
00218 
00219             $sAddField = oxConfig::getParameter("sAddField");
00220             if ( empty( $sAddField ) ) {
00221                 oxSession::setVar( "iErrorCode", ERR_REQUIREDMISSING );
00222                 return;
00223             }
00224 
00225             $this->aFieldArray = oxUtils::getInstance()->assignValuesFromText( $oSelectlist->oxselectlist__oxvaldesc->getRawValue() );
00226 
00227             $oField = new stdClass();
00228             $oField->name      = $sAddField;
00229             $oField->price     = oxConfig::getParameter( "sAddFieldPriceMod" );
00230             $oField->priceUnit = oxConfig::getParameter( "sAddFieldPriceModUnit" );
00231 
00232             $this->aFieldArray[] = $oField;
00233             if ( $iPos = oxConfig::getParameter( "sAddFieldPos" ) ) {
00234                 if ( $this->_rearrangeFields( $oField, $iPos-1 ) ) {
00235                     return;
00236                 }
00237             }
00238 
00239             $this->save();
00240         }
00241     }
00242 
00248     public function changeField()
00249     {
00250         $sAddField = oxConfig::getParameter( "sAddField" );
00251         if ( empty( $sAddField ) ) {
00252             oxSession::setVar("iErrorCode", ERR_REQUIREDMISSING );
00253             return;
00254         }
00255 
00256         $aChangeFields = oxConfig::getParameter( "aFields" );
00257         if ( is_array( $aChangeFields ) && count( $aChangeFields ) ) {
00258 
00259             $oSelectlist = oxNew( "oxselectlist" );
00260             if ( $oSelectlist->loadInLang( $this->_iEditLang, $this->getEditObjectId() ) ) {
00261 
00262                 $this->aFieldArray = oxUtils::getInstance()->assignValuesFromText( $oSelectlist->oxselectlist__oxvaldesc->getRawValue() );
00263                 $sChangeFieldName = $this->parseFieldName( $aChangeFields[0] );
00264 
00265                 foreach ( $this->aFieldArray as $sKey => $oField ) {
00266                     if ( $oField->name == $sChangeFieldName ) {
00267                         $this->aFieldArray[$sKey]->name      = $sAddField;
00268                         $this->aFieldArray[$sKey]->price     = oxConfig::getParameter( "sAddFieldPriceMod" );
00269                         $this->aFieldArray[$sKey]->priceUnit = oxConfig::getParameter( "sAddFieldPriceModUnit" );
00270                         if ( $iPos = oxConfig::getParameter( "sAddFieldPos" ) ) {
00271                             if ( $this->_rearrangeFields( $this->aFieldArray[$sKey], $iPos-1 ) ) {
00272                                 return;
00273                             }
00274                         }
00275                         break;
00276                     }
00277                 }
00278                 $this->save();
00279             }
00280         }
00281     }
00282 
00292     protected function _rearrangeFields( $oField, $iPos )
00293     {
00294         if ( !isset( $this->aFieldArray ) || !is_array( $this->aFieldArray ) ) {
00295            return true;
00296         }
00297 
00298         $iFieldCount = count( $this->aFieldArray );
00299         if ( $iPos < 0 || $iPos >= $iFieldCount ) {
00300             oxSession::setVar( "iErrorCode", ERR_POSOUTOFBOUNDS );
00301             return true;
00302         }
00303 
00304         $iCurrentPos = -1;
00305         for ( $i = 0; $i < $iFieldCount; $i++ ) {
00306             if ( $this->aFieldArray[$i] == $oField ) {
00307                 $iCurrentPos = $i;
00308                 break;
00309             }
00310         }
00311 
00312         if ( $iCurrentPos == -1 ) {
00313             return true;
00314         }
00315 
00316         if ( $iCurrentPos == $iPos ) {
00317             return false;
00318         }
00319 
00320         $sField = $this->aFieldArray[$iCurrentPos];
00321         if ( $iCurrentPos < $iPos ) {
00322             for ( $i = $iCurrentPos; $i < $iPos; $i++ ) {
00323                 $this->aFieldArray[$i] = $this->aFieldArray[$i+1];
00324             }
00325             $this->aFieldArray[$iPos] = $sField;
00326             return false;
00327         } else {
00328             for ( $i = $iCurrentPos; $i > $iPos; $i-- ) {
00329                 $this->aFieldArray[$i] = $this->aFieldArray[$i-1];
00330             }
00331             $this->aFieldArray[$iPos] = $sField;
00332             return false;
00333         }
00334     }
00335 
00344     function parseFieldName($sInput)
00345     {
00346         $aInput = explode('__@@', $sInput, 3);
00347         return $aInput[1];
00348     }
00349 }