protected function _create() { $this->seo(['title' => 'Campaign Create', 'description' => 'Create a new campaign']); $session = Registry::get('session'); $view = $this->getActionView(); $advertisers = \User::isEmpty(["org_id = ?" => $this->org->_id, 'type = ?' => 'advertiser', 'live' => true], ['_id', 'name', 'meta'], ['msg' => 'Please Add an Advertiser!!', 'controller' => $this, 'redirect' => '/advertiser/add.html']); $view->set('advertisers', User::objectArr($advertisers, ['_id', 'name', 'meta'])); $categories = \Category::isEmpty(['org_id' => $this->org->_id], ['name', '_id'], ['msg' => 'Please Set Categories!!', 'controller' => $this, 'redirect' => '/admin/settings.html']); $view->set('categories', $categories); $link = RM::get("link"); if (!$link) { $session->set('$flashMessage', 'Please give any link!!'); $this->redirect('/campaign/manage.html'); } if ($session->get('$lastFetchedLink') !== $link) { $session->set('$lastFetchedLink', $link); $meta = Shared\Utils::fetchCampaign($link); $session->set('Campaign\\Create:$meta', $meta); } else { $meta = $session->get('Campaign\\Create:$meta'); } $view->set("meta", $meta)->set("type", RM::get("type"))->set("errors", []); }
/** * Process the Meta table for campaign urls and create the * campaign from the corresponding URL */ protected function importCampaigns() { $metas = \Meta::all(['prop = ?' => 'campImport']); $users = []; $orgs = []; foreach ($metas as $m) { $user = Usr::find($users, $m->propid, ['_id', 'org_id', 'email', 'meta']); $org = \Organization::find($orgs, $user->org_id); $categories = \Category::all(['org_id' => $org->_id], ['_id', 'name']); $categories = array_values($categories); $category = $categories[array_rand($categories)]; // fetch ad info foreach URL $advert_id = $m->value['advert_id']; $comm = $m->value['campaign'] ?? $org->meta; if (!isset($comm['model']) || !isset($comm['rate'])) { continue; } $urls = $m->value['urls']; foreach ($urls as $url) { $ad = \Ad::first(['org_id' => $org->_id, 'url' => $url]); if ($ad) { continue; } // already crawled URL may be due to failed cron earlier $data = Utils::fetchCampaign($url); $image = Utils::downloadImage($data['image']); if (!$image) { $image = ''; } $ad = new \Ad(['user_id' => $advert_id, 'org_id' => $org->_id, 'title' => $data['title'], 'description' => $data['description'], 'url' => $url, 'image' => $image, 'category' => [$category->_id], 'type' => 'article', 'live' => false, 'device' => ['ALL']]); if ($ad->validate()) { $ad->save(); $rev = $comm['revenue'] ?? 1.25 * (double) $comm['rate']; $commission = new \Commission(['ad_id' => $ad->_id, 'model' => $comm['model'], 'rate' => $comm['rate'], 'revenue' => round($rev, 6), 'coverage' => ['ALL']]); $commission->save(); } else { var_dump($ad->getErrors()); } } $msg = 'Campaigns imported for the user: ' . $user->email; $this->log($msg); $m->delete(); } }