示例#1
0
 public function processImportBadges($badges, $badges_lang, $id_lang)
 {
     $formated_badges_lang = array();
     foreach ($badges_lang as $lang) {
         $formated_badges_lang[$lang->id_ps_badge] = array('name' => array($id_lang => $lang->name), 'description' => array($id_lang => $lang->description), 'group_name' => array($id_lang => $lang->group_name));
     }
     $current_badges = array();
     $result = Db::getInstance()->ExecuteS('SELECT id_ps_badge FROM ' . _DB_PREFIX_ . 'badge');
     foreach ($result as $row) {
         $current_badges[] = (int) $row['id_ps_badge'];
     }
     $cond_ids = $this->getFormatedConditionsIds();
     foreach ($badges as $badge) {
         try {
             //if badge already exist we update language data
             if (in_array((int) $badge->id_ps_badge, $current_badges)) {
                 $bdg = new Badge(Badge::getIdByIdPs((int) $badge->id_ps_badge));
                 $bdg->name[$id_lang] = $formated_badges_lang[$badge->id_ps_badge]['name'][$id_lang];
                 $bdg->description[$id_lang] = $formated_badges_lang[$badge->id_ps_badge]['description'][$id_lang];
                 $bdg->group_name[$id_lang] = $formated_badges_lang[$badge->id_ps_badge]['group_name'][$id_lang];
                 $bdg->update();
                 unset($current_badges[(int) array_search($badge->id_ps_badge, $current_badges)]);
             } else {
                 $badge_data = array_merge((array) $badge, $formated_badges_lang[$badge->id_ps_badge]);
                 $bdg = new Badge();
                 $bdg->hydrate($badge_data, (int) $id_lang);
                 $bdg->add();
                 foreach ($badge->conditions as $cond) {
                     Db::getInstance()->insert('condition_badge', array('id_condition' => $cond_ids[$cond], 'id_badge' => $bdg->id));
                 }
             }
             unset($bdg);
         } catch (Exception $e) {
             continue;
         }
     }
     // Delete badges that are not in the file anymore
     foreach ($current_badges as $id_ps_badge) {
         $bdg = new Badge(Badge::getIdByIdPs((int) $id_ps_badge));
         $bdg->delete();
     }
 }