Skip to content

Instantly share code, notes, and snippets.

@l1a7t
Forked from christianhanvey/modx-snippets.php
Last active August 29, 2015 14:08
Show Gist options
  • Save l1a7t/c723a3179ae67de9c30f to your computer and use it in GitHub Desktop.
Save l1a7t/c723a3179ae67de9c30f to your computer and use it in GitHub Desktop.

Revisions

  1. @christianhanvey christianhanvey revised this gist Oct 14, 2013. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions modx-snippets.php
    Original file line number Diff line number Diff line change
    @@ -200,6 +200,18 @@ public function init() {
    foreach ($modx->getIterator('modResource', array('parent' =>15)) as $res) { $res->set('template',5); $res->save(); }


    /* general xpdo query */
    $sql = 'SELECT * FROM `'.$modx->getOption(xPDO::OPT_TABLE_PREFIX).'site_htmlsnippets`';
    $result = $modx->query($sql);

    if (!is_object($result)) {
    return 'No result!';
    }
    else {
    while ($row = $q_chunks->fetch(PDO::FETCH_ASSOC))
    // do something: $row['name'];
    }
    }


    /* ***************************************** */
  2. @christianhanvey christianhanvey revised this gist Oct 14, 2013. 1 changed file with 19 additions and 0 deletions.
    19 changes: 19 additions & 0 deletions modx-snippets.php
    Original file line number Diff line number Diff line change
    @@ -59,6 +59,25 @@
    }
    return $output;


    /* ***************************************** */
    /* SNIPPETS */
    /* ***************************************** */

    // class boilerplate for creating a class for use in a snippet
    // provides access to $modx object by reference and snippet properties
    class MyClass {
    function __construct($modx, $scriptProperties) {
    $this->modx =& $modx;
    $this->props =& $scriptProperties;
    }
    public function init() {
    $this->size = $this->props['size'];
    $this->color = $this->props['color'];
    }
    }


    /* ***************************************** */
    /* RESOURCES */
    /* ***************************************** */
  3. @christianhanvey christianhanvey revised this gist Oct 14, 2013. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions modx-snippets.php
    Original file line number Diff line number Diff line change
    @@ -63,6 +63,8 @@
    /* RESOURCES */
    /* ***************************************** */

    $id = $modx->resource->get('id');

    /* Display a particular document */
    $resource = $modx->getObject('modResource', array('pagetitle'=>'MyDocument')); // with properties
    $resource = $modx->getObject('modResource', $resourceID); // with a document id
    @@ -72,6 +74,7 @@
    /* change a resource property */
    $resource = $modx->getObject('modResource', $resourceID); // with a document id
    $resource->set('template', 7);
    $resource->save();

    /* get all published resources that have not been deleted */
    $resources = $modx->getCollection('modResource',
  4. @christianhanvey christianhanvey revised this gist Oct 9, 2012. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions modx-snippets.php
    Original file line number Diff line number Diff line change
    @@ -105,7 +105,7 @@
    $tv->save();

    /* to get a TV value for the current resource */
    ???


    /* retrieve a number of TVs for resource */
    $results = array();
    @@ -144,7 +144,7 @@
    foreach ($users as $user) {
    $output .= '<p>Username: ' . $user->get('username') . '</p>';
    }
    return $output
    return $output;

    /* Get the user object for the creator of the current document */
    $user = $modx->resource->getOne('CreatedBy');
    @@ -163,11 +163,11 @@
    /* XPDO */
    /* ***************************************** */

    /* getObject - get one table row */
    /* getCollection - get many table rows */
    /* getObject - get one object/table row */
    /* getCollection - get many objects/table rows */
    /* getOne - get one related object */
    /* getMany - get many related objects */
    /* getCollectionGraph - ??? */
    /* getCollectionGraph - get many objects and their related objects */


    /* other xpdo object methods */
  5. @christianhanvey christianhanvey revised this gist Oct 8, 2012. 1 changed file with 141 additions and 29 deletions.
    170 changes: 141 additions & 29 deletions modx-snippets.php
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,55 @@
    Snippet: [[SnippetName]]
    Chunk: [[$ChunkName]]
    System Setting: [[++SettingName]]
    System Setting: [[++SettingName]]
    TV: [[*fieldName/TvName]]
    Link tag: [[~PageId]]
    Placeholder: [[+PlaceholderName]]
    Link tag: [[~PageId? &paramName=`value`]]
    Placeholder: [[+PlaceholderName]]


    <?php

    /* ***************************************** */
    /* GLOBALS AND SETTINGS */
    /* ***************************************** */

    /* always available objects */
    $modx->resource; // the current document
    $modx->user; // the current user

    /* get a system setting */
    $modx->getOption('SettingName'); // eg site_start


    /* ***************************************** */
    /* LOGGING */
    /* ***************************************** */

    /* log an error */
    $elementName = '[SnippetName]';
    $modx->log(modX::LOG_LEVEL_ERROR, $elementName.' Could not do something.');
    // also: LOG_LEVEL_DEBUG, LOG_LEVEL_INFO, LOG_LEVEL_WARN

    /* change target of logging */
    $modx->setLogTarget('FILE'); // or ECHO
    $target = array(
    'target' => 'FILE',
    'options' => array(
    'filename' => 'path_to_file'),
    );
    $modx->log(xPDO::LOG_LEVEL_ERROR, 'Error Message', $target);

    /* change error level */
    $modx->setDebug(E_ALL & ~E_NOTICE); // sets error_reporting to everything except NOTICE remarks
    $modx->setLogLevel(modX::LOG_LEVEL_DEBUG);

    // To report errors to the MODx error log:
    trigger_error('...', E_USER_ERROR);


    /* ***************************************** */
    /* CHUNKS */
    /* ***************************************** */

    /* get simple chunk */
    $modx->getChunk('ChunkName');

    @@ -24,6 +59,9 @@
    }
    return $output;

    /* ***************************************** */
    /* RESOURCES */
    /* ***************************************** */

    /* Display a particular document */
    $resource = $modx->getObject('modResource', array('pagetitle'=>'MyDocument')); // with properties
    @@ -35,19 +73,6 @@
    $resource = $modx->getObject('modResource', $resourceID); // with a document id
    $resource->set('template', 7);

    /* To set a TV value for the current resource */
    $tv = $modx->getObject('modTemplateVar',array('name'=>'tvName')); // get the required TV object by name (or id)
    $tv->setValue($modx->resource->get('id'), $newValue); // set the new value and save it
    $tv->save();

    /* Get all active users */
    $users = $modx->getCollection('modUser',
    array('active'=>'1'));
    foreach ($users as $user) {
    $output .= '<p>Username: ' . $user->get('username') . '</p>';
    }
    return $output

    /* get all published resources that have not been deleted */
    $resources = $modx->getCollection('modResource',
    array('published'=>'1','deleted'=>'0'));
    @@ -57,10 +82,6 @@
    $output .= '<p>Content:' . $resource->get('content') . '</p></div>';
    }

    /* Get the user object for the creator of the current document */
    $user = $modx->resource->getOne('CreatedBy');
    return $user->get('username');

    /* Get the parent object of the current resource */
    $parent = $modx->resource->getOne('Parent');
    return $parent->get('pagetitle');
    @@ -71,6 +92,65 @@
    $output .= '<p>' . $child->get('pagetitle') . '</p>';
    }



    /* ***************************************** */
    /* TVs */
    /* ***************************************** */


    /* To set a TV value for the current resource */
    $tv = $modx->getObject('modTemplateVar',array('name'=>'tvName')); // get the required TV object by name (or id)
    $tv->setValue($modx->resource->get('id'), $newValue); // set the new value and save it
    $tv->save();

    /* to get a TV value for the current resource */
    ???

    /* retrieve a number of TVs for resource */
    $results = array();
    $tvs = $modx->getCollection(
    'modTemplateVar',
    array("`name` IN (
    'image',
    'imageClass',
    'imageAlt',
    'imageCaption'
    )")
    );
    foreach ($tvs as $tv) {
    $results[$tv->get('name')] = (empty($processTVs) ? $tv->getValue($resourceId) : $tv->renderOutput($resourceId));
    }
    // render to a chunk of goodness
    return $modx->getChunk('Picture.tpl',$results);


    /* get all tvs for another resource */
    $obj = $modx->getObject('modResource', array('name'=>'MyDoc'));
    $id = $obj->get('id');
    $tvs = $obj->getMany('TemplateVars');
    foreach ($tvs as $tv) {
    $rawValue = $tv->getValue($id);
    $processedValue = $tv->renderOutput($id);
    }

    /* ***************************************** */
    /* USERS */
    /* ***************************************** */

    /* Get all active users */
    $users = $modx->getCollection('modUser',
    array('active'=>'1'));
    foreach ($users as $user) {
    $output .= '<p>Username: ' . $user->get('username') . '</p>';
    }
    return $output

    /* Get the user object for the creator of the current document */
    $user = $modx->resource->getOne('CreatedBy');
    return $user->get('username');


    /* Publish a set of resources from a list of resource IDs */
    $resourceIds = array('12, 23, 32, 45');
    foreach ($resourceIds as $id) {
    @@ -79,22 +159,54 @@
    $resource->save();
    }

    /* ***************************************** */
    /* XPDO */
    /* ***************************************** */

    /* getObject - get one table row */
    /* getCollection - get many table rows */
    /* getOne - get one related object */
    /* getMany - get many related objects */
    /* getCollectionGraph - ??? */


    /* other xpdo object methods */
    $object->get('fieldName');
    $object->toArray();

    /* change the template of many documents with the same parent */
    foreach ($modx->getIterator('modResource', array('parent' =>15)) as $res) { $res->set('template',5); $res->save(); }




    /* ***************************************** */
    /* MODX SERVICES - EMAIL */
    /* ***************************************** */

    /* send an email */

    // first, get the email with the placeholders in it replaced by the snippet call.
    // (Note the properties in the snippet call are in the array $scriptProperties.
    // http://api.modx.com/revolution/2.1/_model_modx_mail_modmail.class.html
    $message = $modx->getChunk('myEmail',$scriptProperties);
    /* now load modMail, and setup options */
    $modx->getService('mail', 'mail.modPHPMailer');
    $modx->mail->set(modMail::MAIL_BODY,$message);
    $modx->mail->set(modMail::MAIL_FROM,$scriptProperties['fromEmail']);
    $modx->mail->set(modMail::MAIL_FROM_NAME,$scriptProperties['fromName']);
    $modx->mail->set(modMail::MAIL_SENDER,$scriptProperties['fromName']);
    $modx->mail->set(modMail::MAIL_SUBJECT,$scriptProperties['subject']);
    $modx->mail->address('reply-to',$scriptProperties['fromEmail']);
    $modx->mail->setHTML(true);
    /* specify the recipient */
    $modx->mail->address('to',$scriptProperties['toEmail']);
    /* send! */
    $modx->mail->send();
    $modx->mail->reset();

    /* log an error */
    $elementName = '[SnippetName]';
    $modx->log(modX::LOG_LEVEL_ERROR, $elementName.' Could not do something.');
    // also:
    // LOG_LEVEL_DEBUG
    // LOG_LEVEL_INFO
    // LOG_LEVEL_WARN


    /* change the template of many documents with the same parent */
    foreach ($modx->getIterator('modResource', array('parent' =>15)) as $res) { $res->set('template',5); $res->save(); }


    ?>
  6. Christian Hanvey created this gist May 28, 2012.
    100 changes: 100 additions & 0 deletions modx-snippets.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,100 @@
    Snippet: [[SnippetName]]
    Chunk: [[$ChunkName]]
    System Setting: [[++SettingName]]
    TV: [[*fieldName/TvName]]
    Link tag: [[~PageId]]
    Placeholder: [[+PlaceholderName]]


    <?php

    /* always available objects */
    $modx->resource; // the current document
    $modx->user; // the current user

    /* get a system setting */
    $modx->getOption('SettingName'); // eg site_start

    /* get simple chunk */
    $modx->getChunk('ChunkName');

    /* parse a chunk many times */
    foreach ($docs as $doc) {
    $output .= $modx->getChunk('teaserTpl', $doc->toArray() );
    }
    return $output;


    /* Display a particular document */
    $resource = $modx->getObject('modResource', array('pagetitle'=>'MyDocument')); // with properties
    $resource = $modx->getObject('modResource', $resourceID); // with a document id
    $output = '<p>Document:' . $resource->get('longtitle') . '</p>';
    $output .= '<p>Content:' . $resource->get('content') . '</p>';

    /* change a resource property */
    $resource = $modx->getObject('modResource', $resourceID); // with a document id
    $resource->set('template', 7);

    /* To set a TV value for the current resource */
    $tv = $modx->getObject('modTemplateVar',array('name'=>'tvName')); // get the required TV object by name (or id)
    $tv->setValue($modx->resource->get('id'), $newValue); // set the new value and save it
    $tv->save();

    /* Get all active users */
    $users = $modx->getCollection('modUser',
    array('active'=>'1'));
    foreach ($users as $user) {
    $output .= '<p>Username: ' . $user->get('username') . '</p>';
    }
    return $output

    /* get all published resources that have not been deleted */
    $resources = $modx->getCollection('modResource',
    array('published'=>'1','deleted'=>'0'));
    foreach($resources as $resource) {
    $output .= '<div class="resource"><p>Document:' .
    $resource->get('longtitle') . '</p>';
    $output .= '<p>Content:' . $resource->get('content') . '</p></div>';
    }

    /* Get the user object for the creator of the current document */
    $user = $modx->resource->getOne('CreatedBy');
    return $user->get('username');

    /* Get the parent object of the current resource */
    $parent = $modx->resource->getOne('Parent');
    return $parent->get('pagetitle');

    /* Get the child objects of the current resource */
    $children = $modx->resource->getMany('Children');
    foreach ($children as $child) {
    $output .= '<p>' . $child->get('pagetitle') . '</p>';
    }

    /* Publish a set of resources from a list of resource IDs */
    $resourceIds = array('12, 23, 32, 45');
    foreach ($resourceIds as $id) {
    $resource = $modx->getObject('modResource',$id);
    $resource->set('published', '1');
    $resource->save();
    }

    /* other xpdo object methods */
    $object->get('fieldName');
    $object->toArray();


    /* log an error */
    $elementName = '[SnippetName]';
    $modx->log(modX::LOG_LEVEL_ERROR, $elementName.' Could not do something.');
    // also:
    // LOG_LEVEL_DEBUG
    // LOG_LEVEL_INFO
    // LOG_LEVEL_WARN


    /* change the template of many documents with the same parent */
    foreach ($modx->getIterator('modResource', array('parent' =>15)) as $res) { $res->set('template',5); $res->save(); }


    ?>