Overview
  • Namespace
  • Class

Namespaces

  • MatthiasWeb
    • RealMediaLibrary
      • api
      • attachment
      • base
      • comp
        • complexquery
      • folder
      • general
      • metadata
      • order
      • rest
      • usersettings
  • None

Classes

  • MatthiasWeb\RealMediaLibrary\attachment\CountCache
  • MatthiasWeb\RealMediaLibrary\attachment\CustomField
  • MatthiasWeb\RealMediaLibrary\attachment\Filter
  • MatthiasWeb\RealMediaLibrary\attachment\Permissions
  • MatthiasWeb\RealMediaLibrary\attachment\Shortcut
  • MatthiasWeb\RealMediaLibrary\attachment\Structure
  • MatthiasWeb\RealMediaLibrary\attachment\Upload
  • MatthiasWeb\RealMediaLibrary\base\Assets
  • MatthiasWeb\RealMediaLibrary\base\Base
  • MatthiasWeb\RealMediaLibrary\base\Core
  • MatthiasWeb\RealMediaLibrary\comp\complexquery\ComplexQuery
  • MatthiasWeb\RealMediaLibrary\comp\complexquery\ResetNames
  • MatthiasWeb\RealMediaLibrary\comp\ExImport
  • MatthiasWeb\RealMediaLibrary\comp\PageBuilders
  • MatthiasWeb\RealMediaLibrary\comp\PolyLang
  • MatthiasWeb\RealMediaLibrary\comp\WPML
  • MatthiasWeb\RealMediaLibrary\folder\BaseFolder
  • MatthiasWeb\RealMediaLibrary\folder\Collection
  • MatthiasWeb\RealMediaLibrary\folder\Creatable
  • MatthiasWeb\RealMediaLibrary\folder\CRUD
  • MatthiasWeb\RealMediaLibrary\folder\Folder
  • MatthiasWeb\RealMediaLibrary\folder\Gallery
  • MatthiasWeb\RealMediaLibrary\folder\Root
  • MatthiasWeb\RealMediaLibrary\general\Activator
  • MatthiasWeb\RealMediaLibrary\general\Assets
  • MatthiasWeb\RealMediaLibrary\general\Core
  • MatthiasWeb\RealMediaLibrary\general\FolderShortcode
  • MatthiasWeb\RealMediaLibrary\general\Lang
  • MatthiasWeb\RealMediaLibrary\general\Migration
  • MatthiasWeb\RealMediaLibrary\general\Options
  • MatthiasWeb\RealMediaLibrary\general\QueryCount
  • MatthiasWeb\RealMediaLibrary\general\Util
  • MatthiasWeb\RealMediaLibrary\general\View
  • MatthiasWeb\RealMediaLibrary\metadata\CoverImage
  • MatthiasWeb\RealMediaLibrary\metadata\Description
  • MatthiasWeb\RealMediaLibrary\metadata\Meta
  • MatthiasWeb\RealMediaLibrary\order\GalleryOrder
  • MatthiasWeb\RealMediaLibrary\order\Sortable
  • MatthiasWeb\RealMediaLibrary\rest\Attachment
  • MatthiasWeb\RealMediaLibrary\rest\Folder
  • MatthiasWeb\RealMediaLibrary\rest\Reset
  • MatthiasWeb\RealMediaLibrary\rest\Service
  • MatthiasWeb\RealMediaLibrary\usersettings\Demo

Interfaces

  • MatthiasWeb\RealMediaLibrary\api\IFolder
  • MatthiasWeb\RealMediaLibrary\api\IFolderActions
  • MatthiasWeb\RealMediaLibrary\api\IFolderContent
  • MatthiasWeb\RealMediaLibrary\api\IMetadata
  • MatthiasWeb\RealMediaLibrary\api\IStructure
  • MatthiasWeb\RealMediaLibrary\api\IUserSettings

Exceptions

  • MatthiasWeb\RealMediaLibrary\general\FolderAlreadyExistsException

