public function testMongoID() { $object = new \MongoDB\BSON\ObjectID(); $expected = Utils::getMongoID($object); $known = $object->__toString(); $this->assertEquals($known, $expected, 'ObjectID is not converted to string'); $this->assertInstanceOf('MongoDB\\BSON\\ObjectID', Utils::mongoObjectId($expected), 'Failed to convert it to bson object'); }
public function inUse() { $count = Ad::count(['org_id' => $this->org_id, 'category' => ['$elemMatch' => ['$eq' => Utils::mongoObjectId($this->_id)]]]); if ($count === 0) { return false; } else { return true; } }
public static function campImport($uid, $advert_id, $urls, $extra = []) { $uid = Utils::mongoObjectId($uid); $advert_id = Utils::mongoObjectId($advert_id); $data = ['prop' => 'campImport', 'propid' => $uid, 'value' => ['advert_id' => $advert_id, 'urls' => $urls]]; if (!empty($extra)) { $data['value']['campaign'] = $extra; } $meta = new self($data); $meta->save(); return $meta; }
public static function convertType($value, $type = 'id') { switch ($type) { case 'id': return Utils::mongoObjectId($value); case 'regex': return Utils::mongoRegex($value); case 'date': case 'datetime': case 'time': return self::time($value); } return ''; }
/** * @before _secure * @after _displayData */ public function platforms($id = null) { $this->seo(["title" => "Platform wise click stats"]); $view = $this->getActionView(); $org = $this->org; $clickCol = Registry::get("MongoDB")->clicks; // find the platforms $platforms = \Platform::all(['user_id' => ['$in' => $org->users('advertisers')]], ['_id', 'url']); if (count($platforms) === 0) { return $view->set(['platforms' => [], 'publishers' => []]); } $key = array_rand($platforms); $url = RM::get('link', $platforms[$key]->url); // find ads having this url $ads = \Ad::all(['org_id' => $org->_id], ['_id', 'url']); $in = Utils::mongoObjectId(array_keys($ads)); $matched = []; foreach ($ads as $a) { $regex = preg_quote($url, '.'); if (preg_match('#^' . $regex . '#', $a->url)) { $matched[] = Utils::mongoObjectId($a->_id); } } if (count($matched) === 0) { $query['adid'] = ['$in' => $in]; } else { $query['adid'] = ['$in' => $matched]; } $query['is_bot'] = false; $query['created'] = Db::dateQuery($this->start, $this->end); $records = $clickCol->aggregate([['$match' => $query], ['$projection' => ['_id' => 1, 'pid' => 1]], ['$group' => ['_id' => '$pid', 'count' => ['$sum' => 1]]], ['$sort' => ['count' => -1]]]); $result = []; $publishers = []; foreach ($records as $r) { $obj = (object) $r; $id = Utils::getMongoID($obj->_id); $user = User::first(['_id' => $id], ['_id', 'name']); $result[$id] = (object) ['_id' => $user->_id, 'name' => $user->name, 'clicks' => $obj->count]; } $view->set(['platforms' => $platforms, 'link' => $url, 'publishers' => $result]); }
/** * @important | @core function * Specific types are needed for MongoDB for proper querying * @param misc $value * @param string $type */ public function _convertToType($value, $type) { if (Db::isType($value, 'regex')) { return $value; } switch ($type) { case 'text': $value = (string) $value; break; case 'integer': $value = (int) $value; break; case 'boolean': $value = (bool) $value; break; case 'decimal': $value = (double) $value; break; case 'datetime': case 'date': if (is_array($value)) { break; } else { if (is_object($value)) { $date = $value; if (Db::isType($value, 'date')) { break; } else { if (is_a($value, 'DateTime')) { $date = $value->format('Y-m-d'); } } $value = Db::time($date); } else { $value = Db::time($value); } } break; case 'autonumber': case 'mongoid': if (Db::isType($value, 'id')) { break; } else { if (is_array($value)) { $copy = $value; $value = []; foreach ($copy as $key => $val) { $value[$key] = Utils::mongoObjectId($val); } } else { $value = Utils::mongoObjectId($value); } } break; case 'array': if (!is_array($value)) { $value = (array) $value; } break; default: $value = $value; break; } return $value; }
public function users($type = "publisher", $object = true) { $users = \User::all(["org_id = ?" => $this->_id, "type = ?" => $type], ["_id"]); $ids = array_keys($users); if ($object === true) { return Utils::mongoObjectId($ids); } else { if ($object === 'users') { return $users; } else { if ($object === false) { return $ids; } } } }
/** * Overrides the parent delete method to check for clicks on the * ad before deleting it */ public function delete() { $id = Utils::mongoObjectId($this->_id); $count = \Click::count(['adid' => $id]); if ($count !== 0) { return ['message' => 'Can not delete!! Campaign contain clicks', 'success' => false]; } $this->_removeMedia(); parent::delete(); \Commission::deleteAll(['ad_id' => $id]); \Link::deleteAll(['ad_id' => $id]); return ['message' => 'Campaign removed successfully!!', 'success' => true]; }