/anchor/models/extend.php

https://gitlab.com/valced/anchor-cms · PHP · 260 lines · 185 code · 63 blank · 12 comment · 21 complexity · acea164fefd779a87f9b81f542861702 MD5 · raw file

  1. <?php
  2. class Extend extends Base {
  3. public static $table = 'extend';
  4. public static function field($type, $key, $id = -1) {
  5. $field = Query::table(static::table())
  6. ->where('type', '=', $type)
  7. ->where('key', '=', $key)
  8. ->fetch();
  9. if($field) {
  10. $meta = Query::table(static::table($type . '_meta'))
  11. ->where($type, '=', $id)
  12. ->where('extend', '=', $field->id)
  13. ->fetch();
  14. $field->value = Json::decode($meta ? $meta->data : '{}');
  15. }
  16. return $field;
  17. }
  18. public static function value($extend, $value = null) {
  19. switch($extend->field) {
  20. case 'text':
  21. if( ! empty($extend->value->text)) {
  22. $value = $extend->value->text;
  23. }
  24. break;
  25. case 'html':
  26. if( ! empty($extend->value->html)) {
  27. $md = new Markdown;
  28. $value = $md->transform($extend->value->html);
  29. }
  30. break;
  31. case 'image':
  32. case 'file':
  33. if( ! empty($extend->value->filename)) {
  34. $value = asset('content/' . $extend->value->filename);
  35. }
  36. break;
  37. }
  38. return $value;
  39. }
  40. public static function fields($type, $id = -1) {
  41. $fields = Query::table(static::table())->where('type', '=', $type)->get();
  42. foreach(array_keys($fields) as $index) {
  43. $meta = Query::table(static::table($type . '_meta'))
  44. ->where($type, '=', $id)
  45. ->where('extend', '=', $fields[$index]->id)
  46. ->fetch();
  47. $fields[$index]->value = Json::decode($meta ? $meta->data : '{}');
  48. }
  49. return $fields;
  50. }
  51. public static function html($item) {
  52. switch($item->field) {
  53. case 'text':
  54. $value = isset($item->value->text) ? $item->value->text : '';
  55. $html = '<input id="extend_' . $item->key . '" name="extend[' . $item->key . ']" type="text" value="' . $value . '">';
  56. break;
  57. case 'html':
  58. $value = isset($item->value->html) ? $item->value->html : '';
  59. $html = '<textarea id="extend_' . $item->key . '" name="extend[' . $item->key . ']" type="text">' . $value . '</textarea>';
  60. break;
  61. case 'image':
  62. case 'file':
  63. $value = isset($item->value->filename) ? $item->value->filename : '';
  64. $html = '<span class="current-file">';
  65. if($value) {
  66. $html .= '<a href="' . asset('content/' . $value) . '" target="_blank">' . $value . '</a>';
  67. }
  68. $html .= '</span>
  69. <span class="file">
  70. <input id="extend_' . $item->key . '" name="extend[' . $item->key . ']" type="file">
  71. </span>';
  72. if($value) {
  73. $html .= '</p><p>
  74. <label>' . __('global.delete') . ' ' . $item->label . ':</label>
  75. <input type="checkbox" name="extend_remove[' . $item->key . ']" value="1">';
  76. }
  77. break;
  78. default:
  79. $html = '';
  80. }
  81. return $html;
  82. }
  83. public static function paginate($page = 1, $perpage = 10) {
  84. $query = Query::table(static::table());
  85. $count = $query->count();
  86. $results = $query->take($perpage)->skip(($page - 1) * $perpage)->get();
  87. return new Paginator($results, $count, $page, $perpage, Uri::to('admin/extend/fields'));
  88. }
  89. /*
  90. Process field types
  91. */
  92. public static function files() {
  93. // format file array
  94. $files = array();
  95. if(isset($_FILES['extend'])) {
  96. foreach($_FILES['extend'] as $label => $items) {
  97. foreach($items as $key => $value) {
  98. $files[$key][$label] = $value;
  99. }
  100. }
  101. }
  102. return $files;
  103. }
  104. public static function upload($file) {
  105. $storage = PATH . 'content' . DS;
  106. if(!is_dir($storage)) mkdir($storage);
  107. $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
  108. // Added rtrim to remove file extension before adding again
  109. $filename = slug(rtrim($file['name'], '.' . $ext)) . '.' . $ext;
  110. $filepath = $storage . $filename;
  111. if(move_uploaded_file($file['tmp_name'], $filepath)) {
  112. return $filepath;
  113. }
  114. return false;
  115. }
  116. public static function process_image($extend) {
  117. $file = Arr::get(static::files(), $extend->key);
  118. if($file and $file['error'] === UPLOAD_ERR_OK) {
  119. $name = basename($file['name']);
  120. $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
  121. if($filepath = static::upload($file)) {
  122. $filename = basename($filepath);
  123. // resize image
  124. if(isset($extend->attributes->size->width) and isset($extend->attributes->size->height)) {
  125. $image = Image::open($filepath);
  126. $width = intval($extend->attributes->size->width);
  127. $height = intval($extend->attributes->size->height);
  128. // resize larger images
  129. if(
  130. ($width and $height) and
  131. ($image->width() > $width or $image->height() > $height)
  132. ) {
  133. $image->resize($width, $height);
  134. $image->output($ext, $filepath);
  135. }
  136. }
  137. return Json::encode(compact('name', 'filename'));
  138. }
  139. }
  140. }
  141. public static function process_file($extend) {
  142. $file = Arr::get(static::files(), $extend->key);
  143. if($file and $file['error'] === UPLOAD_ERR_OK) {
  144. $name = basename($file['name']);
  145. if($filepath = static::upload($file)) {
  146. $filename = basename($filepath);
  147. return Json::encode(compact('name', 'filename'));
  148. }
  149. }
  150. }
  151. public static function process_text($extend) {
  152. $text = Input::get('extend.' . $extend->key);
  153. return Json::encode(compact('text'));
  154. }
  155. public static function process_html($extend) {
  156. $html = Input::get('extend.' . $extend->key);
  157. return Json::encode(compact('html'));
  158. }
  159. /*
  160. Save
  161. */
  162. public static function process($type, $item) {
  163. foreach(static::fields($type, $item) as $extend) {
  164. if($extend->attributes) {
  165. $extend->attributes = Json::decode($extend->attributes);
  166. }
  167. $data = call_user_func_array(array('Extend', 'process_' . $extend->field), array($extend, $item));
  168. // save data
  169. if( ! is_null($data)) {
  170. $table = static::table($extend->type . '_meta');
  171. $query = Query::table($table)
  172. ->where('extend', '=', $extend->id)
  173. ->where($extend->type, '=', $item);
  174. if($query->count()) {
  175. $query->update(array('data' => $data));
  176. }
  177. else {
  178. $query->insert(array(
  179. 'extend' => $extend->id,
  180. $extend->type => $item,
  181. 'data' => $data
  182. ));
  183. }
  184. }
  185. // remove data
  186. if(Input::get('extend_remove.' . $extend->key)) {
  187. if(isset($extend->value->filename) and strlen($extend->value->filename)) {
  188. Query::table(static::table($extend->type . '_meta'))
  189. ->where('extend', '=', $extend->id)
  190. ->where($extend->type, '=', $item)->delete();
  191. $resource = PATH . 'content' . DS . $extend->value->filename;
  192. file_exists($resource) and unlink(PATH . 'content' . DS . $extend->value->filename);
  193. }
  194. }
  195. }
  196. }
  197. }