예제 #1
0
 public static function insertProduct($product)
 {
     Log::info(__CLASS__ . "::" . __METHOD__ . "::" . "Attempting to insert product data into database");
     #First insert any new keywords
     $keyword_array = explode(" ", $product['keywords']);
     $final_key_array = array();
     foreach ($keyword_array as $key) {
         $key = substr($key, 1);
         $inserted_key = Keyword::firstOrCreate(['keyword' => $key]);
         array_push($final_key_array, $inserted_key->id);
     }
     #Now retrieve the keyword ids and save them
     $keywords_from_db = DB::table('keywords')->whereIn('id', $final_key_array)->get();
     # Get all the filenames in form of an array
     $filename_array = explode(":", $product['filenamestring']);
     $primary_image_path = 'no_image.jpg';
     if (count($filename_array) > 0) {
         $primary_image_path = $filename_array[0];
     }
     # Now that we have all the information, start transaction
     try {
         DB::beginTransaction();
         $logged_in_user = Auth::id();
         $user = User::where('id', $logged_in_user)->first();
         #First insert in the product table
         Log::info(__CLASS__ . "::" . __METHOD__ . "::" . "Attempting to insert product data into database");
         $product_id = DB::table('products')->insertGetId(['title' => $product['title'], 'description' => $product['description'], 'delivery' => $product['delivery'], 'pickup' => $product['pickup'], 'price' => $product['price'], 'free' => $product['free'], 'user_id' => $logged_in_user, 'primary_image_path' => $primary_image_path, 'new' => $product['new'], 'used' => $product['used'], 'state' => 'PRESUBMIT', 'university_id' => $user->university_id]);
         Log::info(__CLASS__ . "::" . __METHOD__ . "::" . "Got product id " . $product_id);
         #Now insert products_categories
         foreach ($product['category'] as $cat) {
             Log::info(__CLASS__ . "::" . __METHOD__ . "::" . "Inserting product category " . $product_id . ":" . $cat);
             DB::table('products_categories')->insert(['product_id' => $product_id, 'category_id' => $cat]);
         }
         # Now insert keywors
         foreach ($keywords_from_db as $keyword_db) {
             Log::info(__CLASS__ . "::" . __METHOD__ . "::" . "Inserting product keyword " . $product_id . ":" . $keyword_db->id);
             DB::table('products_keywords')->insert(['product_id' => $product_id, 'keyword_id' => $keyword_db->id]);
         }
         # Now insert the images
         if (count($filename_array) > 1) {
             for ($i = 1; $i < count($filename_array) - 1; $i++) {
                 Log::info(__CLASS__ . "::" . __METHOD__ . "::" . "Inserting product image " . $product_id . ":" . $filename_array[$i]);
                 DB::table('products_images')->insert(['product_id' => $product_id, 'image_path' => $filename_array[$i]]);
             }
         }
         DB::commit();
         return $product_id;
     } catch (Exception $e) {
         Log::info(__CLASS__ . "::" . __METHOD__ . "::" . "Exception inserting product" . $e->message());
         DB::rollBack();
         return $e->message();
     }
     return $results;
 }