From The Smartest Wiki
Jump to: navigation, search

SmartestAssetCreationHelper is a class that assists in the creation of new properly-formed rows in the Assets table.

The class can create new assets (files) in four different situations:

  • From a file upload (using an instance of SmartestUploadHelper)
  • From a textarea input
  • From a file that has already been uploaded to the server via FTP or similar
  • From an external URL (for YouTube, Instagram, Vimeo and general URLs and embeds)

The class is normally instantiated with the code for the type of asset you would like to create:

$ach = new SmartestAssetCreationHelper('SM_ASSETTYPE_RICH_TEXT');

However in cases where this is not known, such as a file upload, this can be left blank and the class will try to guess the file type from the file's suffix (for which it will use the first file type that corresponds to that suffix).

Different asset types are stored in different ways. Depending on the way they are stored, a different method will be needed to create a fully-formed asset in the database.

Creating assets from uploads

boolean createNewAssetFromFileUpload(SmartestUploadHelper $upload_helper, string $new_asset_label)

The createNewAssetFromFileUpload() method is used when a file from the user's computer is being uploaded, and it is only meta-data about this file that needs to be stored in the database:

First, a sample form for uploading a file:

<form action="/upload/endpoint" method="post" enctype="multipart/form-data">
  <input type="file" name="new_asset_file" />
  <input type="submit" value="Submit" />

Then, deal with the upload with a SmartestUploadHelper object:

$upload = new SmartestUploadHelper('new_asset_file');

Now, create the asset from the upload using SmartestAssetCreationHelper. $asset is your new and perfectly formed:

$ach = new SmartestAssetCreationeHelper();
$ach->createNewAssetFromFileUpload($upload, "The label of your new file");
$asset = $ach->finish();

Creating assets from text areas

CSS files, rich text, Javascript, templates and other file types can all be created from the user directly inputting their content from a text area on a web page. The createNewAssetFromTextArea() method is used to get this right:

boolean createNewAssetFromTextArea(string $textarea_content, string $new_asset_label)

$ach = new SmartestAssetCreationHelper('SM_ASSETTYPE_RICH_TEXT');
$ach->createNewAssetFromTextArea($textarea_content, "The label of your new file");
$asset = $ach->finish();

Creating a new asset from a file already on the server

When files have already been placed on the server using an FTP client, they just need to be added to the media library database, with the correct file type. This is done with the createNewAssetFromUnImportedFile() method:

boolean(string $filename, string $new_asset_label)

$ach = new SmartestAssetCreationHelper('SM_ASSETTYPE_JPEG_IMAGE');
// Note that the file should already be in the correct folder, and only the actual filename, not the entire path, should be used
$ach->createNewAssetFromTextArea('existing_file.jpg', "The label of your new file"); 
$asset = $ach->finish();

Creating a new asset from a URL to a resource on different server

For YouTube and Vimeo clips, Instagram posts, and oEmbed resources, all of which entail a resource stored on another server and displayed using an embed code, the createNewAssetFromUrl() method is used:

boolean createNewAssetFromUrl(SmartestExternalUrl $url, string $asset_label[, bool $attempt_og_thumbnail=true])

$ach = new SmartestAssetCreationHelper('SM_ASSETTYPE_VIMEO_CLIP');
$url = new SmartestExternalUrl('');
$ach->createNewAssetFromUrl(new SmartestExternalUrl($url, "Label for you new asset", true);
$asset = $ach->finish();

The $attempt_og_thumbnail parameter determines whether or not to scan the markup found at the supplied URL and look for an Open Graph og:image meta tag, which if found, allows a thumbnail image for the clip to be downloaded without the need for any video processing by Smartest.