Ordering Multiple Uploads

2 posts by 2 authors in: Forums > CMS Builder
Last Post: September 21, 2022   (RSS)

By celuch - September 19, 2022

I have asked something similar before, but is there a way to display multiple uploads and order them numerically or alphabetically?  In the old flash uploader images stayed in order but now they upload and do not stay in order.  I post photographs of sports, people and other jobs that need to display in the order they were photographed.  Thanks for any suggestions.

celuch

By daniel - September 21, 2022

Hey celuch,

There are a few options for ordering uploads. Here is a snippet that will make sure that uploads stay in the order that they were saved in:

addAction('upload_saved', 'uploadOrder_setUploadOrderToUploadTime', null, 4);
function uploadOrder_setUploadOrderToUploadTime($tablename, $fieldname, $recordNum, $newUploadNum) {
  $uploadStartTime = substr(intval(floatval($_SERVER['REQUEST_TIME_FLOAT']) * 1000), 4);
  mysql_update('uploads', $newUploadNum, null, ['order' => $uploadStartTime ]);
}

The main drawback here is that when images are uploaded in bulk, they can finish uploading in different orders, so it's not always fully accurate.

It's also possible to order the uploads when a record is saved, with something like this:

addAction('record_postsave', 'uploadOrder_updateUploadsOrder', null, 4);
function uploadOrder_updateUploadsOrder($tableName, $isNewRecord, $oldRecord, $recordNum) {
  $schemaFields = getSchemaFields($tableName);
  foreach($schemaFields as $field) {
    if ($field['type'] == 'upload') {
      $uploads = mysql_select('uploads', mysql_escapef("`tableName` = ? AND `fieldName` = ? AND `recordNum` = ? ORDER BY filePath, num", $tableName, $field['name'], $recordNum ));
      if($uploads) {
        $order = 1;
        foreach($uploads as $upload) {
          mysql_update('uploads', $upload['num'], null, ['order' => $order]);
          $order++;
        }
      }
    }
  }
}

This should automatically alphabetize the uploads in any record when it is saved. Note that it applies to all upload fields in all tables, but it could be customized to be a bit more specific. $tableName can be used to restrict which section(s) it applies to, and $field['name'] can apply to specific fields.

You can put either of these snippets into a plugin (CMSB comes with samplePlugin.php as a plugin template) to use them.

Let me know if you have any other questions!

Thanks,

Daniel
Technical Lead
interactivetools.com