Ejemplo n.º 1
0
 public function saveeditAction()
 {
     $this->view->disable();
     if ($this->request->isPost()) {
         $data = $this->request->getPost('data');
     }
     $data = json_decode($data, true);
     $uri = null;
     foreach ($data as $cat) {
         if ($cat['lang'] == 'ru') {
             $uri = preg_replace('/\\s+/', '-', iconv("UTF-8", "ASCII//TRANSLIT//IGNORE", transliterator_transliterate('Any-Latin; Latin-ASCII', $cat['title'])));
         }
     }
     $status = 1;
     foreach ($data as $cat) {
         // If there IS a title
         if (!empty($cat['title'])) {
             // Update an existing record
             if ($category = PCategory::findFirst("category_id={$cat['id']} AND lang='{$cat['lang']}'")) {
                 $category->uri = $uri;
                 $category->updated = time();
                 $category->title = $cat['title'];
                 $category->active = $cat['active'];
                 if (!$category->save()) {
                     $status = 0;
                 }
                 // Create a record if it doesn't exist
             } else {
                 $cat_ru = PCategory::findFirst("category_id={$cat['id']} AND lang='ru'");
                 $category = new PCategory();
                 $category->category_id = $cat['id'];
                 $category->uri = $uri;
                 $category->_parent = $cat_ru->_parent;
                 $category->updated = time();
                 $category->ordered = $cat_ru->ordered;
                 $category->title = $cat['title'];
                 $category->level = $cat_ru->level;
                 $category->megamenuphoto_id = $cat_ru->megamenuphoto_id;
                 $category->active = $cat['active'];
                 $category->lang = $cat['lang'];
                 if (!$category->save()) {
                     $status = 0;
                 }
             }
             // If there IS NO title delete a record
         } else {
             if ($category = PCategory::findFirst("category_id={$cat['id']} AND lang='{$cat['lang']}'")) {
                 if (!$category->delete()) {
                     $status = 0;
                 }
             }
         }
     }
     echo $status;
 }