Files and images
In MV there are 3 data types for storing files:
- "file" (single file)
- "image" (single image)
- "multi_images" (an array of images with comments)
Paths to files from a project root without first slash are stored in a database. "File" type files are stored in the "userfiles/models/name_of_model-files/" folders. All images of models are located in folders of "userfiles/models/name_of_model-images/". When uploading on the server and file with such name already exists, a number will be added to file's name.
Thumbs images are created in subfolders, so the name of the subfolder is defined automatically, based on the sizes and the name of the current model, for example "media/userfiles/models/gallery-images/gallery_70x70/gallery_70x70_f64.jpg" is a small copy of "f64.jpg" file located in "gallery-images" folder.
Links to download files and images
- displayFileLink($field [, $link_text, $no_file_text]) - returns a link to download the file
Additional parameters
$link_text - the name of the file to insert into the link (if doesn't exist, then the name of the file will be inserted instead)
$no_file_text - if the file doesn't exist and this parameter is specified, then this text will be displayed
- displayImage($field [, $alt_text, $no_image_text]) - returns the "img" tag with this image with initial width and height
Additional parameters
$alt_text - an alternative text for the "img" tag
$no_image_text - if the image doesn't exist and this parameter is specified, then this text will be displayed
Resize and crop images
- cropImage($image, $width, $height [, $extra_params]) - resize image to the specified sizes with "cropping" to make proper shape of final image
- resizeImage($image, $width, $height [, $extra_params]) - proportional resize of the image without "cropping", so image stays in native sizes
These methods return the "img" tag with an image, proportionally resized, and in case of the "cropImage()" method - to the specified sizes additionally.
The "cropImage()" and "resizeImage()" methods can be called from the object of model and from object of the record, thus they create a folder for resized thumb copies, based on file belonging to a certain model. Additional parameters are similar to the previous methods.
Extra parameters can be passed as array $extra_params. Possible values (keys) of extra parameters are: "alt-text", "no-image-text", "title", "css-class". The values will be added to final "img" tag automatically.
Attention! When calling the methods from the object of model as a parameter of "$image" you need to pass the path to the file, as it is in a database (without a root path), and in case when these methods are called from object of the class "Record", you need to pass this parameter as the name of a field of model, then the path to the file will be taken from record field, loaded in record object. See examples below.
Operating with an array of images
- extractImages($field [, $no-comments]) - conversion to an PHP array of images. Returns an associative array, where keys - are paths to images, and values - are comments. If you pass the value of the second optional parameter as "no-comments", then regular indexed array of paths to images will be returned
- getFirstImage($field) - get the first image from an array. Returns a string value of the path to the image without comment.
Samples
Let's create a model and get the required data in templates
<? class Products extends Model { protected $name = "Products"; protected $model_elements = array( array("Name", "char", "name", array("required" => true)), array("Main image", "image", "main_image"), array("Additional images", "multi_images", "extra_images"), array("Manual", "file", "manual"), array("Description", "text", "text_description") ); } ?>
CREATE TABLE "news" ( "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" VARCHAR, "main_image" VARCHAR, "extra_images" TEXT, "manual" VARCHAR, "text_description" TEXT)
Display the list of all products with images for main template ("products.model.php" file).
<? public function display() { $rows = $this -> select(); $html = ""; foreach($rows as $row) { $url = $this -> root_path."product/".$row["id"]."/"; $html .= '<div class="name">'; $html .= '<a href="'.$url.'">'.$row["name"].'</a></div>'; $html .= '<div class="image">'; $html .= $this -> cropImage($row["main_image"], 150, 150, array("alt-text" => $row["name"])).'</div>'; $html .= '<div class="desc">'; $html .= Service :: cutText($row["text_description"], 200, "...").'</div>'; } return $html; } ?>
Display the list of manuals if any ("products.model.php" file).
<? public function displayManuals() { $rows = $this -> select("manual!=" => ""); $html = ""; foreach($rows as $row) { $html .= '<div class="dowload-file">'; $html .= '<a href="'.$this -> root_path.$row["manual"].'">'.$row["name"].'</a>'; $html .= '</div>'; } return $html; } ?>
On the product page we display all accessible data ("view-product.php" file).
<? //Get the record, based on passed URL in router object $product = $mv -> products -> defineProductPage($mv -> router); $mv -> display404($product); include $mv -> views_path."main-header.php"; ?> <div id="content"> <h2><? echo $product -> name; ?></h2> <div id="main-picture"> <a class="lightbox" href="<? echo $mv -> root_path.$product -> main_image; ?>"> <? echo $mv -> products -> resizeImage($product -> main_image, 300, 250); ?> </a> </div> <div id="extra-pictures"> <? //Display an array of images for lightbox foreach($product -> extractImages("extra_images", "no-comments") as $image) { echo '<a class="lightbox" href="'.$mv -> root_path.$image.'">'; echo $mv -> products -> cropImage($image, 100, 100).'</a>'; } ?> </div> <p><? echo $product -> text_description; ?></p> <p><? echo $product -> displayFileLink("manual", "Download manual"); ?></p> </div> <? include $mv -> views_path."main-footer.php"; ?>
Previous section
Object of Record class