| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly |
| 4 |
if ( ! defined('ABSPATH')) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
class UpStream_Model_File extends UpStream_Model_Meta_Object |
| 10 |
{ |
| 11 |
protected $fileId = 0; |
| 12 |
|
| 13 |
protected $upfsFileId = null; |
| 14 |
|
| 15 |
protected $createdAt = 0; |
| 16 |
|
| 17 |
protected $reminders = []; |
| 18 |
|
| 19 |
protected $metadataKey = '_upstream_project_files'; |
| 20 |
|
| 21 |
protected $type = UPSTREAM_ITEM_TYPE_FILE; |
| 22 |
|
| 23 |
/** |
| 24 |
* UpStream_Model_File constructor. |
| 25 |
*/ |
| 26 |
public function __construct($parent, $item_metadata) |
| 27 |
{ |
| 28 |
parent::__construct($parent, $item_metadata); |
| 29 |
|
| 30 |
$this->type = UPSTREAM_ITEM_TYPE_FILE; |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
protected function loadFromArray($item_metadata) |
| 35 |
{ |
| 36 |
parent::loadFromArray($item_metadata); |
| 37 |
|
| 38 |
$this->createdAt = !empty($item_metadata['created_at']) ? $item_metadata['created_at'] : null; |
| 39 |
|
| 40 |
if (upstream_filesytem_enabled() && isset($item_metadata['file']) && upstream_upfs_info($item_metadata['file'])) { |
| 41 |
$this->upfsFileId = $item_metadata['file']; |
| 42 |
} elseif (isset($item_metadata['file']) && $item_metadata['file']) { |
| 43 |
$fid = @attachment_url_to_postid($item_metadata['file']); |
| 44 |
if ($fid) { |
| 45 |
$this->fileId = $fid; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
if (!$this->fileId && !$this->upfsFileId) { |
| 50 |
if (!empty($item_metadata['file_id'])) { |
| 51 |
$file = get_attached_file($item_metadata['file_id']); |
| 52 |
if ($file != false) { |
| 53 |
$this->fileId = $item_metadata['file_id']; |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
if (!empty($item_metadata['reminders'])) { |
| 59 |
foreach ($item_metadata['reminders'] as $reminder_data) { |
| 60 |
|
| 61 |
try { |
| 62 |
$d = json_decode($reminder_data, true); |
| 63 |
$reminder = new UpStream_Model_Reminder($d); |
| 64 |
$this->reminders[] = $reminder; |
| 65 |
} catch (\Exception $e) { |
| 66 |
// don't add anything else |
| 67 |
} |
| 68 |
|
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public function storeToArray(&$item_metadata) |
| 74 |
{ |
| 75 |
parent::storeToArray($item_metadata); |
| 76 |
|
| 77 |
if ($this->fileId > 0) { |
| 78 |
$url = wp_get_attachment_url($this->fileId); |
| 79 |
|
| 80 |
if ($url != false) { |
| 81 |
$item_metadata['file'] = $url; |
| 82 |
$item_metadata['file_id'] = $this->fileId; |
| 83 |
} |
| 84 |
} elseif ($this->upfsFileId && upstream_filesytem_enabled()) { |
| 85 |
$item_metadata['file'] = $this->upfsFileId; |
| 86 |
} |
| 87 |
|
| 88 |
if ($this->createdAt >= 0) $item_metadata['created_at'] = $this->createdAt; |
| 89 |
$item_metadata['reminders'] = []; |
| 90 |
|
| 91 |
foreach ($this->reminders as $reminder) { |
| 92 |
$r = []; |
| 93 |
$reminder->storeToArray($r); |
| 94 |
$item_metadata['reminders'][] = json_encode($r); |
| 95 |
} |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
public function __get($property) |
| 100 |
{ |
| 101 |
switch ($property) { |
| 102 |
|
| 103 |
case 'fileId': |
| 104 |
if (upstream_filesytem_enabled()) |
| 105 |
return $this->upfsFileId; |
| 106 |
else |
| 107 |
return $this->fileId; |
| 108 |
|
| 109 |
case 'filename': |
| 110 |
if (upstream_filesytem_enabled()) { |
| 111 |
if ($file = upstream_upfs_info($this->upfsFileId)) { |
| 112 |
return $file->orig_filename; |
| 113 |
} |
| 114 |
} else { |
| 115 |
$fileId = $this->fileId; |
| 116 |
if ($fileId > 0) { |
| 117 |
$file = get_attached_file($fileId); |
| 118 |
return $file ? basename($file) : ''; |
| 119 |
} |
| 120 |
} |
| 121 |
return ''; |
| 122 |
|
| 123 |
case 'fileURL': |
| 124 |
if (upstream_filesytem_enabled()) { |
| 125 |
if ($file = upstream_upfs_info($this->upfsFileId)) { |
| 126 |
return upstream_upfs_get_file_url($this->upfsFileId); |
| 127 |
} |
| 128 |
} else { |
| 129 |
$fileId = $this->fileId; |
| 130 |
if ($fileId > 0) { |
| 131 |
$url = wp_get_attachment_url($fileId); |
| 132 |
return $url || ''; |
| 133 |
} |
| 134 |
} |
| 135 |
return ''; |
| 136 |
|
| 137 |
case 'createdAt': |
| 138 |
if ($this->createdAt > 0) |
| 139 |
return self::timestampToYMD($this->createdAt); |
| 140 |
else |
| 141 |
return ''; |
| 142 |
|
| 143 |
default: |
| 144 |
return parent::__get($property); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
public function __set($property, $value) |
| 149 |
{ |
| 150 |
switch ($property) { |
| 151 |
|
| 152 |
case 'fileId': |
| 153 |
if (upstream_filesytem_enabled()) { |
| 154 |
throw new UpStream_Model_ArgumentException(__('Set not implemented for Upfs.', 'upstream')); |
| 155 |
} else { |
| 156 |
$file = get_attached_file($value); |
| 157 |
if ($file === false) |
| 158 |
throw new UpStream_Model_ArgumentException(sprintf(__('File ID %s is invalid.', 'upstream'), $value)); |
| 159 |
|
| 160 |
$this->fileId = $value; |
| 161 |
} |
| 162 |
break; |
| 163 |
|
| 164 |
default: |
| 165 |
parent::__set($property, $value); |
| 166 |
break; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
public static function fields() |
| 172 |
{ |
| 173 |
$fields = parent::fields(); |
| 174 |
|
| 175 |
$fields['fileId'] = [ 'type' => 'file', 'title' => __('File'), 'search' => false, 'display' => true ]; |
| 176 |
$fields['createdAt'] = [ 'type' => 'date', 'title' => __('Upload Date'), 'search' => true, 'display' => true ]; |
| 177 |
|
| 178 |
$fields = self::customFields($fields, UPSTREAM_ITEM_TYPE_FILE); |
| 179 |
|
| 180 |
return $fields; |
| 181 |
} |
| 182 |
|
| 183 |
public static function create($parent, $title, $createdBy) |
| 184 |
{ |
| 185 |
$item_metadata = ['title' => $title, 'created_by' => $createdBy]; |
| 186 |
|
| 187 |
return new self($parent, $item_metadata); |
| 188 |
} |
| 189 |
|
| 190 |
} |