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 {
00013 
00017     public $aFieldArray = null;
00018 
00026     public function render()
00027     {
00028         $myConfig = $this->getConfig();
00029         parent::render();
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             $this->_createCategoryTree("artcattree", $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(oxRegistry::getLang()->getLanguageNames(), $oOtherLang);
00066             if (count($aLang)) {
00067                 $this->_aViewData["posslang"] = $aLang;
00068             }
00069 
00070             foreach ($oOtherLang as $id => $language) {
00071                 $oLang = new stdClass();
00072                 $oLang->sLangDesc = $language;
00073                 $oLang->selected = ($id == $this->_iEditLang);
00074                 $this->_aViewData["otherlang"][$id] = clone $oLang;
00075             }
00076 
00077             $iErr = oxRegistry::getSession()->getVariable("iErrorCode");
00078 
00079             if (!$iErr) {
00080                 $iErr = ERR_SUCCESS;
00081             }
00082 
00083             $this->_aViewData["iErrorCode"] = $iErr;
00084             oxRegistry::getSession()->setVariable("iErrorCode", ERR_SUCCESS);
00085 
00086         }
00087         if (oxRegistry::getConfig()->getRequestParameter("aoc")) {
00088             $oSelectlistMainAjax = oxNew('selectlist_main_ajax');
00089             $this->_aViewData['oxajax'] = $oSelectlistMainAjax->getColumns();
00090 
00091             return "popups/selectlist_main.tpl";
00092         }
00093 
00094         return "selectlist_main.tpl";
00095     }
00096 
00102     public function save()
00103     {
00104         parent::save();
00105 
00106         $sOxId = $this->getEditObjectId();
00107         $aParams = oxRegistry::getConfig()->getRequestParameter("editval");
00108 
00109         // shopid
00110         $sShopID = oxRegistry::getSession()->getVariable("actshop");
00111         $aParams['oxselectlist__oxshopid'] = $sShopID;
00112         $oAttr = oxNew("oxselectlist");
00113 
00114         if ($sOxId != "-1") {
00115             $oAttr->loadInLang($this->_iEditLang, $sOxId);
00116         } else {
00117             $aParams['oxselectlist__oxid'] = null;
00118         }
00119 
00120 
00121         //$aParams = $oAttr->ConvertNameArray2Idx( $aParams);
00122         $oAttr->setLanguage(0);
00123         $oAttr->assign($aParams);
00124 
00125         //#708
00126         if (!is_array($this->aFieldArray)) {
00127             $this->aFieldArray = oxRegistry::getUtils()->assignValuesFromText($oAttr->oxselectlist__oxvaldesc->getRawValue());
00128         }
00129         // build value
00130         $oAttr->oxselectlist__oxvaldesc = new oxField("", oxField::T_RAW);
00131         foreach ($this->aFieldArray as $oField) {
00132             $oAttr->oxselectlist__oxvaldesc->setValue($oAttr->oxselectlist__oxvaldesc->getRawValue() . $oField->name, oxField::T_RAW);
00133             if (isset($oField->price) && $oField->price) {
00134                 $oAttr->oxselectlist__oxvaldesc->setValue($oAttr->oxselectlist__oxvaldesc->getRawValue() . "!P!" . trim(str_replace(",", ".", $oField->price)), oxField::T_RAW);
00135                 if ($oField->priceUnit == '%') {
00136                     $oAttr->oxselectlist__oxvaldesc->setValue($oAttr->oxselectlist__oxvaldesc->getRawValue() . '%', oxField::T_RAW);
00137                 }
00138             }
00139             $oAttr->oxselectlist__oxvaldesc->setValue($oAttr->oxselectlist__oxvaldesc->getRawValue() . "__@@", oxField::T_RAW);
00140         }
00141 
00142         $oAttr->setLanguage($this->_iEditLang);
00143         $oAttr->save();
00144 
00145         // set oxid if inserted
00146         $this->setEditObjectId($oAttr->getId());
00147     }
00148 
00154     public function saveinnlang()
00155     {
00156         $sOxId = $this->getEditObjectId();
00157         $aParams = oxRegistry::getConfig()->getRequestParameter("editval");
00158 
00159         // shopid
00160         $sShopID = oxRegistry::getSession()->getVariable("actshop");
00161         $aParams['oxselectlist__oxshopid'] = $sShopID;
00162         $oObj = oxNew("oxselectlist");
00163 
00164         if ($sOxId != "-1") {
00165             $oObj->loadInLang($this->_iEditLang, $sOxId);
00166         } else {
00167             $aParams['oxselectlist__oxid'] = null;
00168         }
00169 
00170 
00171         parent::save();
00172 
00173         //$aParams = $oObj->ConvertNameArray2Idx( $aParams);
00174         $oObj->setLanguage(0);
00175         $oObj->assign($aParams);
00176 
00177         // apply new language
00178         $oObj->setLanguage(oxRegistry::getConfig()->getRequestParameter("new_lang"));
00179         $oObj->save();
00180 
00181         // set oxid if inserted
00182         $this->setEditObjectId($oObj->getId());
00183     }
00184 
00190     public function delFields()
00191     {
00192         $oSelectlist = oxNew("oxselectlist");
00193         if ($oSelectlist->loadInLang($this->_iEditLang, $this->getEditObjectId())) {
00194 
00195             $aDelFields = oxRegistry::getConfig()->getRequestParameter("aFields");
00196             $this->aFieldArray = oxRegistry::getUtils()->assignValuesFromText($oSelectlist->oxselectlist__oxvaldesc->getRawValue());
00197 
00198             if (is_array($aDelFields) && count($aDelFields)) {
00199                 foreach ($aDelFields as $sDelField) {
00200                     $sDel = $this->parseFieldName($sDelField);
00201                     foreach ($this->aFieldArray as $sKey => $oField) {
00202                         if ($oField->name == $sDel) {
00203                             unset($this->aFieldArray[$sKey]);
00204                             break;
00205                         }
00206                     }
00207                 }
00208                 $this->save();
00209             }
00210         }
00211     }
00212 
00218     public function addField()
00219     {
00220         $oSelectlist = oxNew("oxselectlist");
00221         if ($oSelectlist->loadInLang($this->_iEditLang, $this->getEditObjectId())) {
00222 
00223 
00224             $sAddField = oxRegistry::getConfig()->getRequestParameter("sAddField");
00225             if (empty($sAddField)) {
00226                 oxRegistry::getSession()->setVariable("iErrorCode", ERR_REQUIREDMISSING);
00227 
00228                 return;
00229             }
00230 
00231             $this->aFieldArray = oxRegistry::getUtils()->assignValuesFromText($oSelectlist->oxselectlist__oxvaldesc->getRawValue());
00232 
00233             $oField = new stdClass();
00234             $oField->name = $sAddField;
00235             $oField->price = oxRegistry::getConfig()->getRequestParameter("sAddFieldPriceMod");
00236             $oField->priceUnit = oxRegistry::getConfig()->getRequestParameter("sAddFieldPriceModUnit");
00237 
00238             $this->aFieldArray[] = $oField;
00239             if ($iPos = oxRegistry::getConfig()->getRequestParameter("sAddFieldPos")) {
00240                 if ($this->_rearrangeFields($oField, $iPos - 1)) {
00241                     return;
00242                 }
00243             }
00244 
00245             $this->save();
00246         }
00247     }
00248 
00254     public function changeField()
00255     {
00256         $sAddField = oxRegistry::getConfig()->getRequestParameter("sAddField");
00257         if (empty($sAddField)) {
00258             oxRegistry::getSession()->setVariable("iErrorCode", ERR_REQUIREDMISSING);
00259 
00260             return;
00261         }
00262 
00263         $aChangeFields = oxRegistry::getConfig()->getRequestParameter("aFields");
00264         if (is_array($aChangeFields) && count($aChangeFields)) {
00265 
00266             $oSelectlist = oxNew("oxselectlist");
00267             if ($oSelectlist->loadInLang($this->_iEditLang, $this->getEditObjectId())) {
00268 
00269                 $this->aFieldArray = oxRegistry::getUtils()->assignValuesFromText($oSelectlist->oxselectlist__oxvaldesc->getRawValue());
00270                 $sChangeFieldName = $this->parseFieldName($aChangeFields[0]);
00271 
00272                 foreach ($this->aFieldArray as $sKey => $oField) {
00273                     if ($oField->name == $sChangeFieldName) {
00274                         $this->aFieldArray[$sKey]->name = $sAddField;
00275                         $this->aFieldArray[$sKey]->price = oxRegistry::getConfig()->getRequestParameter("sAddFieldPriceMod");
00276                         $this->aFieldArray[$sKey]->priceUnit = oxRegistry::getConfig()->getRequestParameter("sAddFieldPriceModUnit");
00277                         if ($iPos = oxRegistry::getConfig()->getRequestParameter("sAddFieldPos")) {
00278                             if ($this->_rearrangeFields($this->aFieldArray[$sKey], $iPos - 1)) {
00279                                 return;
00280                             }
00281                         }
00282                         break;
00283                     }
00284                 }
00285                 $this->save();
00286             }
00287         }
00288     }
00289 
00299     protected function _rearrangeFields($oField, $iPos)
00300     {
00301         if (!isset($this->aFieldArray) || !is_array($this->aFieldArray)) {
00302             return true;
00303         }
00304 
00305         $iFieldCount = count($this->aFieldArray);
00306         if ($iPos < 0 || $iPos >= $iFieldCount) {
00307             oxRegistry::getSession()->setVariable("iErrorCode", ERR_POSOUTOFBOUNDS);
00308 
00309             return true;
00310         }
00311 
00312         $iCurrentPos = -1;
00313         for ($i = 0; $i < $iFieldCount; $i++) {
00314             if ($this->aFieldArray[$i] == $oField) {
00315                 $iCurrentPos = $i;
00316                 break;
00317             }
00318         }
00319 
00320         if ($iCurrentPos == -1) {
00321             return true;
00322         }
00323 
00324         if ($iCurrentPos == $iPos) {
00325             return false;
00326         }
00327 
00328         $sField = $this->aFieldArray[$iCurrentPos];
00329         if ($iCurrentPos < $iPos) {
00330             for ($i = $iCurrentPos; $i < $iPos; $i++) {
00331                 $this->aFieldArray[$i] = $this->aFieldArray[$i + 1];
00332             }
00333             $this->aFieldArray[$iPos] = $sField;
00334 
00335             return false;
00336         } else {
00337             for ($i = $iCurrentPos; $i > $iPos; $i--) {
00338                 $this->aFieldArray[$i] = $this->aFieldArray[$i - 1];
00339             }
00340             $this->aFieldArray[$iPos] = $sField;
00341 
00342             return false;
00343         }
00344     }
00345 
00354     public function parseFieldName($sInput)
00355     {
00356         $aInput = explode('__@@', $sInput, 3);
00357 
00358         return $aInput[1];
00359     }
00360 }