/** * @before _secure, changeLayout, _admin */ public function fbapps() { $this->seo(array("title" => "FBApps", "view" => $this->getLayoutView())); $view = $this->getActionView(); if (RequestMethods::post("action") == "fbapps") { $fbapp = new Meta(array("user_id" => $this->user->id, "property" => "fbapp", "value" => RequestMethods::post("fbapp"))); $fbapp->save(); $view->set("message", "FBApp Added Successfully"); } $fbapps = Meta::all(array("property=?" => "fbapp")); $view->set("fbapps", $fbapps); }
/** * 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(); } }