Created
February 23, 2012 06:36
-
-
Save enminc/1891111 to your computer and use it in GitHub Desktop.
MODX Revo indexer plugin. Allows fast TV filter searches using custom snippet (not included)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* MODX Revo indexer plugin. Allows fast TV filter searches using custom snippet (not included) | |
* | |
* License: public domain | |
* | |
* Created by: Jeroen Kenters / www.kenters.com | |
* First released: 2012-02-21 (yyyy-mm-dd) | |
* | |
* !!!!!!!Important!!!!!!! | |
* You need to change this! This won't work out of the box! | |
* If you created CMPs before this should be an easy thing to do. Otherwise: hire someone to do the job :-) | |
* | |
* Events (check this in the plugins System Events tab: | |
* - onDocFormSave | |
* - onDocFormDelete | |
* - onResourceUndelete | |
* | |
* Example schema: | |
* <?xml version="1.0" encoding="UTF-8"?> | |
* <model package="myindex" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM"> | |
* | |
* <object class="myObj" table="myindex_objects" extends="xPDOSimpleObject"> | |
* <field key="resource" dbtype="int" precision="20" attributes="unsigned" null="false" default="0" /> | |
* <field key="pagetitle" dbtype="varchar" precision="255" phptype="string" null="false" default="" /> | |
* <field key="field1" dbtype="varchar" precision="255" phptype="string" null="false" default="" /> | |
* <field key="field2" dbtype="varchar" precision="255" phptype="string" null="false" default="" /> | |
* <field key="mytv1" dbtype="text" phptype="string" /> | |
* <field key="mytv2" dbtype="text" phptype="string" /> | |
* <field key="overviewimg" dbtype="varchar" precision="255" phptype="string" null="false" default="" /> | |
* </object> | |
* | |
* </model> | |
* | |
*/ | |
$action = false; //save, delete or false (none) | |
$objtype = false; //you can have different indexable objects (hotels, cars, jobs, etc.) | |
$eventName = $modx->event->name; | |
switch($eventName) { | |
case 'OnDocFormSave': | |
case 'OnResourceUndelete': | |
$action = 'save'; | |
break; | |
case 'OnDocFormDelete': | |
$action = 'delete'; | |
break; | |
default: | |
$action = false; | |
} | |
if($eventName == 'OnResourceUndelete') { | |
//OnResourceUndelete: $resource not available | |
$resource = $modx->getObject('modResource', $id); | |
} | |
if($resource->get('deleted') == 1 || $resource->get('published') != 1) { | |
$action = 'delete'; | |
} | |
if($action != false) { | |
//check if this is an indexable resource based on template id | |
//change this if you have other conditions to determine object type | |
$template = $resource->get('template'); | |
switch($template) { | |
case 4: | |
$objtype = 'myObj'; | |
break; | |
case 5: | |
$objtype = 'myObj2'; | |
break; | |
case 6: | |
$objtype = 'myObj3'; | |
break; | |
default: | |
$objtype = false; //not an indexable object | |
} | |
//add package only if we found an indexable object | |
if($objtype != false) { | |
$modx->addPackage('myindex', $modx->getOption('core_path').'components/myindex/model/'); | |
} | |
} | |
//create/modify index object when resource is saved | |
if($action == 'save' && $objtype != false) { | |
//get or create index object | |
$obj = $modx->getObject($objtype, array('resource' => $resource->get('id'))); | |
if(!is_object($obj)) { | |
$obj = $modx->newObject($objtype); | |
} | |
//we need the raw value of these 2 TVs, others can be the resource/processed value | |
$myTV1 = $modx->getObject('modTemplateVar', array('name' => 'myTV1')); | |
$myTV2 = $modx->getObject('modTemplateVar', array('name' => 'myTV2')); | |
//set index properties | |
$obj->fromArray(array( | |
'resource' => $resource->get('id'), | |
'pagetitle' => $resource->get('pagetitle'), | |
'field1' => $resource->getTVValue('field1'), | |
'field2' => $resource->getTVValue('field2'), | |
'mytv1' => $myTV1->getValue($resource->get('id')), | |
'mytv2' => $myTV2->getValue($resource->get('id')), | |
'overviewimg' => $resource->getTVValue('overviewimg') | |
)); | |
//save index object | |
$obj->save(); | |
} | |
//remove index object when resource is deleted/unpublished | |
elseif($action == 'delete' && $objtype != false) { | |
$modx->removeCollection($objtype, array('resource' => $resource->get('id'))); | |
} | |
return; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment