Ejemplo n.º 1
0
 /**
  * @before _secure
  */
 public function create()
 {
     $this->_create();
     $view = $this->getActionView();
     if (RM::type() == 'POST') {
         $img = null;
         // give preference to uploaded image
         $img = $this->_upload('image', 'images', ['extension' => 'jpe?g|gif|bmp|png|tif']);
         if (!$img) {
             $img_url = RM::post("image_url");
             $img = Shared\Utils::downloadImage($img_url);
         }
         if (!$img) {
             return $view->set('message', 'Failed to upload the image');
         }
         $expiry = RM::post('expiry');
         $campaign = new \Ad(['user_id' => RM::post('advert_id'), 'title' => RM::post('title'), 'description' => RM::post('description'), 'org_id' => $this->org->_id, 'url' => RM::post('url'), 'preview_url' => RM::post('preview_url'), 'category' => \Ad::setCategories(RM::post('category')), 'image' => $img, 'type' => RM::post('type', 'article'), 'device' => RM::post('device', ['all']), 'live' => false]);
         $visibility = RM::post('visibility', 'public');
         if ($visibility === "private") {
             $campaign->meta = ['private' => true];
         }
         $permission = RM::post('permission', false);
         if ($permission == "yes") {
             $campaign->meta = ['permission' => true];
         }
         if ($expiry) {
             $campaign->expiry = $expiry;
         }
         try {
             if ($campaign->type === "video") {
                 $url = RM::post('videoUrl');
                 $ytdl = new Downloader($url);
                 $campaign->getMeta()['processing'] = true;
                 $campaign->getMeta()['videoUrl'] = $ytdl->getUrl();
             }
         } catch (\Exception $e) {
             // Invalid URL
             return $view->set("errors", ['videoUrl' => ["Pass a valid youtube video URL"]]);
         }
         if (!$campaign->validate()) {
             return $view->set("errors", $campaign->errors);
         }
         $campaign->save();
         $models = RM::post('model');
         $comm_desc = RM::post('comm_desc');
         $revenue = RM::post('revenue');
         $rate = RM::post('rate');
         $coverage = RM::post('coverage');
         foreach ($models as $key => $value) {
             $commission = new \Commission(['ad_id' => $campaign->_id, 'description' => $comm_desc[$key], 'model' => $value, 'rate' => $this->currency($rate[$key]), 'revenue' => $this->currency($revenue[$key]), 'coverage' => $coverage[$key]]);
             $commission->save();
         }
         Registry::get("session")->set('$flashMessage', 'Campaign Created successfully!!');
         $this->redirect("/campaign/manage.html");
     }
 }
Ejemplo n.º 2
0
 /**
  * @before _secure
  * @after _cleanUp
  */
 public function campaign($id = null)
 {
     $view = $this->getActionView();
     $org = $this->_org;
     $active = RequestMethods::get('active', 1);
     $fields = ['_id', 'title', 'description', 'image', 'url', 'device', 'expiry', 'created'];
     $commFields = ['model', 'rate', 'revenue', 'coverage'];
     if ($id) {
         $campaign = Ad::first(['_id' => $id, 'org_id' => $org->_id], $fields);
     } else {
         $campaign = null;
     }
     if ($id && !$campaign) {
         return $this->failure('30');
     }
     $type = RequestMethods::type();
     switch ($type) {
         case 'GET':
             if (!$id) {
                 // display list of campaigns
                 $ads = Ad::all(['org_id' => $org->_id, 'live' => $active], $fields);
                 $ads = Ad::objectArr($ads, $fields);
                 $results = [];
                 foreach ($ads as $id => $a) {
                     $arr = Utils::toArray($a);
                     $comms = Commission::all(['ad_id' => $a->_id], $commFields);
                     $arr['commissions'] = Ad::objectArr($comms, $commFields);
                     $results[$id] = (object) $arr;
                 }
                 $data = ['campaigns' => $results];
                 $view->set('data', $data);
             } else {
                 $ads = Ad::objectArr($campaign, $fields);
                 $campaign = array_shift($ads);
                 $comm = Commission::all(['ad_id' => $campaign->_id], $commFields);
                 $data = ['campaign' => $campaign, 'commissions' => Commission::objectArr($comm, $commFields)];
                 $view->set('data', $data);
             }
             break;
         case 'POST':
             if ($id) {
                 // edit a particular campaign
             } else {
                 // create a new campaign
                 $fields = ['title', 'description', 'url', 'expiry', 'category', 'device', 'user_id'];
                 $img = RequestMethods::post('image');
                 // contains image url
                 $campaign = new Ad(['org_id' => $org->_id, 'type' => 'article', 'image' => Utils::media($img, 'download')]);
                 foreach ($fields as $f) {
                     $campaign->{$f} = RequestMethods::post($f);
                 }
                 $view->set('success', false);
                 $visibility = RequestMethods::post('visibility', 'public');
                 if ($visibility === 'private') {
                     $campaign->getMeta()['private'] = true;
                 }
                 $opts = ['devices' => array_keys(Shared\Markup::devices()), 'advertisers' => $org->users('advertiser', false)];
                 if (true) {
                     // $campaign->save();
                     $arr = ArrayMethods::reArray($_POST['commissions']);
                     var_dump($arr);
                     var_dump($_POST['commissions']);
                     $view->set('success', true);
                 } else {
                     $data = ['errors' => $campaign->errors];
                     $view->set('data', $data);
                 }
             }
             break;
         case 'DELETE':
             $message = $campaign->delete();
             $view->set($message);
             break;
     }
 }