/** * @ApiDoc( * description = "Creates and saves a new campaign.", * section="Z_DISABLED", * statusCodes = { * 201 = "Returned when the campaign was added to the database", * 400 = "Returned when the validation returns false ", * 403 = {"Invalid API KEY", "Incorrect combination of request inputs."}, * 500 = "Header x-wsse does not exist" * }, * requirements = { * {"name"="_format", "dataType"="string","requirement"="json|xml","description"="Format"}, * }, * parameters={ * {"name"="name", "dataType"="text", "required"=true, "description"="The campaign name"}, * {"name"="client", "dataType"="string","required"=true,"description"="The campaign client."}, * {"name"="brand", "dataType"="string","required"=true,"description"="The campaign brand."}, * {"name"="product", "dataType"="string","required"=true,"description"="The campaign product."}, * {"name"="division", "dataType"="string","required"=true,"description"="The campaign division."}, * {"name"="productline", "dataType"="string","required"=true,"description"="The campaign productline."}, * {"name"="country", "dataType"="string","required"=true,"description"="The campaign country."}, * {"name"="completion_date", "dataType"="string","required"=true,"description"="The campaign completion date."}, * {"name"="client_deliverabledate","dataType"="string","required"=true,"description"="The campaign deliverable date."}, * } * ) * return string * @View() */ public function postCampaignAction(Request $request) { $user = $this->getUser(); $creationDate = new \DateTime(); $creationDate->setTimezone(self::timezoneUTC()); $em = $this->getDoctrine()->getManager(); $key = Uuid::uuid4()->toString(); $token_key = Uuid::uuid4()->toString(); $client_id = $request->get('client'); $country_id = $request->get('country'); $brand_id = $request->get('brand'); $product_id = $request->get('product'); $productline_id = $request->get('productline'); $division_id = $request->get('division'); $response = new Response(); //Disallow VIEWERS TO POST CAMPAIGNS if ($user->hasRole('ROLE_VIEWER')) { $response->setStatusCode(403); $response->setContent(json_encode(array('success' => false, 'message' => "Viewers are not allowed to create campaigns"))); return $response; } ///////////////////////////////////////////////////////////////////////////////////// // Checks to verify object's existence into the database. ///////////////////////////////////////////////////////////////////////////////////// $client = $this->getDoctrine()->getRepository('CampaignBundle:Client')->findOneById($client_id); if (!$client) { $response->setStatusCode(403); $response->setContent(json_encode(array('success' => false, 'message' => 'Invalid ID provided for field client.'))); return $response; } $division = $this->getDoctrine()->getRepository('CampaignBundle:Division')->findOneById($division_id); if (!$division) { $response->setStatusCode(403); $response->setContent(json_encode(array('success' => false, 'message' => 'Invalid ID provided for field division.'))); return $response; } $brand = $this->getDoctrine()->getRepository('CampaignBundle:Brand')->findOneById($brand_id); if (!$brand) { $response->setStatusCode(403); $response->setContent(json_encode(array('success' => false, 'message' => 'Invalid ID provided for field brand.'))); return $response; } $productline = $this->getDoctrine()->getRepository('CampaignBundle:Productline')->findOneById($productline_id); if (!$productline) { $response->setStatusCode(403); $response->setContent(json_encode(array('success' => false, 'message' => 'Invalid ID provided for field productline.'))); return $response; } $product = $this->getDoctrine()->getRepository('CampaignBundle:Product')->findOneById($product_id); if (!$product) { $response->setStatusCode(403); $response->setContent(json_encode(array('success' => false, 'message' => 'Invalid ID provided for field product.'))); return $response; } $country = $this->getDoctrine()->getRepository('CampaignBundle:Country')->findOneById($country_id); if (!$country) { $response->setStatusCode(403); $response->setContent(json_encode(array('success' => false, 'message' => 'Invalid ID provided for field country .'))); return $response; } //AFTER VALIDATING INPUT , FOR A CONTRIBUTOR , VALIDATE THE ACCESS TOO if ($user->hasRole('ROLE_CONTRIBUTOR')) { $post_can_continue = self::validate_the_put_and_post_for_contributor($client, $country, $user); if (!$post_can_continue) { //print_r($user->getUsername()); $response->setStatusCode(200); $response->setContent(json_encode(array('success' => false, 'message' => "You do not have permissions to create a campaign for the specified client and country values"))); return $response; } } //DISABLED VALIDATION HERE // THE CLIENT WANTS TO BE ABLE TO CREATE DUPLICATE CAMPAIGNS IN SELECT CASES , SO THEY WILL BE RESPONSIBLE FOR MONITORING THE DUPLICATES MANUALLY // ///VERIFY THAT THERE IN'T ALREADY A CAMPAIGN CREATED BY THIS USER , USING THE SPECIFIED NAME. // // $campaing_already_exists_for_creator_name_combo = $this->getDoctrine()->getRepository('CampaignBundle:Campaign')->findOneBy([ // 'user' => $user, // 'name' => $request->get('name')]); // // // if ($campaing_already_exists_for_creator_name_combo) { // $response->setStatusCode(403); // $response->setContent(json_encode(array('success' => false, 'message' => 'You already have a campaign that uses that campaign name. Please choose another one!'))); // return $response; // } // /// End of newly added validation. //////// ///////////////////////////////////////////////////////////////////////////////////// // END Checks to verify object's existence into the database. //////////////////////////////////////////////////////////////////////////////////// ////RELATIONAL CHECKS ////RELATIONAL CHECKS //////////////////////////////////////////////////// // Client should have the respective division // Division should have the respective brand // Brand should have the respective productline // Productline should have the respective product ////////////////////////////////////////////////////////////////// ////////////////////// //Validate that the division specified belongs to the client specified. ////////////////////// if (!($division->getClient()->getId() == $client->getId())) { $response->setStatusCode(403); $response->setContent(json_encode(array('success' => false, 'message' => 'Division does not belong to this Client.'))); return $response; } ////////////////////// //Validate that the brand specified belongs to the division specified. ////////////////////// if (!($brand->getDivision()->getId() == $division->getId())) { $response->setStatusCode(403); $response->setContent(json_encode(array('success' => false, 'message' => 'Brand does not belong to this Division.'))); return $response; } ////////////////////// //Validate that the productline specified belongs to the brand specified. ////////////////////// if (!($productline->getBrand()->getId() == $brand->getId())) { $response->setStatusCode(403); $response->setContent(json_encode(array('success' => false, 'message' => 'Productline does not belong to this Brand.'))); return $response; } //////////////////////// //Validate that the product specified belongs to the productline specified. ////////////////////// if (!($product->getProductline()->getId() == $productline->getId())) { $response->setStatusCode(403); $response->setContent(json_encode(array('success' => false, 'message' => 'Product does not belong to this Productline.'))); return $response; } ////////////////////////////// //END RELATIONAL CHECKS ////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// /////////////////////END OF CHECKS //////////////////////////////////////////////////////////////////////////////////// if (empty($request->get('completion_date'))) { $response->setStatusCode(400); $response->setContent(json_encode(array('success' => false, 'message' => 'The completion_date field is required !'))); return $response; } if (empty($request->get('client_deliverabledate'))) { $response->setStatusCode(400); $response->setContent(json_encode(array('success' => false, 'message' => 'The client_deliverabledate field is required !'))); return $response; } $completion_date_input = $request->get('completion_date'); // Inputs completion and deliverable dates: if ($completion_date_input) { $completion_date = new \DateTime($request->get('completion_date')); $completion_date->setTimezone(self::timezoneUTC()); } $deliverable_date_input = $request->get('client_deliverabledate'); if ($deliverable_date_input) { $deliverable_date = new \DateTime($request->get('client_deliverabledate')); $deliverable_date->setTimezone(self::timezoneUTC()); } //VALIDATE THAT THE COMPLETION DATE IS LATER THAN THE CLIENT_DELIVERABLEDATE if ($completion_date && $deliverable_date) { $seconds_in_one_day = 60 * 60 * 24; $ts_completion = $completion_date->getTimestamp(); $ts_deliverable = $deliverable_date->getTimestamp(); $difference = $ts_completion - $ts_deliverable; if ($difference < $seconds_in_one_day) { $response->setStatusCode(400); $response->setContent(json_encode(array('success' => false, 'message' => 'The Completion Date must be later than the Client Deliverable Date. (1 day minimum)'))); return $response; } } //ERROR MESSAGE : The Completion Date must be later than the Client Deliverable Date. $campaign_status = $this->getDoctrine()->getRepository('CampaignBundle:Campaignstatus')->find(1); // Populate the Campaign object with data from the Request: $campaign = new Campaign(); $campaign->setId($key); $campaign->setUser($user); //$campaign->setBriefOutline('This is the campaigns bief outline text. hardcoded.'); $campaign->setClientPresentation(false); $campaign->setCompleteness(0); $campaign->setName($request->get('name')); $campaign->setClient($client); $campaign->setBrand($brand); $campaign->setProduct($product); $campaign->setProductline($productline); $campaign->setDivision($division); $campaign->setCountry($country); $campaign->setCampaignstatus($campaign_status); $campaign->setCompletionDate($completion_date); $campaign->setClientDeliverabledate($deliverable_date); $campaign->setToken($token_key); $campaign->setNotVisible(false); $campaign->setScreentype('10000'); // Set time for when the file was created: $campaign->setCreatedAt($creationDate); $campaign->setUpdatedAt($creationDate); // Get validator service to check for errors: $validator = $this->get('validator'); $errors = $validator->validate($campaign); // Create and prepare the Response object to be sent back to client: $response = new Response(); if (count($errors) > 0) { // Return $errors in JSON format: $view = $this->view($errors, 400); return $this->handleView($view); } // If no errors were found, instantiate entity_manager to begin. $em->persist($campaign); ///////////////////////////////////////////////////// //Add the user who created the campaign to the campaign's team. ///////////////////////////////////////////////////// $add_as_teammember = new Teammember(); $add_as_teammember->setCampaign($campaign); $add_as_teammember->setMember($user); $add_as_teammember->setIsReviewer(false); $em->persist($add_as_teammember); ////////////////////////////////////////////////////// /// ///////////////////////////////////////////////////// //Create the set of tasks for this campaign ///////////////////////////////////////////////////// $campaign_unique_id = $campaign->getId(); $task_types = $this->getDoctrine()->getRepository('TaskBundle:Taskname')->findAll(); $default_task_status = $this->getDoctrine()->getRepository('TaskBundle:Taskstatus')->find(1); foreach ($task_types as $tasktype) { $new_task = new Task(); $new_task->setCampaign($campaign); $new_task->setTaskname($tasktype); $new_task->setOwner($user); $new_task->setTaskmessage(NULL); $new_task->setMatrixfileversion(0); $new_task->setTaskstatus($default_task_status); $new_task->setPhase($tasktype->getPhaseid()); $new_task->setCreatedAt($creationDate); $new_task->setCreatedby($user); $new_task->setUpdatedAt($creationDate); $em->persist($new_task); } ////////////////////////////////////////////////////// /// $em->flush(); $response->setStatusCode(201); $response->setContent(json_encode(array('success' => true, 'campaignID' => $campaign->getId()))); return $response; }