Закончил картинки в статьях - нужно порефакторить, но уже работает

This commit is contained in:
Krivchikov Dmitry 2015-08-30 15:55:49 +03:00
parent d204f18353
commit 88e9dffd51
9 changed files with 168 additions and 5 deletions

View File

@ -53,6 +53,38 @@ class Helper
return $string;
}
public static function replaceImgs($string, $replaceType = 'clear')
{
preg_match_all("(\[img[\|]#(\d+)\])", $string, $images);
$images = isset($images[0]) ? $images[0] : FALSE;
if ($images) {
foreach ($images as $img) {
$imgAndId = mb_substr($img, 2, -1);
$imgAndId = explode('#', $imgAndId);
$imgId = $imgAndId[1];
$imgObj = ArticlePhotos::model()->getByPK($imgId);
if ($imgObj) {
switch ($replaceType) {
case 'html':
$imgObj->caption = $linkTitle = str_replace('"', 'ʺ', $imgObj->caption);
$imgTag = '<div style="text-align: center;"><img src="/photos/articles/middle/' . $imgObj->name . '" title="' . $imgObj->caption . '" class="imageInArticle" /></div>';
$string = str_replace($img, $imgTag, $string);
break;
default:
$imgTag = '';
$string = str_replace($img, $imgTag, $string);
break;
}
}
}
}
return $string;
}
public static function buildOG($point)
{
$og = array();

View File

@ -29,15 +29,17 @@ class Image
* Создаёт все требуемые размеры изображения и раскладывает их по папкам.
* @param type $imgName
*/
public static function createAllSizes($imgName)
public static function createAllSizes($imgName, $noWatermark = false, $folder = '')
{
$sizes = App::getConfig('images');
$pathInfo = pathinfo($_SERVER['SCRIPT_FILENAME']);
$uploaddir = $pathInfo['dirname'] . '/photos/';
$uploaddir = $pathInfo['dirname'] . '/photos/' . $folder;
$watermarkDir = App::getBasedir() . 'watermarks/';
foreach ($sizes as $dir => $size) {
$watermark = isset($size[3]) ? $watermarkDir . $size[3] : '';
$watermark = $noWatermark ? '' : $watermark;
self::imgResize(
$uploaddir . $imgName, // Source - original image
$uploaddir . $dir . '/' . $imgName, // Destination

View File

@ -154,8 +154,12 @@ abstract class Model
$calledClass = get_called_class();
while ($res && $row = $res->fetch(PDO::FETCH_ASSOC)) {
if (isset($params['noKeys'])) {
$ready[] = isset($params['asArray']) && $params['asArray'] ? $row : new $calledClass($row);
} else {
$ready[$row[$this->_primaryKey_]] = isset($params['asArray']) && $params['asArray'] ? $row : new $calledClass($row);
}
}
return (isset($params['limit']) && $params['limit'] == 1) ? array_shift($ready) : $ready;
}

View File

@ -45,4 +45,32 @@ class ImagesController extends Controller
// print $photo;
}
static function actionLoadPhoto()
{
if (isset($_FILES) && !empty($_FILES) && App::$user) {
$userId = App::$user->id;
$uploaddir = App::getBasedir() . '../public/photos/articles/';
$fileName = (string)$_FILES['photo']['name'];
$temp = pathinfo($fileName);
$extension = strtolower($temp['extension']) ? strtolower($temp['extension']) : 'jpg';
$photo = $userId . strtolower('_' . substr(md5($fileName . time()), 0, 5) . '.' . $extension);
if (move_uploaded_file($_FILES['photo']['tmp_name'], $uploaddir . $photo)) {
ArticlePhotos::model()->add(array(
'name' => $photo,
'owner' => $userId,
'caption' => isset($_POST['caption'])?$_POST['caption']:'',
'uploaded' => time(),
));
Image::createAllSizes($photo, true, 'articles/');
die($photo);
}
} else {
App::error404();
}
die;
}
}

View File

@ -413,4 +413,22 @@ class JsonController extends Controller
));
}
static function actionGetmyartphotos()
{
if (!App::$user) {
App::error404();
}
self::renderPartial('json.php', array(
'data' => ArticlePhotos::model()->getAll(
array(
'where'=>'`owner` = '.(int) App::$user->id,
'order' => '`id` DESC',
'asArray'=>true,
'noKeys'=>true
)
)
));
}
}

