Exemple #1
0
 public static function minutely()
 {
     // process all the campaign which needs processing
     $today = date('Y-m-d');
     $ads = \Ad::all(['meta.processing' => true, 'created' => Db::dateQuery($today, $today)]);
     foreach ($ads as $a) {
         $meta = $a->meta;
         switch ($a->type) {
             case 'video':
                 // download the video and save it
                 $arr = [];
                 $found = false;
                 foreach (['240p', '360p'] as $q) {
                     $vid = Utils::media($a->meta['videoUrl'], 'download', ['type' => 'video', 'quality' => $q]);
                     if (strlen($vid) >= 4) {
                         $found = true;
                         $arr[] = ['file' => $vid, 'quality' => $q];
                     }
                 }
                 if ($found) {
                     unset($meta['processing']);
                 }
                 $meta['videos'] = $arr;
                 $a->meta = $meta;
                 $a->save();
                 break;
         }
     }
 }
Exemple #2
0
 public static function customFields($user, $org)
 {
     $afields = \Meta::search('customField', $org);
     if (count($afields) > 0) {
         $meta = $user->meta ?? [];
         $extraFields = [];
         foreach ($afields as $value) {
             $key = $value['name'];
             $type = $value['type'];
             $message = $value['label'] . " is required!!";
             switch ($type) {
                 case 'file':
                     $v = Utils::media($key, 'upload', ['extension' => 'jpe?g|gif|bmp|png|tif|pdf']);
                     if (!$v) {
                         $message = "Please Upload a valid image or pdf file";
                     }
                     break;
                 case 'text':
                     $v = RequestMethods::post($key);
                     break;
                 case 'date':
                     $d = RequestMethods::post($key, date('Y-m-d'));
                     $v = Db::convertType($d, 'date');
                     break;
                 default:
                     $v = '';
                     break;
             }
             if (!$v && $value['required']) {
                 return ["message" => $message, "success" => false];
             }
             $extraFields[$key] = $v;
         }
         $meta['afields'] = $extraFields;
         $user->meta = $meta;
     }
     $user->save();
     return ["success" => true];
 }
Exemple #3
0
 public function removeFields()
 {
     $meta = $this->_meta;
     $afields = $meta['afields'] ?? [];
     foreach ($meta['afields'] as $key => $value) {
         Utils::media($value, 'remove');
     }
 }
Exemple #4
0
 /**
  * @before _secure
  * @after _csrfToken
  */
 public function settings()
 {
     $this->seo(array("title" => "Settings"));
     $view = $this->getActionView();
     $user = $this->user;
     $org = $this->org;
     $search = ['prop' => 'customField', 'propid' => $org->_id];
     $meta = Meta::first($search) ?? (object) [];
     $view->set('fields', $meta->value ?? []);
     $apikey = ApiKey::first(["org_id = ?" => $org->id]);
     $mailConf = Meta::first(['prop' => 'orgSmtp', 'propid' => $this->org->_id]) ?? (object) [];
     $view->set('mailConf', $mailConf->value ?? [])->set("errors", []);
     if (RM::type() == 'POST') {
         $action = RM::post('action', '');
         switch ($action) {
             case 'account':
                 $user->name = RM::post('name');
                 $user->currency = RM::post('currency', 'INR');
                 $user->region = ["currency" => RM::post('currency', 'INR'), "zone" => RM::post('timezone', 'Asia/Kolkata')];
                 $user->phone = RM::post('phone');
                 $user->save();
                 $view->set('message', 'Account Updated!!');
                 break;
             case 'password':
                 $old = RM::post('password');
                 $new = RM::post('npassword');
                 $view->set($user->updatePassword($old, $new));
                 break;
             case 'billing':
                 $billing = $org->billing;
                 $billing["aff"]["auto"] = RM::post("autoinvoice", 0);
                 $billing["aff"]["freq"] = RM::post("freq", 15);
                 $billing["aff"]["minpay"] = $this->currency(RM::post('minpay', 100));
                 $billing["aff"]["ptypes"] = RM::post("ptypes");
                 $billing["adv"]["paypal"] = RM::post("paypal");
                 $org->billing = $billing;
                 $org->save();
                 $this->setOrg($org);
                 $view->set('message', 'Organization Billing Updated!!');
                 break;
             case 'org':
                 $meta = $org->meta;
                 if (RM::post("widgets")) {
                     $meta["widgets"] = RM::post("widgets");
                     $org->meta = $meta;
                 }
                 $zopim = RM::post("zopim");
                 $meta["zopim"] = $zopim;
                 if (strlen($zopim) == 0) {
                     unset($meta["zopim"]);
                 }
                 $org->name = RM::post('name');
                 $org->meta = $meta;
                 $org->logo = $this->_upload('logo');
                 $org->url = RM::post('url');
                 $org->email = RM::post('email');
                 $org->save();
                 $this->setOrg($org);
                 $view->set('message', 'Network Settings updated!!');
                 break;
             case 'customField':
                 $label = RM::post("fname");
                 $type = RM::post("ftype", "text");
                 $required = RM::post("frequired", 1);
                 $name = strtolower(str_replace(" ", "_", $label));
                 $field = ['label' => ucwords($label), 'type' => $type, 'name' => $name, 'required' => (bool) $required];
                 if (!$label) {
                     break;
                 }
                 if (!is_object($meta) || !is_a($meta, 'Meta')) {
                     $meta = new Meta($search);
                 }
                 $fields = $meta->value;
                 $fields[] = $field;
                 $meta->value = $fields;
                 $meta->save();
                 $view->set('fields', $meta->value ?? []);
                 $view->set('message', 'Extra Field Added!!');
                 break;
             case 'smtp':
                 $msg = \Shared\Services\Smtp::create($this->org);
                 $view->set('message', $msg);
                 break;
             case 'apikey':
                 $view->set('message', "Api Key Updated!!");
                 if (!$apikey) {
                     $apikey = new ApiKey(['org_id' => $this->org->_id, 'key' => uniqid() . uniqid() . uniqid()]);
                     $view->set('message', "Api Key Created!!");
                 }
                 $apikey->updateIps();
                 $apikey->save();
                 break;
         }
         $this->setUser($user);
     }
     $view->set("apiKey", $apikey);
     if (RM::type() === 'DELETE') {
         if (is_a($meta, 'Meta')) {
             $meta->delete();
         }
         $view->set('message', 'Extra Fields removed!!');
     }
     $img = RM::get("img");
     if (RM::get("action") == "removelogo" && $img === $org->logo) {
         Utils::media($org->logo, 'remove');
         $org->logo = ' ';
         $this->setOrg($org);
         $org->save();
         $this->redirect("/admin/settings.html");
     }
 }
Exemple #5
0
 /**
  * @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);
 }
Exemple #6
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;
     }
 }
Exemple #7
0
 /**
  * Removes the Media files of the campaign
  * @return [type] [description]
  */
 protected function _removeMedia()
 {
     $videos = $this->_meta['videos'] ?? [];
     foreach ($videos as $v) {
         Utils::media($v['file'], 'remove', ['type' => 'video']);
     }
     Utils::media($this->image, 'remove');
 }