Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Добавил события при создании вендора #861 #864

Merged
merged 1 commit into from
Apr 27, 2023

Conversation

Electrica
Copy link
Contributor

@Electrica Electrica commented Apr 20, 2023

Что оно делает?

Добавляем события при удалении и создании, обновлении производителей

'msOnBeforeVendorCreate', => 'mode', 'data', 'object,
'msOnAfterVendorCreate',  => 'mode', 'data', 'object,
'msOnBeforeVendorUpdate',  => 'mode', 'data', 'object,
'msOnAfterVendorUpdate',  => 'mode', 'data', 'object,
'msOnBeforeVendorDelete',  => 'object',
'msOnAfterVendorDelete',  => 'object',

Связанные проблема(ы)/PR(ы)

#861

@webinmd
Copy link
Collaborator

webinmd commented Apr 25, 2023

Начну с замечаний, не столь критичных.
Все события с наименованием Before вызываются по 2 раза
Data и Object по сути содержать одно и тоже, просто data- массив. Object в этом случае функциональнее. Но так как процессоры просто наследуются, не вижу смысла менять эти 2 момента
Пример плагина для получения всех данных и вывода их в консоль.
Необходимо для тестирования, практичного применения не вижу

<?php
/*
'msOnBeforeVendorCreate',
'msOnAfterVendorCreate',
'msOnBeforeVendorUpdate',
'msOnAfterVendorUpdate',
'msOnBeforeVendorDelete',
'msOnAfterVendorDelete',
*/


switch ($modx->event->name) {
  case "msOnBeforeVendorCreate":
      $modx->log(1, "msOnBeforeVendorCreate mode ".$mode);
      $modx->log(1, "msOnBeforeVendorCreate id ".$id);
      $modx->log(1, "msOnBeforeVendorCreate data ".print_r($data, 1));
      $modx->log(1, "msOnBeforeVendorCreate object ".print_r($object->toArray(), 1));
    
    break;
    
  case "msOnAfterVendorCreate":
      $modx->log(1, "msOnAfterVendorCreate mode ".$mode);
      $modx->log(1, "msOnAfterVendorCreate id ".$id);
      $modx->log(1, "msOnAfterVendorCreate object ".print_r($object->toArray(), 1));
    
    break;
    
  case "msOnBeforeVendorUpdate":
      $modx->log(1, "msOnBeforeVendorUpdate mode ".$mode);
      $modx->log(1, "msOnBeforeVendorUpdate id ".$id);
      $modx->log(1, "msOnBeforeVendorUpdate data ".print_r($data, 1));
      $modx->log(1, "msOnBeforeVendorUpdate object ".print_r($object->toArray(), 1));
    
    break;
    
  case "msOnAfterVendorUpdate":
      $modx->log(1, "msOnAfterVendorUpdate mode ".$mode);
      $modx->log(1, "msOnAfterVendorUpdate id ".$id);
      $modx->log(1, "msOnAfterVendorUpdate object ".print_r($object->toArray(), 1));
    
    break;
    
  case "msOnBeforeVendorDelete":
      $modx->log(1, "msOnBeforeVendorDelete id ".$id);
      $modx->log(1, "msOnBeforeVendorDelete object ".print_r($object->toArray(), 1)); 
    
    break;
    
  case "msOnAfterVendorDelete":
      $modx->log(1, "msOnAfterVendorDelete id ".$id);
      $modx->log(1, "msOnAfterVendorDelete object ".print_r($object->toArray(), 1));
    
    break;
}

Бонусом плагин на СОЗДАНИЕ ресурса при создании вендора и их привязки

<?php
switch ($modx->event->name) {
   
  case "msOnAfterVendorCreate": 
      if($object->resource == 0) {
          
         // можно через процессор, можно через newObject
          $response = $modx->runProcessor('resource/create', array(
            'template' => 1,
            'isfolder' => 0,
            'published' => 1,
            'createdby' => 1,
            'parent' => 13,
            'pagetitle' => $object->name
          ));
          
          if ($response->isError()) {
                $modx->log(modX::LOG_LEVEL_ERROR, 'Возникла проблемма с созданием страницы вендора...');
                $modx->log(modX::LOG_LEVEL_ERROR, $response->getMessage()); 
            } else {
                
                $resource = $response->response['object']['id'];
                $object->set('resource', $resource);
                $object->save();
            }
      }
    
    break;
    
}

@biz87 biz87 merged commit 078f4e2 into modx-pro:master Apr 27, 2023
@Electrica Electrica deleted the vendorevents branch April 27, 2023 16:20
dimasites pushed a commit to dimasites/miniShop2 that referenced this pull request Apr 30, 2023
Добавил события при создании вендора modx-pro#861
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants