public function run() { $faker = Faker::create(); $log = new Stream('php://stdout'); $log->info('Start ' . __CLASS__); /** @var Phalcon\Db\AdapterInterface $database */ $database = $this->getDI()->get('db'); $database->begin(); for ($i = 0; $i <= self::TAGS_TOTAL; $i++) { $title = $faker->company; $description = $faker->text; $tags = new Tags(); $tags->name = $title; $tags->slug = \Phalcon\Tag::friendlyTitle($title); $tags->numberPosts = 0; $tags->noBounty = 'N'; $tags->noDigest = 'N'; $tags->description = $description; if (!$tags->save()) { var_dump($tags->getMessages()); $database->rollback(); die; } $log->info('tags: ' . $tags->getName()); } }
public function run() { $faker = Faker::create(); $log = new Stream('php://stdout'); $log->info('Start ' . __CLASS__); /** @var Phalcon\Db\AdapterInterface $database */ $database = $this->getDI()->get('db'); $userIds = Users::find(['columns' => 'id'])->toArray(); $database->begin(); for ($i = 0; $i <= self::POSTS_TOTAL; $i++) { $title = $faker->company; $userRandId = array_rand($userIds); $posts = new Posts(); $posts->usersId = $userIds[$userRandId]['id']; $posts->type = rand(0, 1) ? 'questions' : 'tips'; $posts->title = $title; $posts->slug = \Phalcon\Tag::friendlyTitle($title); $posts->numberViews = rand(5, 100); $posts->numberReply = rand(0, 20); $posts->content = $faker->text; $posts->sticked = 'N'; $posts->status = 'A'; $posts->locked = 'N'; $posts->deleted = 0; $posts->acceptedAnswer = 'N'; if (!$posts->save()) { var_dump($posts->getMessages()); $database->rollback(); die; } $log->info('posts: ' . $posts->getTitle()); } }
/** * Registers common function in Twig * * @param \Phalcon\Mvc\ViewInterface $view * @param \Phalcon\DiInterface $di * @param array $userFunctions */ protected function registryFunctions($view, DiInterface $di, $userFunctions = array()) { $options = array('is_safe' => array('html')); $functions = array(new \Twig_SimpleFunction('content', function () use($view) { return $view->getContent(); }, $options), new \Twig_SimpleFunction('partial', function ($partialPath) use($view) { return $view->partial($partialPath); }, $options), new \Twig_SimpleFunction('linkTo', function ($parameters, $text = null) { return \Phalcon\Tag::linkTo($parameters, $text); }, $options), new \Twig_SimpleFunction('textField', function ($parameters) { return \Phalcon\Tag::textField($parameters); }, $options), new \Twig_SimpleFunction('passwordField', function ($parameters) { return \Phalcon\Tag::passwordField($parameters); }, $options), new \Twig_SimpleFunction('hiddenField', function ($parameters) { return \Phalcon\Tag::hiddenField($parameters); }, $options), new \Twig_SimpleFunction('fileField', function ($parameters) { return \Phalcon\Tag::fileField($parameters); }, $options), new \Twig_SimpleFunction('checkField', function ($parameters) { return \Phalcon\Tag::checkField($parameters); }, $options), new \Twig_SimpleFunction('radioField', function ($parameters) { return \Phalcon\Tag::radioField($parameters); }, $options), new \Twig_SimpleFunction('submitButton', function ($parameters) { return \Phalcon\Tag::submitButton($parameters); }, $options), new \Twig_SimpleFunction('selectStatic', function ($parameters, $data = array()) { return \Phalcon\Tag::selectStatic($parameters, $data); }, $options), new \Twig_SimpleFunction('select', function ($parameters, $data = array()) { return \Phalcon\Tag::select($parameters, $data); }, $options), new \Twig_SimpleFunction('textArea', function ($parameters) { return \Phalcon\Tag::textArea($parameters); }, $options), new \Twig_SimpleFunction('form', function ($parameters = array()) { return \Phalcon\Tag::form($parameters); }, $options), new \Twig_SimpleFunction('endForm', function () { return \Phalcon\Tag::endForm(); }, $options), new \Twig_SimpleFunction('getTitle', function () { return \Phalcon\Tag::getTitle(); }, $options), new \Twig_SimpleFunction('stylesheetLink', function ($parameters = null, $local = true) { return \Phalcon\Tag::stylesheetLink($parameters, $local); }, $options), new \Twig_SimpleFunction('javascriptInclude', function ($parameters = null, $local = true) { return \Phalcon\Tag::javascriptInclude($parameters, $local); }, $options), new \Twig_SimpleFunction('image', function ($parameters) { return \Phalcon\Tag::image($parameters); }, $options), new \Twig_SimpleFunction('friendlyTitle', function ($text, $separator = null, $lowercase = true) { return \Phalcon\Tag::friendlyTitle($text, $separator, $lowercase); }, $options), new \Twig_SimpleFunction('getDocType', function () { return \Phalcon\Tag::getDocType(); }, $options), new \Twig_SimpleFunction('getSecurityToken', function () use($di) { return $di->get("security")->getToken(); }, $options), new \Twig_SimpleFunction('getSecurityTokenKey', function () use($di) { return $di->get("security")->getTokenKey(); }, $options), new \Twig_SimpleFunction('url', function ($route) use($di) { return $di->get("url")->get($route); }, $options)); if (!empty($userFunctions)) { $functions = array_merge($functions, $userFunctions); } foreach ($functions as $function) { $this->twig->addFunction($function); } }
/** * Creates a random post and return its id * * @param array $attributes Model attributes [Optional] * @return int */ public function havePost(array $attributes = []) { $I = $this; $title = $this->faker->title; $default = ['title' => $title, 'slug' => Tag::friendlyTitle($title), 'content' => $this->faker->text(), 'users_id' => $this->faker->numberBetween(), 'categories_id' => $this->faker->numberBetween()]; // do not generate slug manually if (array_key_exists('slug', $attributes) && $attributes['slug'] === false) { unset($attributes['slug'], $default['slug']); } return $I->haveRecord('Phosphorum\\Models\\Posts', array_merge($default, $attributes)); }
/** * Registers common function in Twig * * @param \Phalcon\Mvc\ViewInterface $view */ private function registryFunctions($view) { $options = array('is_safe' => array('html')); $functions = array(new \Twig_SimpleFunction('content', function () use($view) { return $view->getContent(); }, $options), new \Twig_SimpleFunction('partial', function ($partialPath) use($view) { return $view->partial($partialPath); }, $options), new \Twig_SimpleFunction('linkTo', function ($parameters, $text = null) { return \Phalcon\Tag::linkTo($parameters, $text); }, $options), new \Twig_SimpleFunction('textField', function ($parameters) { return \Phalcon\Tag::textField($parameters); }, $options), new \Twig_SimpleFunction('passwordField', function ($parameters) { return \Phalcon\Tag::passwordField($parameters); }, $options), new \Twig_SimpleFunction('hiddenField', function ($parameters) { return \Phalcon\Tag::hiddenField($parameters); }, $options), new \Twig_SimpleFunction('fileField', function ($parameters) { return \Phalcon\Tag::fileField($parameters); }, $options), new \Twig_SimpleFunction('checkField', function ($parameters) { return \Phalcon\Tag::checkField($parameters); }, $options), new \Twig_SimpleFunction('radioField', function ($parameters) { return \Phalcon\Tag::radioField($parameters); }, $options), new \Twig_SimpleFunction('submitButton', function ($parameters) { return \Phalcon\Tag::submitButton($parameters); }, $options), new \Twig_SimpleFunction('selectStatic', function ($parameters, $data = []) { return \Phalcon\Tag::selectStatic($parameters, $data); }, $options), new \Twig_SimpleFunction('select', function ($parameters, $data = []) { return \Phalcon\Tag::select($parameters, $data); }, $options), new \Twig_SimpleFunction('textArea', function ($parameters) { return \Phalcon\Tag::textArea($parameters); }, $options), new \Twig_SimpleFunction('form', function ($parameters = []) { return \Phalcon\Tag::form($parameters); }, $options), new \Twig_SimpleFunction('endForm', function () { return \Phalcon\Tag::endForm(); }, $options), new \Twig_SimpleFunction('getTitle', function () { return \Phalcon\Tag::getTitle(); }, $options), new \Twig_SimpleFunction('getTitle', function () { return \Phalcon\Tag::getTitle(); }, $options), new \Twig_SimpleFunction('stylesheetLink', function ($parameters = null, $local = true) { return \Phalcon\Tag::stylesheetLink($parameters, $local); }, $options), new \Twig_SimpleFunction('javascriptInclude', function ($parameters = null, $local = true) { return \Phalcon\Tag::javascriptInclude($parameters, $local); }, $options), new \Twig_SimpleFunction('image', function ($parameters) { return \Phalcon\Tag::image($parameters); }, $options), new \Twig_SimpleFunction('friendlyTitle', function ($text, $separator = null, $lowercase = true) { return \Phalcon\Tag::friendlyTitle($text, $separator, $lowercase); }, $options), new \Twig_SimpleFunction('getDocType', function () { return \Phalcon\Tag::getDocType(); }, $options)); foreach ($functions as $function) { $this->_twig->addFunction($function); } }
/** * Set Alias */ public function setAlias($alias = '') { if (!$alias) { $alias = \Phalcon\Tag::friendlyTitle($this->name, "-"); } $this->alias = $alias; }
public static function friendlyTitle($text, $separator = "-", $lowercase = true, $replace = null) { return parent::friendlyTitle($text, $separator, $lowercase, $replace); }
public function beforeUpdate() { $this->slug = \Phalcon\Tag::friendlyTitle($this->title); }
public function uploadByEncodedData($data, $originalName, $mimeType = null) { if (!($headPos = strpos($data, ','))) { throw new Exception\InvalidArgumentException('ERR_FILE_ENCODED_UPLOAD_FORMAT_INCORRECT'); } $fileHead = substr($data, 0, $headPos + 1); $fileEncodedData = trim(substr($data, $headPos + 1)); $data = base64_decode($fileEncodedData); $tmpName = Text::random(\Phalcon\Text::RANDOM_ALNUM, 6); $tmpPath = $this->getUploadTmpPath(); $tmp = $tmpPath . '/' . $tmpName; $adapter = new \Gaufrette\Adapter\Local($tmpPath); $filesystem = new \Gaufrette\Filesystem($adapter); $filesystem->write($tmpName, $data); $fileSize = filesize($tmp); $type = $mimeType; $filenameArray = explode(".", $originalName); $fileExtension = strtolower(array_pop($filenameArray)); $originalFileName = implode('.', $filenameArray); $fileName = Tag::friendlyTitle($originalFileName); if ($fileName == '-') { $fileName = Text::random(Text::RANDOM_ALNUM, 6); } //hash file less then 10M if ($fileSize < 1048576 * 10) { $fileHash = hash_file('CRC32', $tmp, false); } if (false === strpos($type, 'image')) { $isImage = 0; } else { $isImage = 1; } $fileinfo = array('title' => $originalFileName, 'status' => 'published', 'storageAdapter' => 'local', 'originalName' => $originalName, 'fileSize' => $fileSize, 'mimeType' => $type, 'fileExtension' => $fileExtension, 'fileHash' => $fileHash, 'isImage' => $isImage, 'fileName' => $fileName . '.' . $fileExtension, 'createdAt' => time()); if ($isImage) { $image = getimagesize($tmp); $fileinfo['imageWidth'] = $image[0]; $fileinfo['imageHeight'] = $image[1]; } $filesystem = $this->getDI()->getFileSystem(); $path = md5(time()); $path = str_split($path, 2); $pathlevel = $this->getUploadPathLevel(); $pathlevel = $pathlevel > 6 ? 6 : $pathlevel; $path = array_slice($path, 0, $pathlevel); $filePath = implode('/', $path); $path = $filePath . '/' . $fileName . '.' . $fileExtension; $fileinfo['filePath'] = $filePath; $this->assign($fileinfo); if ($this->save()) { if (!$filesystem->has($path)) { if ($filesystem->write($path, file_get_contents($tmp))) { unlink($tmp); } else { throw new Exception\IOException('ERR_FILE_MOVE_TO_STORAGE_FAILED'); } } else { throw new Exception\ResourceConflictException('ERR_FILE_UPLOAD_BY_CONFLICT_NAME'); } } else { throw new Exception\RuntimeException('ERR_FILE_SAVE_TO_DB_FAILED'); } return $this; }
$user->timezone = $faker->timezone; if (!$user->save()) { $database->rollback(); die(join(PHP_EOL, $user->getMessages())); } $log->info('User: '******'columns' => 'id'])->toArray(); $userIds = Users::find(['columns' => 'id'])->toArray(); $database->begin(); for ($i = 0; $i <= 500; $i++) { $title = $faker->company; $post = new Posts(); $post->title = $title; $post->slug = Tag::friendlyTitle($title); $post->content = $faker->text(); $userRandId = array_rand($userIds); $post->users_id = $userIds[$userRandId]['id']; $categoryRandId = array_rand($categoryIds); $post->categories_id = $categoryIds[$categoryRandId]['id']; if (!$post->save()) { $database->rollback(); die(join(PHP_EOL, $post->getMessages())); } if (!mt_rand(0, 10)) { $size = mt_rand(2, 10); $options = []; for ($j = 0; $j < $size; $j++) { $options[$j] = $faker->company; }
/** * Method to set the value of field alias * * @param string $name * @return $this */ public function setAlias($alias) { $this->alias = \Phalcon\Tag::friendlyTitle($alias); return $this; }