View File

@ -193,7 +193,9 @@ class PageController extends Controller
}
App::setConfig('title', $article->title . ' - wikipoints.ru');
$article->text = nl2br(Helper::replaceLinks($article->text, 'html'));
$article->text = Helper::replaceLinks($article->text, 'html');
$article->text = Helper::replaceImgs($article->text, 'html');
$article->text = nl2br($article->text);
self::render('article.php', array(
'article' => $article,

View File

@ -0,0 +1,18 @@
<?php
class ArticlePhotos extends Model
{
function __construct($fromArray = array())
{
$this->_tableName_ = 'articlePhotos';
parent::__construct($fromArray);
$this->setRelation('authorUser', 'owner', 'User', 'id', self::TO_ONE);
}
static function model()
{
return new self();
}
}

View File

@ -1,3 +1,43 @@
<script>
function updatePhotobank() {
$.getJSON("/json/getmyartphotos", function (data) {
$('#photobank').html('');
$.each(data, function (key, val) {
$("<img/> ", {
"class": "my-new-list",
src: '/photos/articles/small/' + val.name,
title: 'img #'+val.id+' :: '+val.caption,
photoId: val.id
}).appendTo('#photobank');
});
$("#photobank img").on("click", function () {
articleText = document.getElementById('articleText');
imgText = '\n[img|#'+$(this).attr('photoid')+']\n';
insertAtCursor(articleText, imgText);
});
});
}
function insertAtCursor(myField, myValue) {
//IE support
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
//MOZILLA and others
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)
+ myValue
+ myField.value.substring(endPos, myField.value.length);
} else {
myField.value += myValue;
}
}
</script>
<div id="pageSmoothBackground" style="background-image: url(<?= $randomPoint->getImg('middle') ?>);"></div>
<div class="container" style="background-color: rgba(255,255,255, 0.8);margin-top: -10px;padding-bottom: 30px;">
@ -18,7 +58,7 @@
<div class="form-group">
<label for="articleText">Текст статьи</label>
<textarea name="text" id="articleText" class="form-control" rows="18"><?= $article->text ?></textarea>
<textarea name="text" id="articleText" class="form-control" rows="10"><?= $article->text ?></textarea>
</div>
<div class="checkbox">
@ -31,5 +71,22 @@
</form>
</div>
</div>
<div class="col-md-12" style="margin: 20px 0; border-top: solid 1px #bbb; border-bottom: solid 1px #bbb;">
<form enctype="multipart/form-data" id="articlePhotoloader" action="/images/loadPhoto" method="POST" name="loadavatar" target="hiddenframe" class="form-inline">
<input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
<div class="form-group">
<label for="photo">Загрузить новое изображение</label>
<input id="photo" name="photo" type="file" class="form-control" />
</div>
<div class="form-group">
<input id="caption" name="caption" type="text" class="form-control" placeholder="Описание (по желанию)" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-sm">Загрузить на сервер</button>
</div>
</form>
</div>
<div class="col-md-12" id="photobank"></div>
<iframe id=hiddenframe name="hiddenframe" style="width:0px; height:0px; border:0px" onload="updatePhotobank()"></iframe>
</div>
</div>

View File

@ -92,6 +92,8 @@ div#box {background-image: url('/img/back.png'); background-repeat: no-repeat; b
.pagePoinsNearCards {display: inline-block; overflow: hidden; padding: 0 2px 4px 2px;width: 186px; height: 139px;}
.pagePoinsNearCardsImg {width: 186px; height: 139px;}
#photobank img {margin: 2px; border: solid 1px white;}
img.imageInArticle {border: solid 3px white;}
/* -------------------------------------------*/
/* ------------- ADAPTIVE --------------------*/