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         $sOxId = $this->_aViewData["oxid"] = $this->getEditObjectId();
00031         $sArticleTable = getViewName('oxarticles');
00032 
00033         //create empty edit object
00034         $this->_aViewData["edit"] = oxNew( "oxselectlist" );
00035 
00036         if ( $sOxId != "-1" && isset( $sOxId)) {
00037             // generating category tree for select list
00038             // A. hack - passing language by post as lists uses only language passed by POST/GET/SESSION
00039             $_POST["language"] = $this->_iEditLang;
00040             $this->_createCategoryTree( "artcattree", $sOxId);
00041 
00042             // load object
00043             $oAttr = oxNew( "oxselectlist" );
00044             $oAttr->loadInLang( $this->_iEditLang, $sOxId );
00045 
00046             $aFieldList = $oAttr->getFieldList();
00047             if ( is_array( $aFieldList ) ) {
00048                 foreach ( $aFieldList as $key => $oField ) {
00049                     if ( $oField->priceUnit == '%' ) {
00050                         $oField->price = $oField->fprice;
00051                     }
00052                 }
00053             }
00054 
00055             $oOtherLang = $oAttr->getAvailableInLangs();
00056             if (!isset($oOtherLang[$this->_iEditLang])) {
00057                 // echo "language entry doesn't exist! using: ".key($oOtherLang);
00058                 $oAttr->loadInLang( key($oOtherLang), $sOxId );
00059             }
00060             $this->_aViewData["edit"] =  $oAttr;
00061 
00062 
00063             // remove already created languages
00064             $aLang = array_diff ( oxRegistry::getLang()->getLanguageNames(), $oOtherLang);
00065             if ( count( $aLang))
00066                 $this->_aViewData["posslang"] = $aLang;
00067 
00068             foreach ( $oOtherLang as $id => $language) {
00069                 $oLang = new stdClass();
00070                 $oLang->sLangDesc = $language;
00071                 $oLang->selected = ($id == $this->_iEditLang);
00072                 $this->_aViewData["otherlang"][$id] = clone $oLang;
00073             }
00074 
00075             $iErr = oxSession::getVar( "iErrorCode");
00076             if (!$iErr)
00077                 $iErr = ERR_SUCCESS;
00078             $this->_aViewData["iErrorCode"] = $iErr;
00079             oxSession::setVar("iErrorCode", ERR_SUCCESS);
00080 
00081         }
00082         if ( oxConfig::getParameter("aoc") ) {
00083             $oSelectlistMainAjax = oxNew( 'selectlist_main_ajax' );
00084             $this->_aViewData['oxajax'] = $oSelectlistMainAjax->getColumns();
00085 
00086             return "popups/selectlist_main.tpl";
00087         }
00088         return "selectlist_main.tpl";
00089     }
00090 
00096     public function save()
00097     {
00098         parent::save();
00099 
00100         $sOxId = $this->getEditObjectId();
00101         $aParams    = oxConfig::getParameter( "editval");
00102 
00103             // shopid
00104             $sShopID = oxSession::getVar( "actshop");
00105             $aParams['oxselectlist__oxshopid'] = $sShopID;
00106         $oAttr = oxNew( "oxselectlist" );
00107 
00108         if ( $sOxId != "-1") {
00109             $oAttr->loadInLang( $this->_iEditLang, $sOxId );
00110         } else {
00111             $aParams['oxselectlist__oxid'] = null;
00112         }
00113 
00114 
00115         //$aParams = $oAttr->ConvertNameArray2Idx( $aParams);
00116         $oAttr->setLanguage(0);
00117         $oAttr->assign( $aParams);
00118 
00119         //#708
00120         if ( !is_array( $this->aFieldArray)) {
00121             $this->aFieldArray = oxRegistry::getUtils()->assignValuesFromText( $oAttr->oxselectlist__oxvaldesc->getRawValue() );
00122         }
00123         // build value
00124         $oAttr->oxselectlist__oxvaldesc = new oxField("", oxField::T_RAW);
00125         foreach ( $this->aFieldArray as $oField) {
00126             $oAttr->oxselectlist__oxvaldesc->setValue( $oAttr->oxselectlist__oxvaldesc->getRawValue() . $oField->name, oxField::T_RAW);
00127             if ( isset( $oField->price) && $oField->price) {
00128                 $oAttr->oxselectlist__oxvaldesc->setValue( $oAttr->oxselectlist__oxvaldesc->getRawValue() . "!P!" . trim(str_replace( ",", ".", $oField->price)), oxField::T_RAW);
00129                 if ($oField->priceUnit == '%')
00130                     $oAttr->oxselectlist__oxvaldesc->setValue( $oAttr->oxselectlist__oxvaldesc->getRawValue() . '%', oxField::T_RAW);
00131             }
00132             $oAttr->oxselectlist__oxvaldesc->setValue( $oAttr->oxselectlist__oxvaldesc->getRawValue() . "__@@", oxField::T_RAW);
00133         }
00134 
00135         $oAttr->setLanguage($this->_iEditLang);
00136         $oAttr->save();
00137 
00138         // set oxid if inserted
00139         $this->setEditObjectId( $oAttr->getId() );
00140     }
00141 
00147     public function saveinnlang()
00148     {
00149         $sOxId = $this->getEditObjectId();
00150         $aParams    = oxConfig::getParameter( "editval");
00151 
00152             // shopid
00153             $sShopID = oxSession::getVar( "actshop");
00154             $aParams['oxselectlist__oxshopid'] = $sShopID;
00155         $oObj = oxNew( "oxselectlist" );
00156 
00157         if ( $sOxId != "-1")
00158             $oObj->loadInLang( $this->_iEditLang, $sOxId );
00159         else
00160             $aParams['oxselectlist__oxid'] = null;
00161 
00162 
00163         parent::save();
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 = oxRegistry::getUtils()->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 = oxRegistry::getUtils()->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 = oxRegistry::getUtils()->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 }