public static function upload(UploadedFile $file, $user, $contest = null) { $entry = new static(); DB::transaction(function () use($entry, $file, $user, $contest) { $entry->save(); // get id $entry->user()->associate($user); $entry->contest()->associate($contest); $entry->filesize = $file->getClientSize(); $entry->original_filename = $file->getClientOriginalName(); $entry->storeFile($file->getRealPath(), $file->getClientOriginalExtension()); $entry->save(); }); return $entry; }
/** * Creates token * @param string $type Type of the token * @param string $name Name of the token unique for selected type * @param callable|AlgorithmInterface $algorithm Algorithm of the token generation * @param int|\DateTime|Expression $expire Expiration date of the token * @return static */ public static function create($type, $name, $algorithm = null, $expire = null) { if ($algorithm === null) { $algorithm = new RandomString(); } $token = static::findOne(['type' => $type, 'name' => $name]); if ($token == null) { $token = new static(); } $token->type = $type; $token->name = $name; if (is_callable($algorithm)) { $token->token = call_user_func($algorithm); } else { $token->token = $algorithm->generate(); } if (is_integer($expire)) { $token->expire_at = \DateTime::createFromFormat('U', $expire, new \DateTimeZone('UTC'))->setTimezone(new \DateTimeZone('MSK'))->format('Y-m-d H:i:s'); } elseif ($expire instanceof \DateTime) { $token->expire_at = $expire->format('Y-m-d h:i:s'); } else { $token->expire_at = $expire; } $token->created_at = new Expression('NOW()'); $token->save(false); return $token; }
public static function register($data) { static::$error = false; $user = new static(); if (!$user->set($data)->success()) { static::$error = 'REGISTER_VALIDATION_FALSE'; return false; } $login = isset($data[static::ROW_LOGIN]) ? $data[static::ROW_LOGIN] : ''; if (!$login) { static::$error = 'REGISTER_NO_LOGIN'; return false; } $found = static::findBy(static::ROW_LOGIN, $login)->getFirst(); if ($found) { static::$error = 'REGISTER_DUBLICATE_USER'; return false; } $password = isset($data[static::ROW_PASSWORD]) ? $data[static::ROW_PASSWORD] : ''; if (!$password) { static::$error = 'REGISTER_NO_PASSWORD'; return false; } $user->{static::ROW_HASH_RESTORE} = static::makeHash(); $user->save(); return $user; }
/** * Set an API statistic in the DB * * @param int $code The HTTP status code from the request or cache * @param string $uri The dynamic call URL or the static call name * @param string $api The API to set the stats for * * @return bool True if the stat was added/updated successfully, or false if it wasn't. */ public static function set_stat($code, $uri, $api = null, $is_static = null) { // Save queries if we don't need the stats. if (\Config::get('engine.track_usage_stats', false) === false) { return true; } $api_name = $api === null ? \V1\APIRequest::get('api') : $api; $is_static = $is_static === null ? \V1\APIRequest::is_static() : $is_static; $api = \V1\Model\APIs::get_api($api_name); // Do we have a stat entry for this timeframe? $existing_api_stats_obj = static::query()->where('apis_id', '=', $api['id'])->where('code', '=', (int) $code)->where('call', '=', $uri)->where('created_at', '>', time() - (int) \Config::get('api_stats_increment', 30) * 60)->get_one(); // If we have a row, update it. if (!empty($existing_api_stats_obj)) { $existing_api_stats_obj->count++; return $existing_api_stats_obj->save(); } // Add the new entry. $api_stats_object = new static(); $api_stats_object->apis_id = $api['id']; $api_stats_object->code = $code; $api_stats_object->count = 1; $api_stats_object->call = $uri; $api_stats_object->is_static = intval($is_static); return $api_stats_object->save(); }
public static function createFolder(array $attributes = []) { $dir = new static(); $dir->is_dir = 1; $dir->user_id = isset($attributes['user_id']) ? $attributes['user_id'] : 0; $dir->project_id = isset($attributes['project_id']) ? $attributes['project_id'] : 0; $dir->parent_id = isset($attributes['parent_id']) ? $attributes['parent_id'] : 0; $dir->name = isset($attributes['name']) ? $attributes['name'] : '新建文件夹'; $dir->type = isset($attributes['type']) ? $attributes['type'] : 'dir'; $dir->path = isset($attributes['path']) ? $attributes['path'] : '/'; $dir->save(); $status = true; if (isset($attributes['path']) || $dir->parent_id == 0) { $dir->path .= $dir->id . '/'; } else { $path = $dir->parent()->where('project_id', $dir->project_id)->where('is_dir', 1)->pluck('path'); if (!$path || $path->count() == 0) { $dir->delete(); $status = false; } else { if (!is_string($path)) { $path = $path[0]; } $dir->path = $path . $dir->id . '/'; } } if ($status) { $dir->save(); return static::find($dir->id); } }
/** * Set the new credentials for the remote API access on the given account * * @param int $account The ID of the account * @param string $credentials The encrypted credentials to set * * @return boolean True if it saved, or false if it didn't */ public static function set_credentials($account, $credentials) { $account_meta_obj = static::query()->where('account_id', '=', $account)->where('key', '=', 'credentials')->get_one(); if (empty($account_meta_obj)) { $account_meta_obj = new static(); $account_meta_obj->account_id = $account; $account_meta_obj->key = 'credentials'; $account_meta_obj->value = $credentials; return $account_meta_obj->save(); } $account_meta_obj->value = $credentials; return $account_meta_obj->save(); }
public static function saveWithCheck(Remain $remain, $post) { if (empty($remain)) { $remain = new static(); $remain->__consignment_id = $post->__consignment_id; $remain->__place_id = $post->__place_to_id; $remain->qty = $post->qty; $remain->save(); } else { $remain->qty += $post->qty; $remain->save(); } }
public static function upload($filePath, $user, $forum = null) { $cover = new static(); DB::transaction(function () use($cover, $filePath, $user, $forum) { $cover->save(); // get id $cover->user()->associate($user); $cover->forum()->associate($forum); $cover->storeFile($filePath); $cover->save(); }); return $cover; }
public static function __create() { $item = new static(); $item->name = 'Small Health Potion'; $item->description = 'Restores a small amount of health'; $item->type = static::TYPE_USABLE; $item->icon_url = 'not-found.png'; $item->equipment_url = 'not-found.png'; $item->possible_slots = 0; $item->filled_slots = 0; $item->highlight = static::HIGHLIGHT_HEALING_ITEM; $item->level = 0; $item->sockets = 0; $item->weight = 10; $item->sealing_type = 0; $item->save(); $item->id = null; $item->name = 'Small Mana Potion'; $item->description = 'Restores a small amount of mana'; $item->save(); $item->id = null; $item->name = 'Dust'; $item->description = 'Just some dust...'; $item->highlight = 0; $item->type = static::TYPE_LOOT; $item->save(); $item->id = null; $item->name = 'Slime'; $item->description = 'Just some slime...'; $item->highlight = 0; $item->type = static::TYPE_LOOT; $item->save(); $item->id = null; $item->name = 'Copper Ore'; $item->description = 'Copper Ore is a material to forge weapons and armors with if you trained Smithing at smithes all over the world'; $item->highlight = 0; $item->type = static::TYPE_MATERIAL | static::TYPE_USABLE; $item->save(); $item->id = null; $item->name = 'Sword'; $item->description = 'A normal sword'; $item->type = static::TYPE_EQUIPPABLE; $item->equipment_url = 'characters/{gender}_sword_{equipped_slot}.png'; $item->possible_slots = static::SLOT_LEFT_HAND | static::SLOT_RIGHT_HAND; $item->filled_slots = 0; $item->highlight = static::HIGHLIGHT_CASUAL; $item->sockets = 1; $item->save(); }
static function store($ref_id = "") { $row = self::where('ref_id', $ref_id)->first(); if (null == $row) { $row = new static(); $row->ref_id = $ref_id; } $row->noperacion = self::$data['noperacion']; $row->rq_decidir = self::$data['rq_decidir']; $row->moneda = self::$data['moneda']; $row->cuotas = self::$data['cuotas']; $row->monto = self::$data['monto']; $row->tarjeta = self::$data['tarjeta']; $row->banco = self::$data['banco']; $row->sps = self::$data['sps']; $row->emailcomprador = self::$data['emailcomprador']; $row->traffic = self::$data['traffic']; $row->gastos_HTL = self::$data['gastos_HTL']; $row->gastos_financieros = self::$data['gastos_financieros']; $row->bonificacion = self::$data['bonificacion']; $row->cargos_gestion = self::$data['cargos_gestion']; $row->desc_cargos_gestion = self::$data['desc_cargos_gestion']; $row->coef_descuento = self::$data['coef_descuento']; $row->uatp = self::$data['uatp']; $row->cambio = self::$data['cambio']; $row->fecha_pago = self::$data['fecha_pago']; $row->rate = self::$data['rate']; // Update $row->save(); // Reset self::$data = []; return $row->id; }
/** * Create and save a new item * @param array $data * @return Comment */ public static function create(array $data) { $item = new static(); // Check required fields foreach (self::$requiredFields as $field) { if (!isset($data[$field])) { throw new Exception("Required field {$field} not specified."); } } // Set field values foreach ($data as $key => $val) { if ($item->exists($key)) { if (empty($val)) { $val = null; } $item->set($key, $val); } } // Set auto values if they exist if ($item->exists("created_date") && !isset($data["created_date"])) { $item->set("created_date", date("Y-m-d H:i:s")); } $item->save(); return $item; }
public static function send($from, $to, $subject, $content) { if (empty($subject)) { return 'Subject must not be empty'; } if (empty($content)) { return 'Content must not be empty'; } try { if (!is_object($from)) { $from = User::findOrFail($from); } if (!is_object($to)) { $to = User::findOrFail($to); } } catch (ModelNotFoundException $e) { return false; } $message = new static(); $message->from = $from->id; $message->to = $to->id; $message->subject = $subject; $message->content = $content; $message->save(); return $message; }
public static function create(array $attributes) { $model = new static(); $model->fill($attributes); $model->save(); return $model; }
public static function create($parent_id, $data) { $resource = new static($data); $resource->parent_id = $parent_id; $resource->save($parent_id); return $resource; }
/** * Create an activity log entry. * * @param mixed * @return boolean */ public static function log($data = array()) { if (is_object($data)) { $data = (array) $data; } if (is_string($data)) { $data = array('action' => $data); } $user = Auth::user(); $activity = new static(); $activity->user_id = isset($user->id) ? $user->id : 0; $activity->content_id = isset($data['contentID']) ? $data['contentID'] : 0; $activity->content_type = isset($data['contentType']) ? $data['contentType'] : ""; $activity->action = isset($data['action']) ? $data['action'] : ""; $activity->description = isset($data['description']) ? $data['description'] : ""; $activity->details = isset($data['details']) ? $data['details'] : ""; //set action and allow "updated" boolean to replace activity text "Added" or "Created" with "Updated" if (isset($data['updated'])) { if ($data['updated']) { $activity->description = str_replace('Added', 'Updated', str_replace('Created', 'Updated', $activity->description)); $activity->action = "Updated"; } else { $activity->action = "Created"; } } if (isset($data['deleted']) && $data['deleted']) { $activity->action = "Deleted"; } //set developer flag $activity->developer = !is_null(Session::get('developer')) ? true : false; $activity->ip_address = Request::getClientIp(); $activity->user_agent = $_SERVER['HTTP_USER_AGENT']; $activity->save(); return true; }
/** @return static|bool */ public static function createForUser(UserInterface $user) { $model = new static(); $model->save(['user_id' => $user->getId(), 'phone' => $user->getPhoneNumber(), 'code' => static::genCode(), 'expires_at' => date('Y-m-d H:i:s', strtotime('+300 second')), 'active' => 1]); Assert::noMessages($model); return $model; }
/** * @param array $values * @return Folder */ public static function create($values) { $object = new static(); $object->setValues($values); $object->save(); return $object; }
public static function bulkUpdate(array $updates) { // Map out existing servers. $existing = []; foreach (static::allServers() as $server) { $existing[$server->server] = $server; } // Loop over the updates. foreach ($updates as $k => $v) { $server = null; // Get existing or create new entry. if (isset($existing[$k])) { $server = $existing[$k]; // Check for changes before updating it. $client_id = $v['client_id']; $client_secret = $v['client_secret']; if ($server->client_id !== $client_id || $server->client_secret !== $client_secret) { $server->client_id = $client_id; $server->client_secret = $client_secret; $server->save(); } } else { $server = new static(); $server->server = $k; $server->client_id = $v['client_id']; $server->client_secret = $v['client_secret']; $server->save(); } } }
public static function logMessage(AuditEntry $entry, $message, $code = 0, $file = '', $line = 0, $trace = []) { $error = new static(); $error->entry = $entry; $error->recordMessage($message, $code, $file, $line, $trace); return $error->save(false) ? $error : null; }
/** * @param string $name * @param bool $default * @param string $title * @return Model */ static function create($name, $default = false, $title = null) { $gp = new static(['permission' => $name, 'default' => $default ? '1' : '0']); if (!is_null($title)) { if (is_string($title)) { $title = is_null($title) ? ucfirst(str_replace('.', ' ', $title)) : $title; $description = glob(ROOT . S . 'engine' . S . 'Language' . S . '*' . S . 'group.json'); foreach ($description as $fd) { $content = json_decode(file_get_contents($fd), true); $content[$name] = $title; @file_put_contents($fd, json_encode($content, JSON_PRETTY_PRINT)); } } elseif (is_array($title)) { foreach ($title as $lng => $t) { $path = ROOT . S . 'engine' . S . 'Language' . S . $lng; if (!is_dir($path)) { @mkdir($path, 0777, true); } $data = [$name => $t]; @file_put_contents($path . S . 'group.json', json_encode($data, JSON_PRETTY_PRINT)); } } } if ($gp->save()) { static::updatePermissionsList(); return $gp; } return false; }
public static function create(array $attributes = []) { $model = new static($attributes); $model->date = new Carbon(); $model->save(); return $model; }
public static function create(array $attributes = []) { $field = new static($attributes); $field->save(); self::createStartingCountries($field); return $field; }
public static function createFromUpload(Upload $upload) { $firmware = new static(); $firmware->upload_id = $upload->id; $firmware->save(); return $firmware; }
public static function post($title, $description) { $job = new static(compact('title', 'description')); $job->save(); event(new JobWasPosted($job)); return $job; }
/** * @param array $values * @return self */ public static function create($values = []) { $user = new static(); $user->setValues($values); $user->save(); return $user; }
/** * 保存记录 * * @param Command $commandObj * @param $task_id * @param $action * @param $duration * @return mixed */ public static function saveRecord(Command $commandObj, $task_id, $action, $duration) { $record = new static(); $record->attributes = ['user_id' => \Yii::$app->user->id, 'task_id' => $task_id, 'status' => (int) $commandObj->getExeStatus(), 'action' => $action, 'created_at' => time(), 'command' => var_export($commandObj->getExeCommand(), true), 'memo' => var_export($commandObj->getExeLog(), true), 'duration' => $duration]; // file_put_contents('/tmp/xx', var_export($record->attributes, true).PHP_EOL.PHP_EOL, 8); return $record->save(); }
public static function afterTopicInsert($tc) { $editor = new \app\lib\Editor(['editor' => Yii::$app->params['settings']['editor']]); $content = $editor->parse($tc->content); $pa = new PhpAnalysis(); $pa->SetSource($tc->topic->title . strip_tags($content)); $pa->resultType = 2; $pa->differMax = true; $pa->StartAnalysis(); $tagNames = $pa->GetFinallyKeywords(3); $tagNames = explode(',', $tagNames); $tags = static::find()->select(['id', 'name'])->where(['in', 'name', $tagNames])->indexBy('name')->all(); foreach ($tagNames as $tn) { if (!empty($tags) && !empty($tags[$tn])) { $tag = $tags[$tn]; $tagTopic = new TagTopic(['tag_id' => $tag->id, 'topic_id' => $tc->topic_id]); $tagTopic->save(false); $tag->updateCounters(['topic_count' => 1]); } else { $tag = new static(['name' => $tn, 'topic_count' => 1]); $tag->save(false); $tagTopic = new TagTopic(['tag_id' => $tag->id, 'topic_id' => $tc->topic_id]); $tagTopic->save(false); } } }
/** @return SmsQueue */ public static function push($number, $text, array $options = []) { $q = new static(); $q->save(['phone' => $number, 'text' => $text, 'created_at' => date('Y-m-d H:i:s'), 'status' => static::STATUS_QUEUED] + $options); Assert::noMessages($q); return $q; }
/** * Update a user's profile * * @param User $user * @param $attributes * * @return static */ public static function updateProfile(User $user, array $attributes) { $profile = $user->profile; $new = false; if (is_null($profile)) { $profile = new static(); $new = true; } $profile->first_name = $attributes['first_name']; $profile->last_name = $attributes['last_name']; $profile->location = array_key_exists('location', $attributes) ? $attributes['location'] : ''; $profile->skill_id = array_key_exists('describe', $attributes) ? $attributes['describe'] : ''; $profile->profession_id = array_key_exists('profession', $attributes) ? $attributes['profession'] : ''; $profile->about = array_key_exists('about', $attributes) ? $attributes['about'] : ''; $profile->facebook = array_key_exists('facebook', $attributes) ? $attributes['facebook'] : ''; $profile->twitter = array_key_exists('twitter', $attributes) ? $attributes['twitter'] : ''; $profile->youtube = array_key_exists('youtube', $attributes) ? $attributes['youtube'] : ''; $profile->published = array_key_exists('published', $attributes) ? true : false; $profile->user_id = $user->id; // To prevent incomplete profiles from been shown, check if the main skill is missing. if (is_null($profile->skill()) or $profile->skill_id == 0) { $profile->published = false; } if ($new) { $profile->save(); $user->profile = $profile; Event::fire(new ProfileCreated($profile, $user)); } return $profile; }
public static function create($strTable, $intEntity, \Module $objModule = null, $blnForce = false) { $blnCreate = true; $objLock = null; $intTime = time(); list($strEditorType, $intEditor) = static::getCurrentEditor(); if (!$blnForce) { $objLock = static::findActiveLock($strTable, $intEntity, $objModule, true); if ($objLock === null) { $blnCreate = true; $objLock = new static(); } else { // throw an exception if there is already a lock linked to another member/user // -> this can never be the case! if ($objLock->editorType != $strEditorType || $objLock->editor != $intEditor) { throw new \Exception('Do not run EntityLockModel::create() if the corresponding entity is locked already by someone else!'); } // if the lock is linked to the current editor, the locked time is increased $blnCreate = false; } } $objLock->tstamp = $objLock->locked = $intTime; if ($blnCreate) { $objLock->dateAdded = $intTime; $objLock->parentTable = $strTable; $objLock->pid = $intEntity; $objLock->editorType = $strEditorType; $objLock->editor = $intEditor; } $objLock->save(); return $objLock; }