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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
<?php
namespace MatthiasWeb\RealMediaLibrary\folder;
use MatthiasWeb\RealMediaLibrary\attachment;
use MatthiasWeb\RealMediaLibrary\general;
use MatthiasWeb\RealMediaLibrary\api;
use MatthiasWeb\RealMediaLibrary\order;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
abstract class Creatable extends BaseFolder {
public function __construct($id, $parent = -1, $name = "", $slug = "", $absolute = "", $order = -1, $cnt = 0, $row = array()) {
if (!$this instanceof order\Sortable) {
$className = explode("\\", get_class($this));
$className = $className[count($className) - 1];
throw new \Exception("The folder type is defined in the wrong way! Please use the class definition:\n
use " . RML_NS . "\\order; // use namespace
class $className extends order\Sortable { ... }\n\n... You can disable the sortable functionality by set the contentCustomOrder to 2 in the database.");
}
$this->id = $id;
$this->parent = $parent;
$this->name = $name;
$this->cnt = $cnt >= 0 ? $cnt : 0;
$this->order = $order;
$this->children = array();
$this->slug = $slug;
$this->absolutePath = $absolute;
$this->owner = isset($row->owner) ? $row->owner : get_current_user_id();
$this->row = $row;
if (isset($row->restrictions) && is_string($row->restrictions) && strlen($row->restrictions) > 0) {
$this->restrictions = explode(',', $row->restrictions);
$this->restrictionsCount = count($this->restrictions);
}
}
public function read($order = null, $orderby = null) {
return self::xread($this->id, $order, $orderby);
}
public function relocate($parentId, $nextFolderId = false) {
global $wpdb;
$table_name = $this->getTableName();
$this->debug($parentId === $this->id ? "Start to relocate folder $this->id inside parent..." : "Start to relocate folder $this->id to parent $parentId...", __METHOD__);
$this->debug($nextFolderId === false ? "The folder should take place at the end of the list..." : "The folder should take place before folder id $nextFolderId...", __METHOD__);
$parent = $parentId === $this->id ? $this : wp_rml_get_object_by_id($parentId);
$next = $nextFolderId === false ? null : wp_rml_get_object_by_id($nextFolderId);
try {
if ($next === null && is_rml_folder($parent)) {
$this->setParent($parent->id);
}else if (is_rml_folder($next) && is_rml_folder($parent)) {
$parent->reindexChildrens();
$_this = wp_rml_structure_reset(null, false, $this->id);
$next = wp_rml_get_object_by_id($next->id);
$newOrder = $next->order;
$sql = "UPDATE $table_name SET ord = ord + 1 WHERE parent = $parent->id AND ord >= $newOrder";
$wpdb->query($sql);
$_this->setParent($parent->id, $newOrder);
}else{
throw new \Exception(__("Something went wrong.", RML_TD));
}
$this->debug("Successfully relocated", __METHOD__);
return true;
}catch (\Exception $e) {
$this->debug("Error: " . $e->getMessage(), __METHOD__);
return array($e->getMessage());
}
}
public function reindexChildrens($resetData = false) {
global $wpdb;
$table_name = $this->getTableName();
$sql = "UPDATE $table_name AS rml2
LEFT JOIN (
SELECT @rownum := @rownum + 1 AS nr, t.ID
FROM ( SELECT rml.id
FROM $table_name AS rml
WHERE rml.parent = $this->id
ORDER BY rml.ord )
AS t, (SELECT @rownum := 0) AS r
) AS rmlnew ON rml2.id = rmlnew.id
SET rml2.ord = rmlnew.nr
WHERE rml2.parent = $this->id";
$wpdb->query($sql);
$this->debug("Reindexed the childrens order of $this->id", __METHOD__);
if ($resetData) {
wp_rml_structure_reset(null, false);
}
}
public function insert($ids, $supress_validation = false, $isShortcut = false) {
$this->debug("Start moving files " . json_encode($ids) . " to $this->id...", __METHOD__);
if (is_array($ids)) {
if ($isShortcut) {
attachment\Shortcut::getInstance()->_resetLastIds();
}
$cacheIds = array();
foreach ($ids as $value) {
if (!wp_cache_get($value, "posts")) {
$cacheIds[] = $value;
}
}
if (count($cacheIds) > 0) {
$this->debug("Get and cache the following post ids: " . implode(",", $cacheIds), __METHOD__);
get_posts(array(
"numberposts" => -1,
"include" => $cacheIds
));
}
foreach ($ids as $value) {
$this->singleCheckInsert($value);
if ($supress_validation === false) {
$this->singleCheckInsertPermissions($value);
}
}
do_action("RML/Item/Move", $this->id, $ids, $this, $isShortcut);
$foldersToUpdate = wp_attachment_folder($ids);
foreach ($ids as $value) {
_wp_rml_synchronize_attachment($value, $this->id, $isShortcut);
}
$foldersToUpdate[] = $this->id;
wp_rml_update_count($foldersToUpdate);
$this->debug("Successfully moved (isShortcut: $isShortcut)", __METHOD__);
do_action("RML/Item/MoveFinished", $this->id, $ids, $this, $isShortcut);
return true;
}else{
throw new \Exception(__("You need to provide a set of files.", RML_TD));
}
}
protected function singleCheckInsertPermissions($id) {
$validation = apply_filters("RML/Validate/Insert", array(), $id, $this);
if (count($validation) > 0) {
throw new \Exception(implode(" ", $validation));
}
}
protected function singleCheckInsert($id) {
}
public function persist() {
$this->debug("Persist to database...", __METHOD__);
if ($this->id === -1) {
global $wpdb;
$parentObj = wp_rml_get_object_by_id($this->parent);
if (!is_rml_folder($parentObj)) {
throw new \Exception(__("The parent $this->parent does not exist.", RML_TD));
}
$table_name = $this->getTableName();
$insert = $wpdb->insert(
$table_name,
array(
'parent' => $this->parent,
'slug' => $this->getSlug(),
'name' => $this->name,
'type' => $this->getType(),
'ord' => $this->order > -1 ? $this->order : $parentObj->getMaxOrder() + 1,
'restrictions' => implode(",", array_unique($this->restrictions)),
'owner' => $this->owner
)
);
if ($insert !== false) {
$this->id = $wpdb->insert_id;
$this->updateThisAndChildrensAbsolutePath();
wp_rml_structure_reset(null, false);
do_action("RML/Folder/Created", $this->parent, $this->name, $this->getType(), $this->id);
$this->debug("Successfully persisted creatable with id " . $this->id, __METHOD__);
return $this->id;
}else{
throw new \Exception(__("The folder could not be created in the database.", RML_TD));
}
}else{
throw new \Exception(__("The folder could not be created because it already exists.", RML_TD));
}
}
public function updateThisAndChildrensAbsolutePath() {
$this->getAbsolutePath(true, true);
$childs = $this->getChildren();
if (is_array($childs) && count($childs) > 0) {
foreach ($childs as $key => $value) {
$value->updateThisAndChildrensAbsolutePath();
}
}
}
public function addChildren($children) {
$this->children[] = $children;
}
public function getMaxOrder() {
global $wpdb;
$table_name = $this->getTableName();
$order = $wpdb->get_var("SELECT MAX(ord) FROM $table_name WHERE parent=$this->id");
return is_numeric($order) ? $order : 0;
}
public function getRowData($field = null) {
if (is_object($this->row)) {
if ($field == null) {
return $this->row;
}else{
return $this->row->$field;
}
}else{
return false;
}
}
public function getTypeName($default = null) {
return apply_filters("RML/Folder/Type/Name", $default === null ? __('Folder', RML_TD) : $default, $this->getType(), $this->getId());
}
public function getTypeDescription($default = null) {
return apply_filters("RML/Folder/Type/Description", $default === null ? __('A folder can contain every type of file or a collection, but no gallery.', RML_TD) : $default, $this->getType(), $this->getId());
}
public function setParent($id, $ord = -1, $force = false) {
$this->debug("Try to set parent of $this->id from $this->parent to $id...", __METHOD__);
$parent = wp_rml_get_object_by_id($id);
if ($id == $this->parent) {
$this->debug("The parent is the same, propably only the order is changed...", __METHOD__);
}else{
if ($parent === null) {
throw new \Exception(__("The given parent does not exist to set the parent for this folder.", RML_TD));
}
if ($this->isRestrictFor("par")) {
throw new \Exception(__("You are not allowed to change the parent for this folder.", RML_TD));
}
if (!$force && !$parent->isValidChildrenType($this->getType())) {
throw new \Exception(__("The given parent does not allow the folder type.", RML_TD));
}
if ($parent->hasChildren($this->name)) {
throw new general\FolderAlreadyExistsException($id, $this->name);
}
}
$oldData = $this->getRowData();
$beforeId = $this->parent;
$this->parent = $id;
$this->order = $ord > -1 ? $ord : $parent->getMaxOrder() + 1;
$this->debug("Use $this->order (passed $ord as parameter) as new order value", __METHOD__);
if ($this->id > -1) {
global $wpdb;
if ($beforeId != $this->parent) {
$this->updateThisAndChildrensAbsolutePath();
}
$table_name = $this->getTableName();
$wpdb->query($wpdb->prepare("UPDATE $table_name SET parent=%d, ord=%d WHERE id = %d", $id, $this->order, $this->id));
do_action($id == $this->id ? 'RML/Folder/Relocated' : 'RML/Folder/Moved', $this, $id, $this->order, $force, $oldData);
$this->debug("Successfully moved and saved in database", __METHOD__);
}else{
$this->debug("Successfully setted the new parent", __METHOD__);
$this->getAbsolutePath(true, true);
}
return true;
}
public function setName($name, $supress_validation = false) {
$this->debug("Try to set name of $this->id from '$this->name' to '$name'...", __METHOD__);
if (!$this->isValidName($name)) {
throw new \Exception(sprintf(__("'%s' is not a valid folder name.", RML_TD), $name));
}
$parent = wp_rml_get_object_by_id($this->parent);
if ($parent !== null && $parent->hasChildren($name)) {
throw new general\FolderAlreadyExistsException($this->parent, $name);
}
if ($supress_validation === false) {
$validation = apply_filters("RML/Validate/Rename", array(), $name, $this);
if (count($validation) > 0) {
throw new \Exception(implode(" ", $validation));
}
}
$this->name = $name;
if ($this->id > -1) {
global $wpdb;
$this->updateThisAndChildrensAbsolutePath();
$oldData = $this->getRowData();
$table_name = $this->getTableName();
$wpdb->query($wpdb->prepare("UPDATE $table_name SET name=%s WHERE id = %d", $name, $this->id));
do_action('RML/Folder/Renamed', $name, $this, $oldData);
$this->debug("Successfully renamed and saved in database", __METHOD__);
}else{
$this->debug("Successfully setted the new name", __METHOD__);
$this->getAbsolutePath(true, true);
}
return true;
}
public function isValidName($name) {
$name = trim($name);
return strlen($name) > 0 && !in_array($name, $this->systemReservedFolders);
}
public static function xread($id, $order = null, $orderby = null) {
$args = array(
'post_status' => 'inherit',
'post_type' => 'attachment',
'posts_per_page' => -1,
'rml_folder' => $id,
'fields' => 'ids'
);
if ($order !== null) {
$args["order"] = $order;
}
if ($orderby !== null) {
$args["orderby"] = $orderby;
}
$args = apply_filters('RML/Folder/QueryArgs', $args);
$query = new \WP_Query($args);
$posts = $query->get_posts();
$posts = apply_filters('RML/Folder/QueryResult', $posts);
return $posts;
}
}