Functions

  • _wp_rml_active
  • _wp_rml_root
  • _wp_rml_sanitize
  • _wp_rml_sanitize_filename
  • add_media_folder_meta
  • add_rml_meta_box
  • add_rml_user_settings_box
  • delete_media_folder_meta
  • delete_media_folder_meta_by_key
  • get_media_folder_meta
  • is_rml_folder
  • rml_skip_php_admin_notice
  • rml_skip_rest_admin_notice
  • rml_skip_wp_admin_notice
  • truncate_media_folder_meta
  • update_media_folder_meta
  • wp_attachment_ensure_source_file
  • wp_attachment_folder
  • wp_attachment_get_shortcuts
  • wp_attachment_has_shortcuts
  • wp_attachment_is_shortcut
  • wp_attachment_order_update
  • wp_rml_create
  • wp_rml_create_all_children_sql
  • wp_rml_create_all_parents_sql
  • wp_rml_create_or_return_existing_id
  • wp_rml_create_shortcuts
  • wp_rml_created_shortcuts_last_ids
  • wp_rml_delete
  • wp_rml_dropdown
  • wp_rml_dropdown_collection
  • wp_rml_dropdown_gallery
  • wp_rml_dropdown_gallery_or_collection
  • wp_rml_get_attachments
  • wp_rml_get_by_absolute_path
  • wp_rml_get_by_id
  • wp_rml_get_object_by_id
  • wp_rml_get_parent_id
  • wp_rml_is_type
  • wp_rml_move
  • wp_rml_objects
  • wp_rml_register_creatable
  • wp_rml_rename
  • wp_rml_root_childs
  • wp_rml_structure
  • wp_rml_structure_reset
  • wp_rml_update_count
  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 
<?php
namespace MatthiasWeb\RealMediaLibrary\order;
use MatthiasWeb\RealMediaLibrary\general;
use MatthiasWeb\RealMediaLibrary\folder;
use MatthiasWeb\RealMediaLibrary\attachment;

defined( 'ABSPATH' ) or die( 'No script kiddies please!' );

/**
 * Handles the sortable content in the folder. The methods of this class contains
 * always the keyword "content".
 */
abstract class Sortable extends folder\Creatable {
    
    public function __construct($id, $parent = -1, $name = "", $slug = "", $absolute = "", $order = -1, $cnt = 0, $row = array()) {
        $this->contentCustomOrder = isset($row->contentCustomOrder) ? $row->contentCustomOrder : "2";
        
        // Parent constructor
        parent::__construct($id, $parent, $name, $slug, $absolute, $order, $cnt, $row);
    }
    
    // Documentated in IFolderContent
    public function contentOrder($attachmentId, $nextId, $lastIdInView = false) {
        // Check, if the folder needs the order enabled first
        $contentCustomOrder = $this->getContentCustomOrder();
        if ($contentCustomOrder == 0 && !$this->contentEnableOrder()) {
            throw new \Exception(__("The given folder does not allow to reorder the files.", RML_TD));
        }else if ($contentCustomOrder == "1") { // Reindex
            $this->contentReindex();
        }
        
        // Process
        global $wpdb;
        $table_name = $this->getTableName("posts");
        $this->debug("The folder $this->id wants to move $attachmentId before $nextId (lastIdInView: $lastIdInView)", __METHOD__);
        
        // Is it the real end?
        if ($nextId === false && $lastIdInView !== false) {
            $this->debug("Want to move to the end of the list and there is a pagination system with the lastIdInView...", __METHOD__);
            $nextIdTo = $this->getAttachmentNextTo($lastIdInView);
            if ($nextIdTo > 0) {
                $nextId = $nextIdTo;
            }
        }
        
        // Push to end
        if ($nextId === false) {
            $newOrder = $this->getContentAggregationNr("MAX") + 1;
            $this->debug("Order the attachment to the end and use the new order value $newOrder...", __METHOD__);
        }else{
            $_newOrder = $this->getContentNrOf($nextId); // Temp save in this, because the query can fail
            $this->debug("Order the attachment before $nextId and change the order value $_newOrder for the moved attachment....", __METHOD__);
            
            // Count up the next ids
            $wpdb->query("UPDATE $table_name SET nr = nr + 1 WHERE fid = $this->id AND nr >= $_newOrder");
            $newOrder = $_newOrder;
        }
        
        // Update the new order number
        if (isset($newOrder) && $newOrder > 0) {
            $wpdb->query($wpdb->prepare("UPDATE $table_name SET nr=%d WHERE fid=%d AND attachment=%d", $newOrder, $this->id, $attachmentId));
            delete_media_folder_meta($this->id, "orderby");
            $this->debug("Successfully updated the order of the attachmnet", __METHOD__);
            
            // Save to old custom order
            $wpdb->query($wpdb->prepare("UPDATE " . $table_name . " SET oldCustomNr = nr WHERE fid = %d;", $this->id));
            $this->debug("Successfully updated the old custom nr of the folder", __METHOD__);
        }else{
            throw new \Exception(__("Something went wrong.", RML_TD));
        }
    }
    
