This repository was archived by the owner on Jun 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathContent.php
More file actions
173 lines (149 loc) · 5.03 KB
/
Content.php
File metadata and controls
173 lines (149 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
namespace ragaga\yii2\content;
use yii\base\InvalidConfigException;
class Content extends \yii\base\Module
{
/**
* @var array List Chars for russian word
*/
private static $rustable =array(
'а' => 'a', 'б' => 'b', 'в' => 'v',
'г' => 'g', 'д' => 'd', 'е' => 'e',
'ё' => 'e', 'ж' => 'zh', 'з' => 'z',
'и' => 'i', 'й' => 'y', 'к' => 'k',
'л' => 'l', 'м' => 'm', 'н' => 'n',
'о' => 'o', 'п' => 'p', 'р' => 'r',
'с' => 's', 'т' => 't', 'у' => 'u',
'ф' => 'f', 'х' => 'h', 'ц' => 'c',
'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sch',
'ь' => '', 'ы' => 'y', 'ъ' => '',
'э' => 'e', 'ю' => 'yu', 'я' => 'ya',
'А' => 'A', 'Б' => 'B', 'В' => 'V',
'Г' => 'G', 'Д' => 'D', 'Е' => 'E',
'Ё' => 'E', 'Ж' => 'Zh', 'З' => 'Z',
'И' => 'I', 'Й' => 'Y', 'К' => 'K',
'Л' => 'L', 'М' => 'M', 'Н' => 'N',
'О' => 'O', 'П' => 'P', 'Р' => 'R',
'С' => 'S', 'Т' => 'T', 'У' => 'U',
'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C',
'Ч' => 'Ch', 'Ш' => 'Sh', 'Щ' => 'Sch',
'Ь' => '', 'Ы' => 'Y', 'Ъ' => '',
'Э' => 'E', 'Ю' => 'Yu', 'Я' => 'Ya',
);
public $controllerNamespace = 'ragaga\yii2\content\controllers';
/**
* @var string $imageDir - Папка для хранения картинок
*/
public $imageDir;
/**
* @var string $imageUrl - Путь до картинки для веба
*/
public $imageUrl;
/**
* @var array Model classes, e.g., ["Content" => "ragnarek\models\Content"]
* Usage:
* $user = Yii::$app->getModule("content")->model("Content", $config);
* (equivalent to)
* $user = new ragnarek\models\Content($config);
*
* The model classes here will be merged with/override the [[getDefaultModelClasses()|default ones]]
*/
public $modelClasses = [] ;
/**
* @var array Storage for models based on $modelClasses
*/
protected $_models;
/**
* @var int Short text length
*/
public $shortTextLength = 250;
public function init()
{
parent::init();
$this->checkModuleProperties();
$this->modelClasses = array_merge($this->getDefaultModelClasses(), $this->modelClasses);
if (empty(\Yii::$app->i18n->translations['content'])) {
\Yii::$app->i18n->translations['content'] = [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => __DIR__ . '/messages',
//'forceTranslation' => true,
];
}
}
/**
* Check for valid module properties
*/
protected function checkModuleProperties()
{
$className = get_called_class();
if(!$this->imageDir){
throw new InvalidConfigException("{$className}: \$imageDir must be defined");
}
$this->imageDir = \Yii::getAlias($this->imageDir);
if(!file_exists($this->imageDir)){
throw new InvalidConfigException("{$className}: Directory {$this->imageDir} is not exist");
}
if(!is_writable($this->imageDir)){
throw new InvalidConfigException("{$className}: Directory {$this->imageDir} must be writable");
}
}
/**
* Get object instance of model
*
* @param string $name
* @param array $config
* @return ActiveRecord
*/
public function model($name, $config = [])
{
// return object if already created
if (!empty($this->_models[$name])) {
return $this->_models[$name];
}
// create model and return it
$className = $this->modelClasses[ucfirst($name)];
$this->_models[$name] = \Yii::createObject(array_merge(["class" => $className], $config));
return $this->_models[$name];
}
/**
* Get default model classes
*/
protected function getDefaultModelClasses()
{
// use single quotes so nothing gets escaped
return [
'Content' => 'ragaga\yii2\content\models\Content',
];
}
/**
* @param null $str
* @param string $spacechar
* @return mixed|null|string
*/
public static function rus2trans($str = null, $spacechar = '_')
{
if ($str)
{
$str = strtolower(strtr($str, self::$rustable));
$str = preg_replace('~[^-a-z0-9_]+~u', $spacechar, $str);
$str = trim($str, $spacechar);
return $str;
} else {
return;
}
}
/**
* Substring string by space
* @param $str
* @param $count
* @return string
*/
public function subString($str, $count = 255,$endChars = '...') {
if (strlen($str)>$count){
$substring = mb_substr($str, 0, $count);
$lastSpace = mb_strrpos($substring,' ');
return mb_substr($substring,0,$lastSpace).$endChars;
}
return $str;
}
}