public function load(ObjectManager $em) { if (self::$go === false) { return; } $albums = array("jsiciarek" => array("The Fly", "The Sea", "My Adventure", "Hot & Cold", "Stars Fell on Alabama", "Who Let the Dogs Out?"), "colak" => array("Lemon Dance", "Strange Butterfly", "Cooking and Fun")); $o = 0; foreach ($albums as $u => $alb) { $user = $this->getReference($u); $creator = new E\Creator(); $creator->setUsername($user->getUsernameCanonical()); $creator->setEmail($user->getEmailCanonical()); $creator->setFirstName($user->getFirstName()); $creator->setLastName($user->getLastName()); $em->persist($creator); foreach ($alb as $a) { $title = sprintf("%s", $a); $description = sprintf("Description of the %s Album created by %s %s.", $a, $creator->getFirstName(), $creator->getLastName()); $obj = new E\Album(); $obj->setIsVisible(true); $obj->setSequenceNumber($o++); $obj->setTitle($title); $obj->setDescription($description); $obj->setCreator($creator); $em->persist($obj); } } $em->flush(); }
/** * @Route("/create-new-album.json", name = "_photogallery_api_create_new_album") */ public function createNewAlbumAction(Request $request) { $frame = array(); try { $this->em->getConnection()->beginTransaction(); $this->checkAccess(); $id = intval($request->get("id")); $title = $request->get("title"); $description = $request->get("description"); $title = trim($title); $description = trim($description); $title = empty($title) ? "New Album" : $title; $description = empty($description) ? null : $description; $images_visible = $request->get("hidden", "off") !== "on"; $is_visible = $request->get("publish", "off") === "on"; $qb = $this->em->createQueryBuilder(); $qb->select("max(a.sequence_number)")->from("SiciarekPhotoGalleryBundle:Album", "a"); $query = $qb->getQuery(); $sequence_number = $query->getSingleScalarResult(); $album = new Album(); $album->setSequenceNumber($sequence_number + 1); if ($id > 0) { $album = $this->em->getRepository("SiciarekPhotoGalleryBundle:Album")->find($id); } $creator = $this->getCreator(); $album->setCreator($creator); $album->setTitle($title); $album->setDescription($description); $album->setIsVisible($is_visible); $files = Request::createFromGlobals()->files->get("photos"); $this->createImages($files, $album, $images_visible); $frame = $this->frames["info"]; $this->em->persist($album); $this->em->flush(); $frame["data"] = array("type" => "album", "id" => $album->getId(), "slug" => $album->getSlug()); $frame["msg"] = sprintf("Album has been %s successfully", $id > 0 ? "updated" : "created"); $this->em->getConnection()->commit(); } catch (\Exception $e) { $this->em->getConnection()->rollback(); $frame = $this->frames["error"]; $frame["msg"] = $e->getMessage(); $frame["data"] = $e->getTraceAsString(); } return $this->jsonResponse($frame); }