/** * @before _secure * @after _csrfToken */ public function edit($id) { $c = \Ad::first(["_id = ?" => $id, "org_id = ?" => $this->org->_id]); if (!$c) { $this->redirect("/campaign/manage.html"); } $this->seo(['title' => sprintf("Edit: %s", $c->title), 'description' => 'Edit the campaign']); $view = $this->getActionView(); $categories = \Category::all(['org_id' => $this->org->id], ['_id', 'name']); if (RM::type() === 'DELETE') { $this->_commissionDel($c, $view); } if (RM::type() === 'POST') { $action = RM::post("action"); switch ($action) { case 'targeting': $campaign = $c; $campaign->device = RM::post('device', $campaign->device); $visibility = RM::post('visibility', 'public'); if ($visibility === "private") { $campaign->getMeta()['private'] = true; } else { unset($campaign->getMeta()['private']); } $permission = RM::post('permission', false); if ($permission == "yes") { $campaign->getMeta()['permission'] = true; } else { unset($campaign->getMeta()['permission']); } $campaign->save(); $view->set("message", "Campaign updated!!"); break; case 'adedit': $c->category = \Ad::setCategories(RM::post('category')); $c->title = RM::post('title'); $c->url = RM::post("url"); $c->description = RM::post('description'); $c->device = RM::post('device', ['all']); $expiry = RM::post('expiry'); if ($expiry) { $c->expiry = $expiry; } if (!$c->validate()) { $view->set("errors", $c->errors); $view->set("message", "Validation Failed"); } else { $c->save(); $view->set("message", "Campaign updated!!"); } break; case 'imageUpload': $message = "Image Updated Successfully!!"; $img = ' '; $imgUrl = RM::post("imageUrl"); if ($_FILES['image']['name']) { $img = Utils::media('image', 'upload'); } else { if ($imgUrl) { $img = Utils::media($imgUrl, 'download'); } } if (strlen($img) > 4) { // to check if media was uploaded successfully Utils::media($c->image, 'remove'); } else { $img = $c->image; $message = "Please Upload a valid Image!!"; } $c->image = $img; $c->save(); $view->set("message", $message); break; case 'commadd': case 'commedit': $cid = RM::post('cid'); if ($cid) { $comm = Commission::first(["id" => $cid, "ad_id" => $c->_id]); } else { $comm = new \Commission(['ad_id' => $c->_id, 'live' => true]); } $comm->model = RM::post('model'); $comm->description = RM::post('description'); $comm->rate = $this->currency(RM::post('rate')); $comm->revenue = $this->currency(RM::post('revenue')); $comm->coverage = RM::post('coverage', ['ALL']); if ($comm->validate()) { $comm->save(); $view->set("message", "Commission updated!!"); } else { $view->set("errors", $comm->errors); $view->set("message", "Validation Failed"); } break; } } $comms = \Commission::all(["ad_id = ?" => $c->_id]); $view->set("c", $c)->set('categories', $categories)->set('advertisers', User::all(["org_id = ?" => $this->org->id, "type = ?" => "advertiser"], ["id", "name"]))->set("comms", $comms); }