Giter Club home page Giter Club logo

cakephp_imagemultiselect's Introduction

ImageMultiselect 1.0 Plugin

This plugin provides your a component to select images in you website. You will have two sets of images (left and right). In the left side you will find all available images (pagination is working). In the right panel you will see selected images. User can choose an image by clicking it and add/remove it by double click. Additionally, there are 4 well known buttons: add one item, add all items, remove one item, remove all items. Developer can customize the view through css.

The plugin is based on the MeioUpload Behavior. You should consider its README and installation notes. http://github.com/jrbasso/MeioUpload

Description

The plugin consist of behaviour, controller with associated view and some CSS and JS code.

Behaviour

Behaviour 'ImageUploadBehavior' is inhereted from the 'MeioUploadBehavior'. Inheretence is used to change several things in MeioUpload. First of all there is a new variable 'image_default_options'. This variable contains default parameters for the behaviour. These parameters are 'generateName' and 'maxFiles'. 'ImageUpload' behaviour will create a following structure of directories: <base_dir>/, where '<base_dir>' is a base directory for upload (inhereted from MeioUpload), and '' is the number. Numeration starts from '1'. Each folder (i.e. 1, 2, 5, e.t.c.) can contain only 'maxFile' files. Each directory will contain additional directory with thumbnails.

For example, 'maxFile' is set to 1. After uploading first image it would be placed in directory with name '1'. Second uploaded image would be automaticly reside in directory with name '2'.

If variable 'generateName' is set to TRUE, then the behaviour will generate the filename instead of using original file name (MeioUpload way). The new file name is generated by the following PHP code: $new_filename = md5(uniqid(rand(), true));

Function _createThumbnail is also overrided. New parameters are added: maxDimension -> a, heightPortrait, widthLandscape. If maxDimension is set to 'a' (adjustable), then the thumbnail is created based on the orientation. heightPortrait and widthLandscape define maximal size of the sides respectively.

Note that user can not upload the images without using the database table to store information about uploads.

Controller

Controller 'ImageSelectsController' consists of two methods: set_instance and 'preview'. 'Set_instance' is used to assiciate data model with the controler. See the usage paragraph to know more about it.

'Preview' is the controller method with a view. Method extracts data from the associated data model and pass it to the view. This method has one parameter: $this->params['form']['selected']) It can contain the IDs of already selected items. The method sets two variables: 'allphotos', which contains paginated version of all images available for selection, and 'allselected', which contains all selected images.

View

File 'preview.ctp' dispalys double-sided regions to select images. You can customize the output by editing CSS or HTML source.

Div with ID 'leftimgs' contains images available for selection, div with ID 'rightimgs' contains selected images. Note, that selected data is not processed in any way. It is your responsibility to get the selected data and process it. One can easily do it with jQuery. Statement $("#rightimgs > div[selected]") returns all images from the right side.

Installation

  1. Download and install MeioUpload. Be sure that you can use AJAX and jQuery in you CakePHP app.

  2. Download the archive from github and extract it in 'plugins/image_select'. Note that if you install it in another directory, then you need to change the code in file '<plugin_dir>/views/image_selects/preview.ctp'. Search for the 'image_select' string.

  3. Define the databse where you want to store the information about uploaded images. Let us consider that its name is 'photos'. There are correspondent controller, model and views. Besides we have the table 'cities' with all necessary things. The idea is that one city could have many photos assigned with it. Photos table should have primary key id,

  4. Connect the behaviour. In your file 'app/models/photo.php' add:

array( 'filename' => array( 'dir' => 'uploads', 'create_directory' => true, 'generateName' => false, 'maxFiles' => 5, 'useTable' => true, 'thumbsizes' => array( '600' => array('heightPortrait' => 600, 'widthLandscape' => 600, 'maxDimension' => 'a'), ), ) ) ); ?>
Please note that currently thumbnail directory name is hard-coded in the
'preview.ctp'. My very next plan is to add a 'preview_link' field in the 
database table to store the name of the thumbnail folder.
  1. Add fields to upload images in your 'app/views/photos/add.ctp': echo $form->create('Photo', array('type' => 'file')); ... echo $form->input('Photo.filename', array('type' => 'file')); echo $form->input('UserName', array('label' => __('Name', true)) );

    Field UserName is used to handle the name of the image entered by user. This name is stored in the database table, in field 'name'. I use name 'UserName' so that if this field is left blank, then the filename without extension is used as the human readable name.

  2. To use the controller you should inherit your 'PhotosController' from 'ImageSelectsController'. Write this in your 'app/controlles/photos_controller.php': App::import('Controller', 'ImageSelect.ImageSelects'); class PhotosController extends ImageSelectsController

  3. Add following beforeFilter method to your PhotosController: function beforeFilter() { $this->set_instance($this->modelKey); }

    Now you can use method 'preview' from your Photos controler.

  4. We will use AJAX to add ability of association iamges with cities. Add CSS and JavaScript code from the plugin to the 'app/views/cities/add.ctp': echo $javascript->link('/image_select/js/image_upload.js', false); echo $html->css('/image_select/css/image_select.css', false);

  5. Define somwhere the container for the multi-side selection element:

  6. Add the following JS code to 'app/views/cities/add.ctp':

    <script type="text/javascript"> var selected = new Array(); //selected[0] = 2; // possible id for the selected images //selected[1] = 38; var data = new Object(); data.selected = selected; var reload_url = "url(array('controller'=>'photos', 'action'=>'preview'));?>"; $(document).ready(function() { loadPiece(reload_url, "#imageList", data); }); </script>
  7. That is it! Now if you open the page to create the new city you will see the double sided element with photos.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.