pepebe
6/22/2015 - 9:11 PM

Bulk update image TVs added to your modmore gallery resources to include the first image (or one of its crop images) of each gallery.

Bulk update image TVs added to your modmore gallery resources to include the first image (or one of its crop images) of each gallery.

    <tr class='warning'>
        <th>[[+i]]</th>
        <th>[[+docid]]</th>
        <td>Gallery did not return an image.</td>
        <td><a target='_blank' href='[[+url]]'>Link</a></td>
        <td><a target='_blank' href='[[++site_url]]manager/?a=resource/update&id=[[+docid]]'>Link</a></td>
    </tr>
    <tr class='success'>
        <th>[[+i]]</th>
        <th>[[+docid]]</th>
        <td>TV updated! New value: <i>[[+file]]</i></td>
        <td><a target='_blank' href='[[+url]]'>Link</a></td>
        <td><a target='_blank' href='[[++site_url]]manager/?a=resource/update&id=[[+docid]]'>Link</a></td>
    </tr>
        <div class='container'>
            <h1>updateGalleryTVs status report</h1>
            <p>Parameters</p>
            <ul>
                <li>templates = [[+params.templates]]</li>
                <li>field = [[+params.field]]</li>
                <li>basename = [[+params.basename]]</li>
                <li>tvId = [[+params.tvId]]</li>
            </ul>
            <p>Updated [[+i]] resources in [[+exec]] seconds</p>
            <table class='table table-striped table-condensed'>
                <thead>
                    <tr><th>No.</th><th>ID</th><th>Message</th><th>View</th><th>Edit</th></tr>
                </thead>
                <tbody>
                    [[+output]]
                </tbody>
            </table>
        </div>
<?php
/*

    If you are using a thumbnailTV for your @modmore #moreGallery resources, updateGalleryTVs might safe you time: https://goo.gl/SjIs5p

    Bulk update image TVs added to your modmore gallery resources 
    to include the first image (or one of its crop images) of each gallery
    
    Changelog:
    ----------
    2015-06-23_01:30 Initial release
    2015-06-23_09:59 Fixed missing updateTV for file_urls
    
    Usage:
    ------
    1. Backup your database (always a good thing to do...)
    2. Update the variables below.
    3. Add this snippet to a resource of your choice.
    4. View it in your browser.
    
    Variables:
    $templates (array) Comma delimited list of template ids that you want to include.
    $field (string) Expects either "file_url" or the name of any of your crop images.
    $basename (bolean) If you work with media sources, set it to true. If you don't, set it to false
    $tvId (int) Id of the TV you want to update
*/

    $templates      =  array(2);
    $field          = 'file_url'; 
    $basename       = true;
    $tvId           = 23;
    
    $tplSuccess = "updateGalleryTVs_tplSuccess";
    $tplWarning = "updateGalleryTVs_tplWarning";
    $tplOuter   = "updateGalleryTVs_tplOuter";    
    
    /* Don't change anything below this line unless you know what you are doing... */
    $output = "";
    
    $startTime = microtime(true);
    
    $site_url = $modx->getOption('site_url');
    
    $docArray = $modx->getCollection(
         'modResource'
        ,array(
             'class_key' => 'mgResource'
            ,'template:IN' => $templates
        )
    );

    $i = 0;
    
    /* --------------------------------------------------------------------------------------- */
    /* Some basic styling.... */
    
    $modx->regClientCSS('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css');
    $modx->regClientScript('https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js');
    $modx->regClientScript('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js');

    /* --------------------------------------------------------------------------------------- */

    foreach($docArray as $doc){
        
        $i++;

        // Props for templating
        $props = array();        
        $props['i'] = $i;
        $props['docid'] = $doc->get('id');
        $props['url'] = $site_url . $modx->makeUrl($props['docid']);;

        $c = $modx->newQuery('mgImage');
        $c->where(array(
            'resource' => $props['docid']
        ));
        $c->sortby('sortorder', 'ASC');
        $c->limit('1');
        
        $image = $modx->getObject('mgImage', $c);
        
        if($image){
            if($field == 'file_url') {
                $image = $image->toArray(); 
                $props['file'] = $image['file_url'];
                
                $tpl = $tplSuccess;
            }
            else {
                $crops = $image->getCropsAsArray();
                
                if(isset($crops[$field])){
                    $props['file'] = $crops[$field]['thumbnail_url'];
                    
                    if(!empty($props['file'])){
                        if($basename){
                            $props['file'] = basename($props['file']);
                        }
                        $doc->setTVValue($tvId, $props['file']);
                        $doc->save();
                        
                        $tpl = $tplSuccess;
                    }
                    
                    
                }
                else {
                    return "Error: This gallery doesn't have a crop named '$field'.";
                }
            }
        }
        else {
            $tpl = $tplWarning;
        }
        
        $output .= $modx->getChunk($tpl , $props);
        
    }
    
    $tpl = $tplOuter;
    
    $props = array();
    $props['i'] = $i;
    $props['output'] = $output;
    $props['exec'] = microtime(true) - $startTime;
    $props['params']['templates'] = implode( ',' , $templates );
    $props['params']['field'] = $field;
    $props['params']['basename'] = $basename;
    $props['params']['tvId'] = $tvId;
    
    $output = $modx->getChunk($tpl , $props);

    return $output;