Last active
February 28, 2018 20:34
-
-
Save matdave/279ca9180b0842efb03155c67c17ff1c to your computer and use it in GitHub Desktop.
resourceToTag
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 | |
//Plugin to autocreate tags | |
switch ($modx->event->name) { | |
case 'OnDocFormSave': | |
if(!empty($resource)){ | |
$temp = $resource->get('template'); | |
//templates to autotag | |
if($temp == 21 || $temp == 22){ | |
$pagetitle = null; | |
//$group = ($temp == 21) ? 2 : ($temp == 22) ? 1 : null; | |
$group = null; | |
//vary group based on template | |
if ($temp == 21){ | |
$group = 2; | |
$pagetitle = preg_replace('/[,]/', '', (!empty($resource->get('menutitle')) ? $resource->get('menutitle') : $resource->get('pagetitle'))); | |
} | |
if ($temp == 22){ | |
$group = 1; | |
$pagetitle = $resource->get('pagetitle'); | |
} | |
//Template Variable to assign Tag ID | |
$tag = $resource->getTVValue('autoTagID'); | |
if(empty($tag)){ | |
$tagger = $modx->getObject('TaggerTag',array('group' => $group, 'alias'=>$resource->get('alias'))); | |
}else{ | |
$tagger = $modx->getObject('TaggerTag',$tag); | |
} | |
if(empty($tagger)){ | |
$tagger = $modx->newObject('TaggerTag'); | |
} | |
$tagger->set('tag',$pagetitle); | |
$tagger->set('group',$group); | |
$tagger->set('alias',$resource->get('alias')); | |
$tagger->save(); | |
$resource->setTVValue('autoTagID', $tagger->id); | |
if(!empty($tagger)){ | |
$modx->log(modX::LOG_LEVEL_ERROR, '[resourceToTag] -t '.$tagger->id.' -r ' .$resource->id); | |
$tagArr = array('tag' => $tagger->id, 'resource' => $resource->id); | |
$taglink = $modx->getObject('TaggerTagResource', $tagArr); | |
if(empty($taglink)){ | |
$taglink = $modx->newObject('TaggerTagResource'); | |
} | |
$taglink->set('tag',$tagger->id); | |
$taglink->set('resource',$resource->id); | |
if($taglink->save()){ | |
$modx->log(modX::LOG_LEVEL_ERROR, '[resourceToTag] Couldn\'t save tag '.$modx->toJSON($taglink->toArray())); | |
} | |
} | |
} | |
} | |
break; | |
} | |
return; |
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 | |
//Snippet to retrieve resources AutoTagged with this items tags. | |
$tagger = $modx->getService('tagger', 'Tagger', $modx->getOption('tagger.core_path', null, $modx->getOption('core_path') . 'components/tagger/') . 'model/tagger/', $scriptProperties); | |
if (!($tagger instanceof Tagger)) | |
return ''; | |
$resources = $modx->getOption('resources', $scriptProperties, $modx->resource->id); | |
$resources = $tagger->explodeAndClean($resources); | |
$groups = $modx->getOption('groups', $scriptProperties, ''); | |
$groups = $tagger->explodeAndClean($groups); | |
$wrapperTpl = $modx->getOption('wrapperTpl', $scriptProperties, null); | |
$defaultRowTpl = $modx->getOption('rowTpl', $scriptProperties, 'details_tags_tpl'); | |
$c = $modx->newQuery('TaggerTag'); | |
$c->leftJoin('TaggerTagResource', 'Resources'); | |
$c->leftJoin('TaggerGroup', 'Group'); | |
$c->leftJoin('modResource', 'Resource', array('Resources.resource = Resource.id')); | |
$c->leftJoin('modTemplateVarResource', 'modTemplateVarResource', array('TaggerTag.id = modTemplateVarResource.value AND modTemplateVarResource.tmplvarid = 48')); //the ID of the Auto Tag Template Variable | |
$c->leftJoin('modResource', 'TagResource', array('modTemplateVarResource.contentid = TagResource.id')); | |
if (!empty($resources)) { | |
$c->where(array( | |
'Resources.resource:IN' => $resources | |
)); | |
} | |
if ($groups) { | |
$c->where(array( | |
'Group.id:IN' => $groups, | |
'OR:Group.name:IN' => $groups, | |
'OR:Group.alias:IN' => $groups, | |
)); | |
} | |
$c->select('TaggerTag.*, TagResource.id as trid, TagResource.published as trpublished, TagResource.content as trcontent, TagResource.class_key as trclass'); | |
$tags = $modx->getCollection('TaggerTag',$c); | |
$output = null; | |
if(!empty($tags)){ | |
$idx = 0; | |
foreach($tags as $t){ | |
$idx++; | |
$link = $tag = $address1 = $address2 = $city = $state = $address_encode = $zip = $phone = $fax = $target = null; | |
$tag = null; | |
//Format response with whatever fields needed | |
if(!empty($t->get('trid'))){ | |
if($t->get('trclass') == 'modDocument'){ | |
if($t->get('trpublished') == 1){ | |
$link = $modx->makeUrl($t->get('trid')); | |
} | |
}else{ | |
if(is_numeric($t->get('trcontent'))){ | |
$link = $modx->makeUrl($t->get('trcontent')); | |
}else{ | |
if(!empty($t->get('trcontent'))){ | |
$target = " target='_blank'"; | |
$link = $t->get('trcontent'); | |
} | |
} | |
} | |
$resource = $modx->getObject('modResource',$t->get('trid')); | |
if(!empty($resource)){ | |
$t->set('tag',$resource->get('pagetitle')); | |
$address1 = $resource->getTVValue('Address_1'); | |
$address2 = $resource->getTVValue('Address_2'); | |
$city = $resource->getTVValue('Address_city'); | |
$fax = $resource->getTVValue('Address_fax'); | |
$phone = $resource->getTVValue('Address_phone'); | |
$state = $resource->getTVValue('Address_state'); | |
$zip = $resource->getTVValue('Address_zip'); | |
if(!empty($address1) && !empty($city) && !empty($state) && !empty($zip)) | |
$address_encode = urlencode($address1.', '.$city.', '.$state.' '.$zip); | |
} | |
} | |
if(empty($link)){ | |
$tag = $t->get('tag'); | |
}else{ | |
$tag = "<a href='$link' $target>".$t->get('tag')."</a>"; | |
} | |
$output .= $modx->getChunk($defaultRowTpl,array( | |
'tag'=>$tag, | |
'idx'=>$idx, | |
'alias'=>$t->get('tag'), | |
'address1' => $address1, | |
'address2' => $address2, | |
'city' => $city, | |
'state' => $state, | |
'zip' => $zip, | |
'address_encode' => $address_encode, | |
'phone' => $phone, | |
'fax' => $fax | |
)); | |
} | |
} | |
if(!empty($wrapperTpl) && !empty($output)){ | |
return $modx->getChunk($wrapperTpl, array('tags'=>$output)); | |
} | |
return $output; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment