Skip to content

Instantly share code, notes, and snippets.

@sidor1989
Last active October 7, 2019 07:26
Show Gist options
  • Save sidor1989/f046ef6dd38e948062b162edec7d2686 to your computer and use it in GitHub Desktop.
Save sidor1989/f046ef6dd38e948062b162edec7d2686 to your computer and use it in GitHub Desktop.
<?
[[+phx:input=`now`:strtotime:el=`[[+publishedon:add=`7776000`]]`:then=` <span style="color: #46a546;" >NEW</span>`:else=``]]
//меняем шаблон всем русурсам у указанного родителя
$parent = 1840; //укажите ID родителя
$template = 83; //укажите ID нового шаблона
foreach ($modx->getIterator('modResource', array('parent' => $parent)) as $resource) {
$resource->set('template', $template);
$resource->save();
}
//преносим значение тв у указанного родителя
$parent = 18; //укажите ID родителя
foreach ($modx->getIterator('modResource', array('parent' => $parent)) as $resource) {
$date1tv = $resource->getTVValue('datestart');
$date2tv = $resource->getTVValue('datestop');
$resource->set('date1', $date1tv);
$resource->set('date2', $date2tv);
$resource->save();
}
// Считаем всех детей контейнера с id 1815, глубина 10
$parent = 1815; //укажите ID родителя
$template = 83; //укажите ID нового шаблона
$child = $modx->getChildIds($parent, 10, array('context' => 'web'));
$collection = $modx->getCollection('modResource', array(
'id:IN' => $child,
));
if ($collection) {
foreach ($collection as $resource) {
$resource->set('template', $template);
$resource->save();
}
}
//**********************************************
//делаем свое событие на примере сохранения продукта минишопа
//регистрируем событие
$Event = $modx->newObject('modEvent');
$Event->set('name', 'msOnAfterSaveProduct');
$Event->set('service',6);
$Event->set('groupname', 'Custom');
//далее пишем в методе событие
/**
* @param null $cacheFlag
*
* @return bool
*/
public function save($cacheFlag = null)
{
if (!$this->isNew() && parent::get('class_key') != 'msProduct') {
$this->loadData()->remove();
parent::set('show_in_tree', true);
} else {
$this->loadData();
}
$saved = parent::save($cacheFlag);
$this->xpdo->invokeEvent('msOnAfterSaveProduct', array(
'msProduct' => &$this,
));
return $saved;
}
///**********************************************************
//метод для процессора очищающий тип поля datetime в обход объекта
/**
* @return bool
*/
public function afterSave()
{
if (empty($this->getProperty('date2'))) {
$table = $this->modx->getTableName('msProductData');
$update = $this->modx->prepare("UPDATE {$table} SET date2 = NULL WHERE id = ?");
$update->execute(array($this->object->get('id')));
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment