We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!

Answered MIGX cmp

    • 42239
    • 73 Posts
    Is there any way set a snippet call when you set a field value to a certain value?

    I've created a registration system with a confirm option yes or no. I want to create an email snippet that runs when the value is set to yes. Whats the best way to go about this?

    This question has been answered by Bruno17. See the first response.

      • 4172
      • 5,888 Posts
      Here's a custom-update-processor, which I've used recently for a project, which does that:

      <?php
      
      if (empty($scriptProperties['object_id'])) {
          $updateerror = true;
          $errormsg = $modx->lexicon('quip.thread_err_ns');
          return;
      }
      
      $config = $modx->migx->customconfigs;
      
      $includeTVList = $modx->getOption('includeTVList', $config, '');
      $includeTVList = !empty($includeTVList) ? explode(',', $includeTVList) : array();
      $includeTVs = $modx->getOption('includeTVs', $config, false);
      $classname = 'modResource';
      
      $newstemplate = $modx->getOption('newstemplate', $config, '');
      $newsparent = $modx->getOption('newsparent', $config, '');
      
      //$saveTVs = false;
      /*
      if ($modx->lexicon) {
      $modx->lexicon->load($packageName . ':default');
      }
      */
      if (isset($scriptProperties['data'])) {
          //$scriptProperties = array_merge($scriptProperties, $modx->fromJson($scriptProperties['data']));
          $data = $modx->fromJson($scriptProperties['data']);
      }
      
      $data['id'] = $modx->getOption('object_id', $scriptProperties, null);
      
      //$parent = $modx->getOption('resource_id', $scriptProperties, false);
      $checkresponse = true;
      
      $task = $modx->getOption('task', $scriptProperties, '');
      
      switch ($task) {
          case 'publish':
              if ($modx->hasPermission('publish_document')) {
                  $response = $modx->runProcessor('resource/publish', $data);
              } else {
                  $updateerror = true;
                  $errormsg = 'Fehlende Berechtigung, News zu veröffentlichen';
                  return;
              }
              break;
          case 'unpublish':
              $response = $modx->runProcessor('resource/unpublish', $data);
              break;
          case 'delete':
              $response = $modx->runProcessor('resource/delete', $data);
              break;
          case 'recall':
              $object = $modx->getObject($classname, $scriptProperties['object_id']);
              $object->set('deleted', '0');
              $object->save();
              $checkresponse = false;
              break;
      
          default:
      
              //$modx->migx->loadConfigs();
              //$tabs = $modx->migx->getTabs();
      
              $freigabe = $modx->getOption('tv_news_freigabe_erwartet', $data, '');
      
      
              if (is_array($freigabe)) {
                  $data['tv_news_freigabe_erwartet'] = array();
                  foreach ($freigabe as $value) {
                      if ($value == '2') {
                          $send_mail = true;
                      } else {
                          $data['tv_news_freigabe_erwartet'][] = $value;
                      }
                  }
              } elseif ($freigabe == '2') {
                  $send_mail = true;
                  unset($data['tv_news_freigabe_erwartet']);
              }
      
              $data['context_key'] = 'web';
      
              //$data['context_key'] = $modx->getOption('context_key', $data, $scriptProperties['wctx']);
      
              $date = $modx->getOption('tv_News_Datum', $data, strftime('%Y-%m-%d %H:%M:%S'));
              $title = $modx->getOption('pagetitle', $data, '');
      
              if ($includeTVs) {
                  $c = $modx->newQuery('modTemplateVar');
                  $collection = $modx->getCollection('modTemplateVar', $c);
                  foreach ($collection as $tv) {
                      $tvname = $tv->get('name');
                      if (isset($data[$tvname])) {
                          $value = $data[$tvname];
                          $data['tv' . $tv->get('id')] = $value;
                          unset($data[$tvname]);
                      }
                  }
      
                  $data['tvs'] = 1;
              }
      
              //get/create news-parent (year-directory)
              if ($resource = $modx->getObject('modResource', array('parent' => $newsparent, 'pagetitle' => strftime('%Y', strtotime($date))))) {
                  $parent = $resource->get('id');
              } else {
                  //todo: set all fields correctly
                  $resource = $modx->newObject('modResource');
                  $resource->set('pagetitle', strftime('%Y', strtotime($date)));
                  $resource->set('longtitle', strftime('%Y', strtotime($date)));
                  $resource->set('parent', $newsparent);
                  $resource->set('class_key', 'modWebLink');
                  $resource->set('content', '1');
                  $resource->save();
                  $parent = $resource->get('id');
              }
      
              if ($scriptProperties['object_id'] == 'new') {
                  //$object = $modx->newObject($classname);
                  $resource = $modx->newObject('modResource');
      
                  if (empty($title)) {
                      $updateerror = true;
                      $errormsg = 'Das Feld Titel muß ausgefüllt werden';
                      return;
                  }
      
                  //$data['pagetitle'] = strftime('%y-%m-%d', strtotime($date)) . '_' . $resource->cleanAlias($title);
      
                  if (!empty($parent)) {
                      $data['parent'] = $parent;
                  }
                  $data['template'] = $newstemplate;
                  $data['hidemenu'] = 1;
                  $data['published'] = 0;
                  $response = $modx->runProcessor('resource/create', $data);
              } else {
                  //$object = $modx->getObject($classname, $scriptProperties['object_id']);
                  //if (empty($object)) return $modx->error->failure($modx->lexicon('quip.thread_err_nf'));
                  if (!empty($parent)) {
                      $data['parent'] = $parent;
                  }
      
                  $response = $modx->runProcessor('resource/update', $data);
              }
      
      }
      
      if ($checkresponse) {
          if ($response->isError()) {
              $updateerror = true;
              $errormsg = $response->getMessage();
          }
          $object = $response->getObject();
      }
      
      if ($send_mail) {
      
          $mailTo = '[email protected]';
          //$mailTo = '[email protected]';
      
          $ph = $object;
          $ph['message'] = $modx->getOption('freigabe_text', $data);
      
          $message = $modx->getChunk('ch_News_Freischalt_Benachrichtigungs_Mail', $ph);
          $parts = explode('<--subject-->', $message);
          if (count($parts) > 1) {
              $subject = $parts[0];
              $message = nl2br($parts[1]);
          }
      
          $emailFromAddress = $modx->getOption('emailsender');
          $emailFromName = $modx->getOption('site_name');
      
          $modx->getService('mail', 'mail.modPHPMailer');
          $modx->mail->set(modMail::MAIL_BODY, $message);
          $modx->mail->set(modMail::MAIL_FROM, $emailFromAddress);
          $modx->mail->set(modMail::MAIL_FROM_NAME, $emailFromName);
          $modx->mail->set(modMail::MAIL_SENDER, $emailFromAddress);
          $modx->mail->set(modMail::MAIL_SUBJECT, $subject);
      
          $mailTo = explode(',', $mailTo);
      
          if (is_array($mailTo)) {
              foreach ($mailTo as $email) {
                  $modx->mail->address('to', $email);
              }
          }
      
          $modx->mail->address('reply-to', $emailFromAddress);
          $modx->mail->setHTML(true);
      
          $success = $modx->mail->send();
          if (!$success) {
              $modx->log(modX::LOG_LEVEL_ERROR, 'An error occurred while trying to send the email: Freischalt Benachrichtigung');
          }
      
          $modx->mail->reset();
      }
      
      
      [ed. note: Bruno17 last edited this post 12 years, 3 months ago.]
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 42239
        • 73 Posts
        Quote from: Bruno17 at Jun 26, 2014, 06:25 AM
        Here's a custom-update-processor, which I've used recently for a project, which does that:

        Exactly what I was looking for.

        After I figured out to put this under mypackagename/processors/mgr/default/update.php I ran a test with this code and it worked as expected on the update:
        <?php
            $mailTo = 'myemail';
        
            $message = 'test';
        
            $emailFromAddress = $modx->getOption('emailsender');
            $emailFromName = $modx->getOption('site_name');
        
            $modx->getService('mail', 'mail.modPHPMailer');
            $modx->mail->set(modMail::MAIL_BODY, $message);
            $modx->mail->set(modMail::MAIL_FROM, $emailFromAddress);
            $modx->mail->set(modMail::MAIL_FROM_NAME, $emailFromName);
            $modx->mail->set(modMail::MAIL_SENDER, $emailFromAddress);
            $modx->mail->set(modMail::MAIL_SUBJECT, $subject);
        
            $mailTo = explode(',', $mailTo);
        
            if (is_array($mailTo)) {
                foreach ($mailTo as $email) {
                    $modx->mail->address('to', $email);
                }
            }
        
            $modx->mail->address('reply-to', $emailFromAddress);
            $modx->mail->setHTML(true);
        
            $success = $modx->mail->send();
            if (!$success) {
                $modx->log(modX::LOG_LEVEL_ERROR, 'An error occurred while trying
         to send the email');
            }
        
            $modx->mail->reset();
        ?>


        I want to pull some information from the fields in the row i'm updating, but i'm not sure how to call that. basically I want to say:
        if(field confirmed == 'yes') { email code } and also pull in the emailfield for the toemail. 


        not sure if this is related but when I attempted to update the row it sent the email as expected but did not save the update I made, I believe previous to implementing the update.php it saved with no issue. I'm wondering does this default update.php cancel that out?
        • discuss.answer
          • 4172
          • 5,888 Posts
          if you was using the default update-processor before, try something like this:

          <?php
          
          include $modx->getOption('core_path') . 'components/migx/processors/mgr/default/update.php';
          
          $confirmed = $modx->getOption('confirmed', $postvalues, '');
          
          if ($confirmed == 'yes') {
              $mailTo = $modx->getOption('email', $postvalues, '');
          
              $message = 'test';
          
              $emailFromAddress = $modx->getOption('emailsender');
              $emailFromName = $modx->getOption('site_name');
          
              $modx->getService('mail', 'mail.modPHPMailer');
              $modx->mail->set(modMail::MAIL_BODY, $message);
              $modx->mail->set(modMail::MAIL_FROM, $emailFromAddress);
              $modx->mail->set(modMail::MAIL_FROM_NAME, $emailFromName);
              $modx->mail->set(modMail::MAIL_SENDER, $emailFromAddress);
              $modx->mail->set(modMail::MAIL_SUBJECT, $subject);
          
              $mailTo = explode(',', $mailTo);
          
              if (is_array($mailTo)) {
                  foreach ($mailTo as $email) {
                      $modx->mail->address('to', $email);
                  }
              }
          
              $modx->mail->address('reply-to', $emailFromAddress);
              $modx->mail->setHTML(true);
          
              $success = $modx->mail->send();
              if (!$success) {
                  $modx->log(modX::LOG_LEVEL_ERROR, 'An error occurred while trying to send the email');
              }
          
              $modx->mail->reset();
          }
          
          ?>
          


            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 42239
            • 73 Posts
            Quote from: Bruno17 at Jun 26, 2014, 11:12 AM
            if you was using the default update-processor before, try something like this:

            Excellent! Thanks! I can do all kinds of fun stuff with this now.