/**
  * Sort all files of an item by name and eventually sort images first.
  *
  * @param Item $item
  * @param boolean $mixImages
  * @return void
  */
 protected function _sortFiles($item, $mixImages = false)
 {
     if ($item->fileCount() < 2) {
         return;
     }
     $list = $item->Files;
     // Make a sort by name before sort by type.
     usort($list, function ($fileA, $fileB) {
         return strcmp($fileA->original_filename, $fileB->original_filename);
     });
     // The sort by type doesn't remix all filenames.
     if (!$mixImages) {
         $images = array();
         $nonImages = array();
         foreach ($list as $file) {
             // Image.
             if (strpos($file->mime_type, 'image/') === 0) {
                 $images[] = $file;
             } else {
                 $nonImages[] = $file;
             }
         }
         $list = array_merge($images, $nonImages);
     }
     // To avoid issues with unique index when updating (order should be
     // unique for each file of an item), all orders are reset to null before
     // true process.
     $db = $this->_db;
     $bind = array($item->id);
     $sql = "\n            UPDATE `{$db->File}` files\n            SET files.order = NULL\n            WHERE files.item_id = ?\n        ";
     $db->query($sql, $bind);
     // To avoid multiple updates, a single query is used.
     foreach ($list as &$file) {
         $file = $file->id;
     }
     // The array is made unique, because a file can be repeated.
     $list = implode(',', array_unique($list));
     $sql = "\n            UPDATE `{$db->File}` files\n            SET files.order = FIND_IN_SET(files.id, '{$list}')\n            WHERE files.id in ({$list})\n        ";
     $db->query($sql);
 }
 /**
  * Sort all files of an item by name.
  *
  * @param Item $item
  * @param boolean $mixFileTypes
  *
  * @return void
  */
 protected function _sortFiles($item, $mixFileTypes = false)
 {
     if ($item->fileCount() < 2) {
         return;
     }
     if ($mixFileTypes) {
         $list = $item->Files;
         BookReader_Creator::sortFilesByOriginalName($list, false);
     } else {
         $bookreader = new BookReader($item);
         // Get leaves and remove blank ones.
         $leaves = array_filter($bookreader->getLeaves());
         $non_leaves = array_filter($bookreader->getNonLeaves());
         // Manage the case where there is no BookReader data.
         if (empty($leaves) && empty($non_leaves)) {
             $list = $item->Files;
             BookReader_Creator::sortFilesByOriginalName($list, false);
         } else {
             // Order them separately.
             BookReader_Creator::sortFilesByOriginalName($leaves, false);
             BookReader_Creator::sortFilesByOriginalName($non_leaves, false);
             // Finally, merge them.
             $list = array_merge($leaves, $non_leaves);
         }
     }
     // To avoid issues with unique index when updating (order should be
     // unique for each file of an item), all orders are reset to null before
     // true process.
     $db = $this->_db;
     $bind = array($item->id);
     $sql = "\n            UPDATE `{$db->File}` files\n            SET files.order = NULL\n            WHERE files.item_id = ?\n        ";
     $db->query($sql, $bind);
     // To avoid multiple updates, a single query is used.
     foreach ($list as &$file) {
         $file = $file->id;
     }
     // The array is made unique, because a leaf can be repeated.
     $list = implode(',', array_unique($list));
     $sql = "\n            UPDATE `{$db->File}` files\n            SET files.order = FIND_IN_SET(files.id, '{$list}')\n            WHERE files.id in ({$list})\n        ";
     $db->query($sql);
 }