In the previous post we have seen the magic of the CKEDITR.insertHtml method, and how we can use it to avoid the whole file manager thing.
The first popup was used to insert files with links to download.
Embedding images (thumbnails with link to full version) is easy too.
The controller action is even simpler:
- /**
- * image browser, renders the (filtered) popup list of image files for the current site
- * @param <string> $opener_instance, name of the field / ckeditor instance
- * that will get the injected html for image display.
- * You can use multiple ck editor instances on teh same page
- */
- function admin_imagebrowser($opener_instance){
- if($this->admin_index('img', true)) {
- $this->set('opener_instance', $opener_instance);
- $this->render('admin_imagebrowser', 'basic');
- }
- }
The admin_index function is still our backbone, providing both the normal action and -in this case- returning the needed files array used in our popups.
The view is where things are slightly different.
in admin_imagebrowser.ctp
- <?php
- $i = 0;
- foreach ($assets as $asset):
- $class = null;
- if ($i++ % 2 == 0) {
- $class = ' class="altrow"';
- }
- ?>
- <?php
-
- if ($asset['Asset']['medium_name'] == 'Image') {
- $thumbsize = 's';
- $version = 's'.DS;
- } else {
- $version = $thumbsize = '';
- }
-
- $path = $version . $asset['Asset']['dirname'] . DS . $asset['Asset']['basename'];
-
- ?>
- <tr<?php echo $class;?>>
- <td>
- <?php
-
- //embed thumb or show icon/image of the document type
-
- // SHOW IMAGE
- if ($asset['Asset']['medium_name'] == 'Image') echo $medium->embed($path);
- ?>
- <?php
-
- // next, show relevant data for the file, based on media type
- ?>
- </td>
- <td colspan="7">
- <?php // common: name, file, directory, dates ?>
- <?php echo $asset['Asset']['id']; ?>, <strong><?php echo $asset['Asset']['name']; ?></strong><br>
- <?php echo $asset['Asset']['description']; ?><br>
- <?php echo $asset['Asset']['dirname']; ?>/<?php echo $asset['Asset']['basename']; ?><br>
- <?php echo __('created: <strong>', false) . $time->format('d-m-Y H:i', $asset['Asset']['created']); ?>
- <?php echo __('</strong>, last modified: <strong>', false) . $time->format('d-m-Y H:i', $asset['Asset']['modified']) .'</strong>'; ?>
- <br>
- <?php
- // if image, show sizes
- if($asset['Asset']['medium_name'] == 'Image' )
- echo __('</strong>format: <strong>', true).
- $asset['Asset']['width'] . 'x' . $asset['Asset']['height'] .' </strong>px. '. __('</strong>, size: <strong>', true)
- . $number->toReadableSize($asset['Asset']['size']) . '</strong>';
-
- ?>
-
- <td class="actions">
- <?php
- //add small image left, add small image right
- //a d d m e d i u m i m a g e baseline + <p>
- $asset['Asset']['description'] != '' ? $description = $asset['Asset']['description'] : $description = $asset['Asset']['name'];
-
- $path_m = 'm/' . $asset['Asset']['dirname'] . '/' . $asset['Asset']['basename'];
- $path_l = 'l/' . $asset['Asset']['dirname'] . '/'. $asset['Asset']['basename'];
-
- // generated HTML to be embedded in CKeditor
- $insert_left =
- $html->div('imageleft',
-
- $html->link(
- $medium->embed($path_m,
- 'alt' => $asset['Asset']['name'],
- 'title' => $asset['Asset']['name'],
- 'escape' => false,
- )
- )
- 'target' => '_blank'), null)
-
- ."</a></p><p><em>".$description."</em></p>"
-
- ;
-
- echo $html->link($html->image('icons_big/Image_Left.png',
- array('alt' => __('embed image',true), 'title' => __('embed image, align left', true), 'border' => 0, 'align' => 'absmiddle')),
-
- 'javascript:InsertHTML(\''. $insert_left.'\');',
-
- ),
- null, null, false
- );
-
- $insert_center =
- $html->div('imagecenter',
- "<p>".
- $html->link(
- $medium->embed($path_l,
- 'alt' => $asset['Asset']['name'],
- 'title' => $asset['Asset']['name'],
- 'escape' => false,
- )
- ),
- )
-
- ."</a></p><p><em>".$description."</em></p>"
- ;
-
- echo ' ';
- echo $html->link($html->image('icons_big/Image_Center.png',
- array('alt' => __('embed image', true), 'title' => __('embed image, big, center', true), 'border' => 0, 'align' => 'absmiddle')),
-
- 'javascript:InsertHTML(\''. $insert_center.'\');',
-
- ),
- null, false
- );
-
- $insert_right =
- $html->div('imageright',
- "<p>".
- $html->link(
- $medium->embed($path_m,
- 'alt' => $asset['Asset']['name'],
- 'title' => $asset['Asset']['name'],
- //'escape' => false,
- )
- ),
- 'target' => '_blank'),null,false
- )
-
- ."</a></p><p><em>".$description."</em></p>"
- ;
-
- echo ' ';
- echo $html->link($html->image('icons_big/Image_Right.png',
- array('alt' => __('embed image',true), 'title' => __('embed image, align right', true), 'border' => 0, 'align' => 'absmiddle')),
-
- 'javascript:InsertHTML(\''. $insert_right.'\');',
-
- ),
- null, null, false
- );
-
- ?>
- </td>
- </tr>
- <?php endforeach; ?>
- </table>
- </div>
- <div class="paging">
- | <?php echo $paginator->numbers();?>
- </div>
- <div class="actions">
- <ul>
- </ul>
- </div>
This time we have 3 links (one for thumb on the left, one for thumb on the right, one for big centered thumbnail).
The main (and only) difference is the preparation of the code to be embedded – just a little bit more complex. See the variables $insert_left, $insert_right and $insert_center.
The javascript doing the actual insert in the ckeditor box is the same as before
on top of admin_imagebrowser.ctp
- <script type="text/javascript">
- <!--
- function InsertHTML(passed)
- {
- var oEditor = opener.CKEDITOR.instances.<?php echo $opener_instance ?>;
- // Check the active editing mode.
- if ( oEditor.mode == 'wysiwyg' )
- {
- // Insert the desired HTML.
- oEditor.insertHtml( passed ) ;
- }
- else
- alert('<?php echo __('You must be on WYSIWYG mode!', true); ?>') ;
-
- window.close();
- }
- -->
- </script>
Next time.. webservices. Searching and embedding youtube videos and flickr images / slideshows.
foreach ($assets as $asset):
$class = null;
if ($i++ % 2 == 0) {
$class = ‘ class=”altrow”‘;
}
?>
<?php
if ($asset[‘Asset’][‘medium_name’] == ‘Image’) {
$thumbsize = ‘s’;
$version = ‘s’.DS;
} else {
$version = $thumbsize = ”;
}
$path = $version . $asset[‘Asset’][‘dirname’] . DS . $asset[‘Asset’][‘basename’];
?>
<tr<?php echo $class;?>>
<td>
<?php
//embed thumb or show icon/image of the document type
// SHOW IMAGE
if ($asset[‘Asset’][‘medium_name’] == ‘Image’) echo $medium->embed($path);
?>
<?php
////echo $path;
// next, show relevant data for the file, based on media type
?>
</td>
<td colspan=”7″>
<?php // common: name, file, directory, dates ?>
<?php echo $asset[‘Asset’][‘id’]; ?>, <strong><?php echo $asset[‘Asset’][‘name’]; ?></strong><br>
<?php echo $asset[‘Asset’][‘description’]; ?><br>
<?php echo $asset[‘Asset’][‘dirname’]; ?>/<?php echo $asset[‘Asset’][‘basename’]; ?><br>
<?php echo __(‘created: <strong>’, false) . $time->format(‘d-m-Y H:i’, $asset[‘Asset’][‘created’]); ?>
<?php echo __(‘</strong>, last modified: <strong>’, false) . $time->format(‘d-m-Y H:i’, $asset[‘Asset’][‘modified’]) .'</strong>’; ?>
<br>
<?php
// if image, show domensions
if($asset[‘Asset’][‘medium_name’] == ‘Image’ )
echo __(‘</strong>format: <strong>’, true).
$asset[‘Asset’][‘width’] . ‘x’ . $asset[‘Asset’][‘height’] .’ </strong>px. ‘. __(‘</strong>, size: <strong>’, true)
. $number->toReadableSize($asset[‘Asset’][‘size’]) . ‘</strong>’;
?>
<?php // echo $asset[‘Asset’][‘site_id’]; ?>
<td class=”actions”>
<?php
//add small image left, add small image right
//a d d m e d i u m i m a g e baseline + <p>
$asset[‘Asset’][‘description’] != ” ? $description = $asset[‘Asset’][‘description’] : $description = $asset[‘Asset’][‘name’];
$path_m = ‘m/’ . $asset[‘Asset’][‘dirname’] . ‘/’ . $asset[‘Asset’][‘basename’];
$path_l = ‘l/’ . $asset[‘Asset’][‘dirname’] . ‘/’. $asset[‘Asset’][‘basename’];
// generated HTML to be embedded in CKeditor
$insert_left =
$html->div(‘imageleft’,
// “<div class=\”imageleft\”>”.”<p><a href=\”/assets/show/”.$asset[‘Asset’][‘id’]. “\” target=\”_blank\”>”.
$html->link(
$medium->embed($path_m,
array(‘align’ => ‘baseline’,
‘alt’ => $asset[‘Asset’][‘name’],
‘title’ => $asset[‘Asset’][‘name’],
‘escape’ => false,
)
)
,array(‘controller’ => ‘assets’, ‘action’ => ‘show’, $asset[‘Asset’][‘id’], ‘admin’ => false),
array(‘escape’ => false,
‘target’ => ‘_blank’), null)
//.”</a></p><p><em>”.htmlentities($description, ENT_QUOTES).”</em></p>”
.”</a></p><p><em>”.$description.”</em></p>”
//.”</div>”
,array(‘escape’ => false)). ‘ ’
;
//debug ($insert_left);
//$insert_left =”-“;
// echo ‘<a href=\’javascript:InsertHTML(\”. $insert_left .’\’);\’ >’
// . $html->image(‘icons_big/Image_Left.png’,
// array(‘alt’ => __(’embed image’, true), ‘title’ => __(’embed image, align left’, true), ‘border’ => 0, ‘align’ => ‘absmiddle’)).
// ‘</a>’;
echo $html->link($html->image(‘icons_big/Image_Left.png’,
array(‘alt’ => __(’embed image’,true), ‘title’ => __(’embed image, align left’, true), ‘border’ => 0, ‘align’ => ‘absmiddle’)),
//’javascript:;’,
‘javascript:InsertHTML(\”. $insert_left.’\’);’,
array(‘escape’ => false,
//’onclick=”javascript:InsertHTML(“‘. $insert_right .'”); return false;”‘
),
null, null, false
);
$insert_center =
$html->div(‘imagecenter’,
“<p>”.
$html->link(
$medium->embed($path_l,
array(‘align’ => ‘baseline’,
‘alt’ => $asset[‘Asset’][‘name’],
‘title’ => $asset[‘Asset’][‘name’],
‘escape’ => false,
)
),
array(‘controller’ => ‘assets’, ‘action’ => ‘show’, $asset[‘Asset’][‘id’], ‘admin’ => false),
array(‘escape’ => false, ‘target’ => ‘_blank’),null,false
)
//.”</a></p><p><em>”.htmlentities($description, ENT_QUOTES).”</em></p>”
.”</a></p><p><em>”.$description.”</em></p>”
,array(‘escape’ => false))
;
echo ‘ ‘;
echo $html->link($html->image(‘icons_big/Image_Center.png’,
array(‘alt’ => __(’embed image’, true), ‘title’ => __(’embed image, big, center’, true), ‘border’ => 0, ‘align’ => ‘absmiddle’)),
//’javascript:;’,
‘javascript:InsertHTML(\”. $insert_center.’\’);’,
array(‘escape’ => false,
//’onclick’ => ‘javascript:InsertHTML(\”. $insert_center .’\’);’
),
null, false
);
$insert_right =
$html->div(‘imageright’,
“<p>”.
$html->link(
$medium->embed($path_m,
array(‘align’ => ‘baseline’,
‘alt’ => $asset[‘Asset’][‘name’],
‘title’ => $asset[‘Asset’][‘name’],
//’escape’ => false,
)
),
array(‘controller’ => ‘assets’, ‘action’ => ‘show’, $asset[‘Asset’][‘id’], ‘admin’ => false),
array(‘escape’ => false,
‘target’ => ‘_blank’),null,false
)
//.”</a></p><p><em>”.htmlentities($description, ENT_QUOTES).”</em></p>”
.”</a></p><p><em>”.$description.”</em></p>”
, array(‘escape’ => false)). ‘ ’
;
echo ‘ ‘;
echo $html->link($html->image(‘icons_big/Image_Right.png’,
array(‘alt’ => __(’embed image’,true), ‘title’ => __(’embed image, align right’, true), ‘border’ => 0, ‘align’ => ‘absmiddle’)),
//’javascript:;’,
‘javascript:InsertHTML(\”. $insert_right.’\’);’,
array(‘escape’ => false,
//’onclick=”javascript:InsertHTML(“‘. $insert_right .'”); return false;”‘
),
null, null, false
);
// $html->link(
// $medium->embed($path_m,
// array( //’align’ => ‘right’,
// ‘alt’ => $asset[‘Asset’][‘name’],
// ‘title’ => $asset[‘Asset’][‘name’],
// //’border’ => 0,
// ‘hspace’ => 8
// )
// ),
// array(‘controller’ => ‘assets’, ‘action’ => ‘show’, $asset[‘Asset’][‘id’], ‘admin’ => false),
// array(‘target’ => ‘_blank’),null,false
// );
// echo $html->link($html->image(‘icons_big/Image_Left.png’,
// array(‘alt’ => __(’embed image’, true), ‘title’ => __(’embed image, align left’, true), ‘border’ => 0, ‘align’ => ‘absmiddle’)),
// ‘javascript:InsertHTML(\”. $insert_left .’\’); return false;\”,
// array(‘escape’ => false,
// //’onclick’ =>’javascript:InsertHTML(\”. $insert_left .’\’); return false;\”
// ),
// null, false
// );
//@TODO: add attach feature (not embed in text, create a relation)
// echo $ajax->link($html->image(‘icons/attachment.png’, array(‘alt’ => ‘allega’, ‘title’ => ‘allega’, ‘border’ => 0, ‘align’ => ‘absmiddle’)) .
// __(‘Embed this file’, true), array(‘action’ => ‘attach’, $asset[‘Asset’][‘id’]), array(‘complete’ => ‘window.close();’), null, false);
//echo ‘<p>’.$insert_center .'</p>’;
?>
</td>
</tr>
<?php endforeach; ?>
</table>
</div>
<div class=”paging”>
<?php echo $paginator->prev(‘<< ‘.__(‘previous’, true), array(), null, array(‘class’=>’disabled’));?>
| <?php echo $paginator->numbers();?>
<?php echo $paginator->next(__(‘next’, true).’ >>’, array(), null, array(‘class’=>’disabled’));?>
</div>
<div class=”actions”>
<ul>
<li><?php echo $html->link($html->image(‘icons/add.png’, array(‘align’ => ‘absmiddle’)).__(‘New Asset’, true), array(‘action’=>’browser_add’), array(‘escape’ => false), null, false); ?></li>
</ul>
</div>