nazar-pc /
CleverStyle-Framework
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * @package Content |
||||
| 4 | * @category modules |
||||
| 5 | * @author Nazar Mokrynskyi <[email protected]> |
||||
| 6 | * @license 0BSD |
||||
| 7 | */ |
||||
| 8 | namespace cs\modules\Content; |
||||
| 9 | |||||
| 10 | use |
||||
| 11 | cs\Cache\Prefix, |
||||
| 12 | cs\Config, |
||||
| 13 | cs\Language, |
||||
| 14 | cs\CRUD_helpers, |
||||
| 15 | cs\Singleton; |
||||
| 16 | |||||
| 17 | /** |
||||
| 18 | * @method static $this instance($check = false) |
||||
| 19 | */ |
||||
| 20 | class Content { |
||||
| 21 | use |
||||
| 22 | CRUD_helpers, |
||||
| 23 | Singleton; |
||||
| 24 | |||||
| 25 | protected $data_model = [ |
||||
| 26 | 'key' => 'text', |
||||
| 27 | 'title' => 'ml:text', |
||||
| 28 | 'content' => 'ml:', |
||||
| 29 | 'type' => 'set:text,html' |
||||
| 30 | ]; |
||||
| 31 | protected $table = '[prefix]content'; |
||||
| 32 | protected $data_model_ml_group = 'Content'; |
||||
| 33 | protected $data_model_files_tag_prefix = 'Content'; |
||||
| 34 | /** |
||||
| 35 | * @var Prefix |
||||
| 36 | */ |
||||
| 37 | protected $cache; |
||||
| 38 | |||||
| 39 | protected function construct () { |
||||
| 40 | $this->cache = new Prefix('Content'); |
||||
| 41 | } |
||||
| 42 | /** |
||||
| 43 | * @inheritdoc |
||||
| 44 | */ |
||||
| 45 | protected function cdb () { |
||||
| 46 | return Config::instance()->module('Content')->db('content'); |
||||
| 47 | } |
||||
| 48 | /** |
||||
| 49 | * Add new content |
||||
| 50 | * |
||||
| 51 | * @param string $key Key associated with content, works like id |
||||
| 52 | * @param string $title Content title |
||||
| 53 | * @param string $content Content itself |
||||
| 54 | * @param string $type Type of content: <b>text</b> or <b>html</b>. Influences on editor type |
||||
| 55 | * |
||||
| 56 | * @return bool |
||||
| 57 | */ |
||||
| 58 | public function add ($key, $title, $content, $type) { |
||||
| 59 | $key = str_replace(['/', '?', '#', '"', '<', '>'], '_', $key); |
||||
| 60 | $result = $this->create($key, $title, $content, $type); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 61 | if ($result) { |
||||
| 62 | $this->clean_cache($key); |
||||
| 63 | } |
||||
| 64 | return (bool)$result; |
||||
| 65 | } |
||||
| 66 | /** |
||||
| 67 | * @param string $key |
||||
| 68 | */ |
||||
| 69 | protected function clean_cache ($key) { |
||||
| 70 | $this->cache->del("$key/".Language::instance()->clang); |
||||
| 71 | } |
||||
| 72 | /** |
||||
| 73 | * Get content |
||||
| 74 | * |
||||
| 75 | * @param string|string[] $key |
||||
| 76 | * |
||||
| 77 | * @return false|mixed |
||||
| 78 | */ |
||||
| 79 | public function get ($key) { |
||||
| 80 | if (is_array($key)) { |
||||
| 81 | return array_map([$this, 'get'], $key); |
||||
| 82 | } |
||||
| 83 | $key = str_replace(['/', '?', '#', '"', '<', '>'], '_', $key); |
||||
| 84 | return $this->cache->get( |
||||
| 85 | "$key/".Language::instance()->clang, |
||||
| 86 | function () use ($key) { |
||||
| 87 | return $this->read($key); |
||||
| 88 | } |
||||
| 89 | ); |
||||
| 90 | } |
||||
| 91 | /** |
||||
| 92 | * Get keys of all content items |
||||
| 93 | * |
||||
| 94 | * @return int[]|false |
||||
| 95 | */ |
||||
| 96 | public function get_all () { |
||||
| 97 | return $this->search([], 1, PHP_INT_MAX, 'key', true); |
||||
| 98 | } |
||||
| 99 | /** |
||||
| 100 | * Set content |
||||
| 101 | * |
||||
| 102 | * @param string $key Key associated with content, works like id |
||||
| 103 | * @param string $title Content title |
||||
| 104 | * @param string $content Content itself |
||||
| 105 | * @param string $type Type of content: <b>text</b> or <b>html</b>. Influences on editor type |
||||
| 106 | * |
||||
| 107 | * @return bool |
||||
| 108 | */ |
||||
| 109 | public function set ($key, $title, $content, $type) { |
||||
| 110 | $result = $this->update($key, $title, $content, $type); |
||||
|
0 ignored issues
–
show
$key of type string is incompatible with the type array expected by parameter $arguments of cs\modules\Content\Content::update().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 111 | if ($result) { |
||||
| 112 | $this->clean_cache($key); |
||||
| 113 | } |
||||
| 114 | return $result; |
||||
| 115 | } |
||||
| 116 | /** |
||||
| 117 | * Delete content |
||||
| 118 | * |
||||
| 119 | * @param string $key |
||||
| 120 | * |
||||
| 121 | * @return bool |
||||
| 122 | */ |
||||
| 123 | public function del ($key) { |
||||
| 124 | $result = $this->delete($key); |
||||
| 125 | if ($result) { |
||||
| 126 | $this->clean_cache($key); |
||||
| 127 | } |
||||
| 128 | return $result; |
||||
| 129 | } |
||||
| 130 | } |
||||
| 131 |