Example #1
0
 static function createBy(User $user, array $info)
 {
     $fields = array('name', 'surname', 'address', 'phone', 'notes');
     $fields = userFields($fields, 'order');
     $order = with(new static())->fill_raw(array_intersect_key($info, array_flip($fields)))->fill_raw(array('password' => static::generatePassword(), 'user' => $user->id, 'manager' => \Vane\Current::config('general.new_order_manager'), 'sum' => Cart::subtotal(), 'ip' => Request::ip()));
     return Event::insertModel($order, 'order');
 }
Example #2
0
 static function findOrCreate(array $info)
 {
     static $fields = array('name', 'surname', 'phone', 'email');
     $model = User::where('email', '=', $info['email'])->first();
     if ($model) {
         return $model;
     } else {
         $password = static::generatePassword();
         $model = with(new User())->fill_raw(array_intersect_key($info, array_flip($fields)));
         // it won't be hashed if set via fill_raw().
         $model->password = $password;
         $model->reg_ip = Request::ip();
         $model = Event::insertModel($model, 'user');
         return S::listable(compact('model', 'password'));
     }
 }
Example #3
0
 function ajax_post_reg()
 {
     $rules = array('name' => 'required', 'surname' => 'required', 'phone' => 'required|min:7|vanemart_phone', 'email' => 'required|email', 'password' => static::PASSWORD_RULE, 'referee' => 'integer');
     $rules += (array) \Vane\Current::config('general.user_fields.user');
     $valid = Validator::make($this->in(), $rules);
     $email = $this->in('email', null);
     if ($email and User::where('email', '=', $email)->count()) {
         return Validator::withError('email', 'vanemart::taken');
     } elseif ($valid->fails()) {
         return $valid;
     } else {
         $user = new User(Input::only(array_keys($rules)));
         $user->referee = null;
         $user->reg_ip = Request::ip();
         if ($referee = $this->in('referee', 0)) {
             $referee = User::where('id', '=', $referee)->or_where('email', '=', $referee)->first();
             $referee and $user->referee = $referee->id;
         }
         return Event::insertModel($user, 'user');
     }
 }
Example #4
0
 static function place($file, array $attributes = array())
 {
     $attributes += array('uploader' => null, 'desc' => '', 'mime' => null, 'name' => null, 'ext' => null);
     if (!empty($attributes['name'])) {
         $attributes['name'] = basename($attributes['name']);
     } else {
         $ext = strtolower(ltrim($attributes['ext'], '.'));
         $attributes['name'] = substr(uniqid(), 0, 8) . ".{$ext}";
         $msg = "Placing a file without explicit name, generated random: {$attributes['name']}.";
         Log::info_File($msg);
     }
     $ext = $attributes['ext'] = strtolower(ltrim(S::ext($attributes['name']), '.'));
     $ext === '' and $attributes['ext'] = 'dat';
     $dest = static::generatePath($attributes['name']);
     S::mkdirOf($dest);
     $attributes['path'] = S::tryUnprefix($dest, static::storage());
     if (is_resource($file)) {
         $attributes['size'] = static::streamCopyTo($dest, $file);
     } else {
         $attributes['size'] = strlen($file);
         if (!file_put_contents($dest, $file, LOCK_EX)) {
             throw new Error("Cannot write new File data [{$dest}].");
         }
     }
     try {
         // explicit ID so it's harder to guess new file's ID (e.g. to access it directly
         // from web) since they're not sequental.
         $attributes['id'] = static::generateID();
         $model = with(new static())->fill_raw($attributes);
         $model->md5 = md5_file($dest);
         $model->mime = $attributes['mime'] ?: \File::mime($model->ext, '');
         return Event::insertModel($model, 'file');
     } catch (\Exception $e) {
         unlink($dest);
         throw $e;
     }
 }
Example #5
0
 function ajax_post_add($type = null, $object = null)
 {
     $object = (int) $object;
     $parent = $this->in('parent', 0) ?: null;
     $eventOptions = compact('type', 'object') + array('block' => $this);
     if (!$this->in('body', '')) {
         $body = Event::until('post.bodyless', array($eventOptions));
         if (isset($this->input)) {
             $this->input['body'] = $body;
         } else {
             Input::merge(compact('body'));
         }
     }
     $valid = Validator::make($this->in(), array('parent' => 'int', 'title' => 'max:50', 'body' => 'required'));
     if (!$type) {
         return E_INPUT;
     } elseif (!$this->accessible('add', $object, $parent)) {
         return false;
     } elseif ($valid->fails()) {
         return $valid;
     } else {
         $model = with(new Post())->fill_raw(Input::only(array('title', 'body')))->fill_raw(compact('type') + array('object' => $object, 'parent' => $parent, 'flags' => $this->can('manager') ? 'manager' : '', 'html' => format('post', $this->in('body')), 'author' => $this->user()->id, 'ip' => Request::ip()));
         $eventOptions['post'] = $model = Event::insertModel($model, 'post');
         if (!$model) {
             return E_SERVER;
         }
         try {
             $attachments = array();
             Event::fire('post.attach', array(&$attachments, $eventOptions));
         } catch (\Exception $e) {
             $model->delete();
             throw $e;
         }
         $eventOptions += compact('attachments');
         Event::fire('post.added', array($eventOptions));
         if ($type and $object and $object = $model->object()->first()) {
             Event::fire('post.object_ref', array(&$object, $eventOptions));
             $object instanceof LaravelModel and $object->save();
         }
         return $model;
     }
 }