Esempio n. 1
0
 public static function add_item_auto()
 {
     $ret = array('done' => 0, 'fail' => 0, 'pass' => 0);
     global $db;
     $stmt = $db->stmt_init();
     $search = "SELECT id,merchant,code,deal_type,start_on,end_on,name,site_wide,url FROM " . DB_TABLE_PREFIX . "popshop_coupon WHERE couponID = 0";
     $stmt->prepare($search);
     $stmt->execute();
     $stmt->bind_result($id, $merchant, $code, $deal_type, $start_on, $end_on, $name, $site_wide, $url);
     $popshop_coupons = array();
     while ($stmt->fetch()) {
         $popshop_coupons[] = array('id' => $id, 'merchant' => $merchant, 'code' => $code, 'deal_type' => $deal_type, 'start_on' => $start_on, 'end_on' => $end_on, 'name' => $name, 'site_wide' => $site_wide, 'url' => $url);
     }
     if (count($popshop_coupons) > 0) {
         //do import
         $stores = array();
         $deal_types = null;
         foreach ($popshop_coupons as $popshop_coupon) {
             if (!isset($stores[$popshop_coupon['merchant']])) {
                 $search = "SELECT id,category,image,popular,visible FROM " . DB_TABLE_PREFIX . "stores WHERE popshopID = " . $popshop_coupon['merchant'];
                 $stmt->prepare($search);
                 $stmt->execute();
                 $stmt->bind_result($id, $category, $image, $popular, $visible);
                 if ($stmt->fetch()) {
                     $stores[$popshop_coupon['merchant']] = array('id' => $id, 'category' => $category, 'image' => $image, 'popular' => $popular, 'visible' => $visible);
                 } else {
                     $stores[$popshop_coupon['merchant']] = NULL;
                 }
             }
             if (!$stores[$popshop_coupon['merchant']]) {
                 $ret['pass']++;
                 continue;
             }
             $stmt->prepare("INSERT INTO " . DB_TABLE_PREFIX . "coupons (popshopID, store, category, popular, title, link, tags, code, visible, start, expiration, lastupdate, date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())");
             $storeid = $stores[$popshop_coupon['merchant']]['id'];
             $category = $stores[$popshop_coupon['merchant']]['category'];
             $popular = $stores[$popshop_coupon['merchant']]['popular'];
             $tags = '';
             if ($popshop_coupon['deal_type'] != '') {
                 if (!$deal_types) {
                     $deal_types = actions::listDealTypes();
                 }
                 $arrs = explode(',', $popshop_coupon['deal_type']);
                 foreach ($arrs as $arr) {
                     if (isset($deal_types[intval($arr)])) {
                         if ($tags == '') {
                             $tags = $deal_types[intval($arr)];
                         } else {
                             $tags .= ',' . $deal_types[intval($arr)];
                         }
                     }
                 }
             }
             if ($popshop_coupon['site_wide']) {
                 if ($tags == '') {
                     $tags = 'Site-Wide';
                 } else {
                     $tags .= ',Site-Wide';
                 }
             }
             $publish = $stores[$popshop_coupon['merchant']]['visible'];
             $stmt->bind_param("iiiissssiss", $popshop_coupon['id'], $storeid, $category, $popular, $popshop_coupon['name'], $popshop_coupon['url'], $tags, $popshop_coupon['code'], $publish, $popshop_coupon['start_on'], $popshop_coupon['end_on']);
             $execute = $stmt->execute();
             if ($execute) {
                 $stmt->prepare("SELECT LAST_INSERT_ID() FROM " . DB_TABLE_PREFIX . "stores");
                 $stmt->execute();
                 $stmt->bind_result($id);
                 $stmt->fetch();
                 $stmt->prepare("UPDATE popshop_coupon SET couponID = ? WHERE id = ?");
                 $stmt->bind_param("ii", $id, $popshop_coupon['id']);
                 $execute = $stmt->execute();
                 $ret['done']++;
             } else {
                 $ret['fail']++;
             }
         }
     }
     return $ret;
 }