    // Documentated in IFolderContent
    public function contentIndex($delete = true) {
        // Check, if this folder is allowed for custom content order
        if (!$this->isContentCustomOrderAllowed()) {
            return false;
        }
        
        // First, delete the old entries from this folder
        if ($delete && !$this->contentDeleteOrder()) {
            return false;
        }
        
        // Create INSERT-SELECT statement for this folder
        global $wpdb;
        $sql = $wpdb->prepare("UPDATE " . $this->getTableName("posts") . " AS rmlp2
                LEFT JOIN (
                    SELECT
                        wpp2.ID AS attachment,
                        wpp2.fid AS fid,
                        @rownum := @rownum + 1 AS nr,
                        @rownum AS oldCustomNr
                    FROM (SELECT @rownum := 0) AS r,
                        (SELECT wpp.ID, rmlposts.fid
                            FROM $wpdb->posts AS wpp
                            INNER JOIN " . $this->getTableName("posts") . " AS rmlposts ON ( wpp.ID = rmlposts.attachment )
                            WHERE rmlposts.fid = %d
                            AND wpp.post_type = 'attachment'
                            AND wpp.post_status = 'inherit'
                            GROUP BY wpp.ID ORDER BY wpp.post_date DESC, wpp.ID DESC) 
                        AS wpp2
                ) AS rmlnew ON rmlp2.attachment = rmlnew.attachment
                SET rmlp2.nr = rmlnew.nr, rmlp2.oldCustomNr = rmlnew.oldCustomNr
                WHERE rmlp2.fid = %d", $this->id, $this->id);
        $wpdb->query($sql);
        $this->debug("Indexed the content order of $this->id", __METHOD__);
        return true;
    }
    
    // Documentated in IFolderContent
    public function contentReindex() {
        if ($this->getContentCustomOrder() != 1) {
            return false;
        }
        
        global $wpdb;
        $table_name = $this->getTableName("posts");
        $sql = "UPDATE $table_name AS rml2
                LEFT JOIN (
                    SELECT @rownum := @rownum + 1 AS nr, t.attachment
                    FROM ( SELECT rml.attachment
                        FROM $table_name AS rml
                        WHERE rml.fid = $this->id
                        ORDER BY rml.nr ASC )
                        AS t, (SELECT @rownum := 0) AS r
                ) AS rmlnew ON rml2.attachment = rmlnew.attachment
                SET rml2.nr = rmlnew.nr
                WHERE rml2.fid = $this->id";
        
        $wpdb->query($sql);
        $this->debug("Reindexed the content order of $this->id", __METHOD__);
        return true;
    }
    
    // Documentated in IFolderContent
    public function contentEnableOrder() {
        // Check, if this folder is allowed for custom content order
        if (!$this->contentIndex(false)) {
            return false;
        }
        
        global $wpdb;
        $wpdb->query($wpdb->prepare("UPDATE " . $this->getTableName() . " SET contentCustomOrder=1 WHERE id = %d", $this->id));
        $this->contentCustomOrder = 1;
        return true;
    }
    
    // Documentated in IFolderContent
    public function contentDeleteOrder() {
        if ($this->getContentCustomOrder() != 1) {
            return false;
        }
        
        global $wpdb;
        $wpdb->query($wpdb->prepare("UPDATE " . $this->getTableName("posts") . " SET nr=NULL, oldCustomNr=NULL WHERE fid=%d", $this->id));
        $wpdb->query($wpdb->prepare("UPDATE " . $this->getTableName() . " SET contentCustomOrder=0 WHERE id=%d", $this->id));
        delete_media_folder_meta($this->id, "orderby");
        $this->debug("Deleted order of the folder $this->id", __METHOD__);
        return true;
    }
    
    // Documentated in IFolderContent
    public function contentRestoreOldCustomNr() {
        global $wpdb;
        $wpdb->query($wpdb->prepare("UPDATE " . $this->getTableName("posts") . " SET nr = oldCustomNr WHERE fid=%d;", $this->id));
        delete_media_folder_meta($this->id, "orderby");
        $this->debug("Restored the order of folder $this->id to the old custom order", __METHOD__);
        return true;
    }
    
    // Documentated in IFolderContent
    public function isContentCustomOrderAllowed() {
        return $this->getContentCustomOrder() != 2;
    }
    
    // Documentated in IFolderContent
    public function getContentCustomOrder() {
        return $this->contentCustomOrder;
    }
    
    // Documentated in IFolderContent
    public function getAttachmentNextTo($attachmentId) {
        if ($this->getContentCustomOrder() != 1) {
            return false;
        }
        
        global $wpdb;
        $sql = $wpdb->prepare("SELECT o.attachment
                        FROM (SELECT *
                            FROM " . $this->getTableName("posts") . "
                            WHERE fid=%d ORDER BY nr) AS o
                        WHERE o.nr > (SELECT o2.nr FROM (SELECT nr FROM " . $this->getTableName("posts") . " WHERE attachment=%d AND fid=%d) AS o2)
                        LIMIT 1;", $this->id, $attachmentId, $this->id);
        $nextNr = $wpdb->get_var($sql);
        $nextNr = !($nextNr > 0) ? false : $nextNr;
        return $nextNr;
    }
    
    // Documentated in IFolderContent
    public function getContentOrderNumbers($fromCache = true, $indexMode = true) {
        if ($this->getContentCustomOrder() != 1) {
            return false;
        }
        
        global $wpdb;
        
        $fid = $this->id;
        if ($fromCache && isset($this->orderNumbers[$fid])) {
            $results = $this->orderNumbers[$fid];
        }else{
            $results = $wpdb->get_results($wpdb->prepare("SELECT o.attachment, o.nr  FROM " . $this->getTableName("posts") . " AS o WHERE o.fid = %d", $fid), ARRAY_A );
            $this->orderNumbers[$fid] = $results;
            
            if (count($results) == 0) {
                return false;
            }
        }
        
        if ($indexMode && count($results) > 0) {
            $_result = array();
            foreach ($results as $key => $value) {
                $_result[((int)$value["attachment"])] = (int) $value["nr"];
            }
            $results = $_result;
        }
        
        return $results;
    }
    
    // Documentated in IFolderContent
    public function getContentAggregationNr($function = "MAX") {
        if (!in_array($function, array("MAX", "MIN"))) {
            throw new \Exception("Only max or min aggregation function allowed!");
        }
        
        global $wpdb;
        $max = $wpdb->get_var($wpdb->prepare("SELECT " . $function . "(o.nr) FROM " . $this->getTableName("posts") . " AS o WHERE o.fid = %d", $this->id));
        return !($max > 0) ? false : $max;
    }
    
    // Documentated in IFolderContent
    public function getContentNrOf($attachmentId) {
        global $wpdb;
        
        $nextNr = $wpdb->get_var($wpdb->prepare("SELECT o.nr FROM " . $this->getTableName("posts") . " AS o WHERE o.attachment = %d AND o.fid = %d", $attachmentId, $this->id));
        return !($nextNr > 0) ? false : $nextNr;
    }
    
    // Documentated in IFolderContent
    public function getContentOldCustomNrCount() {
        global $wpdb;
        $result = $wpdb->get_col($wpdb->prepare("SELECT COUNT(oldCustomNr) FROM " . $this->getTableName("posts") . " WHERE fid=%d", $this->id));
        return $result[0];
    }
    
    /* STATIC FOR ACTIONS AND FILTERS */
    /*
     * When moving to a folder with content custom order, reindex the folder content.
     * 
     * @hooked RML/Item/MoveFinished
     */
    public static function item_move_finished($folderId, $ids, $folder, $isShortcut) {
        $core = general\Core::getInstance();
        general\Util::getInstance()->doActionAnyParentHas($folder, "Folder/Insert", func_get_args());
        
        if ($folder->getContentCustomOrder() == 1) {
            $core->debug("$folderId detected some new files, synchronize with custom content order...", __METHOD__);
            $folder->contentReindex();
        }
    }
    
    /**
     * Add a condition for the automatic order
     */
    public static function folder_insert_anyParentHasMetadata($conditions, $folder, $args) {
        if ($folder->getContentCustomOrder() != 2) {
            $conditions[] = "rmlmeta.meta_key = 'orderAutomatically' AND rmlmeta.meta_value <> '' AND rmltmp.lvl = '0'";
            $conditions[] = "rmlmeta.meta_key = 'orderBy' AND rmltmp.lvl = '0'";
        }
        return $conditions;
    }
    
    public static function folder_insert_anyParentHasMetadata_orderAutomatically($metas, $folder, $args, $all_metas) {
        // Search for orderAutomatically
        foreach ($metas as $meta) {
            if ($meta["folderId"] == $folder->getId()) {
                // Search for orderBy
                if (isset($all_metas["orderby"])) {
                    foreach ($all_metas["orderby"] as $metaOrderBy) {
                        if ($metaOrderBy["folderId"] == $folder->getId()) {
                            $order = $metaOrderBy["value"];
                            general\Core::getInstance()->debug("New content in folder " . $folder->getId() . ", automatically order by $order ...", __METHOD__);
                            GalleryOrder::getInstance()->order($folder->getId(), $order, false);
                            break;
                        }
                    }
                }
                break;
            }
        }
    }
    
    /**
     * JOIN the order table and orderby the nr.
     * It is only affected when
     * $query = new \WP_Query(array(
     *      'post_status' => 'inherit',
     *      'post_type' => 'attachment',
     *      'rml_folder' => 4,
     *      'orderby' => 'rml'
     * ));
     */
    public static function posts_clauses($pieces, $query) {
        if (!empty($query->query_vars['parsed_rml_folder']) &&
            (empty($query->query['orderby']) ||
                (isset($query->query['orderby']) && $query->query['orderby'] == "rml")
            )
        ) {
            global $wpdb;
            // Get folder
            $folder = wp_rml_get_object_by_id($query->query_vars['parsed_rml_folder']);
            if ($folder === null || $folder->getContentCustomOrder() != 1) {
                $pieces["orderby"] = $wpdb->posts.  ".post_date DESC, " . $wpdb->posts.  ".ID DESC";
                return $pieces;
            }
            
            // left join and order by
            $pieces["join"] .= " LEFT JOIN " . general\Core::getInstance()->getTableName("posts") . " AS rmlorder ON rmlorder.fid=$folder->id AND rmlorder.attachment = " . $wpdb->posts . ".ID ";
            $pieces["orderby"] = "rmlorder.nr, " . $wpdb->posts.  ".post_date DESC, " . $wpdb->posts.  ".ID DESC";
        }
        
        return $pieces;
    }
    
    /**
     * Media Library Assistent extension.
     */
    public static function mla_media_modal_query_final_terms($query) {
        $folderId = attachment\Filter::getInstance()->getFolder(null, true);
        if ($folderId !== null) {
            $folder = wp_rml_get_object_by_id($folderId);
            if ($folder !== null && $folder->getContentCustomOrder() == 1) {
                $query['orderby'] = "rml";
                $query['order'] = "asc";
            }
        }
        return $query;
    }
    
    /**
     * Deletes the complete order. Use it with CAUTION!
     */
    public static function delete_all_order() {
        global $wpdb;
        $wpdb->query("UPDATE " . general\Core::getInstance()->getTableName("posts") . " SET nr=null, oldCustomNr=NULL");
        $wpdb->query("UPDATE " . general\Core::getInstance()->getTableName() . " SET contentCustomOrder=0");
        general\Core::getInstance()->debug("Deleted the whole order of all folders", __METHOD__);
    }
}

?>
API documentation generated by ApiGen