We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19004
    • 33 Posts
    Is it currently readily possible (short of a bunch of targeted manually built insert sql calls) to use snippets to generate unpublished pages under a specific document? This would include the population of TVs.

    Essentially what I’m asking is whether there exists an API for that sort of thing, and if not, what the best way to go about accomplishing what I need would be.

    Thanks
      • 22303 MODX Staff
      • 10,725 Posts
      This is coming in 0.9.7; a consistent, object-oriented API that provides this capability to all the various parts of MODx. For now, duplicating those SQL calls and logic in the manager is about your best bet. If you wait a few more days, I can provide an example snippet that uses an xPDO-generated API for the existing MODx database tables, to do exactly what you are referring to. Here’s what the snippet code might look like running in an existing 0.9.5 deployment, as a preview:

      <?php
      $userId= isset ($userId) ? $userId : $modx->getLoginUserID();
      if (!$userId= intval($userId)) {
          $userId= 0;
      }
      $parentId= isset ($parentId) ? $parentId : $modx->documentObject['parent'];
      $templateId= isset ($templateId) ? $templateId : $modx->documentObject['template'];
      $formTpl= isset ($formTpl) ? $formTpl : 'xPublisher.default';
      
      $dsn= $GLOBALS['database_type'] . ':host=' . $modx->db->config['host'] . ';dbname=' . str_replace('`', '', $modx->db->config['dbase']);
      
      include_once ($modx->config['base_path'] . 'xpdo/xpdo.class.php');
      $xpdo= new xPDO($dsn, $modx->db->config['user'], $modx->db->config['pass'], $modx->db->config['table_prefix']);
      $xpdo->setPackage('modx095');
      $xpdo->setLogLevel(XPDO_LOG_LEVEL_INFO);
      $xpdo->setLogTarget('HTML');
      
      $mode= 'insert';
      if (isset ($_REQUEST['docId']) && $docId= intval($_REQUEST['docId']) && $document= $xpdo->getObject('modDocument', $docId)) {
          $mode= 'update';
      } else {
          $docId= 0;
          $document= $xpdo->newObject('modDocument');
      }
      
      $tvCollection= array ();
      
      if (isset ($_POST['xpublish'])) {
          $document->fromArray($_POST, 'document:');
          if ($document->save()) {
              $docId= $document->get('id');
              if ($tvCollection= $document->getMany('modTemplateVar')) {
                  foreach ($tvCollection as $tvId => & $tvObj) {
                      $tvObj->setValue($docId, $_POST['tv' . $tvObj->get('name') . ':value']);
                      $tvObj->save();
                  }
              }
              $output.= '<p class="success">Your document was successfully saved.</p>';
          } else {
              $output.= '<p class="error">There was an error while attempting to save the document.</p>';
              $xpdo->_log(XPDO_LOG_LEVEL_ERROR, "Error saving new document " . print_r($document->toArray(), true));
          }
      }
      
      $output.= "\n\n\n<form action=\"[~[*id*]~]\" method=\"post\">\n";
      $output.= $modx->parseChunk($formTpl, $document->toArray('document:'), '[+', '+]');
      if (empty ($tvCollection)) {
          $tvCollection= & $document->getMany('modTemplateVar');
      }
      if (!empty ($tvCollection)) {
          foreach ($tvCollection as $tvId => $tvObj) {
              $tvKey= 'tv' . $tvObj->get('name');
              if ($tvObj->get('type') == 'richtext' || $tvObj->get('type') == 'htmlarea') {
                  if (is_array($replace_richtexteditor)) {
                      $replace_richtexteditor= array_merge($replace_richtexteditor, array ($tvKey));
                  } else {
                      $replace_richtexteditor= array ($tvKey);
                  }
              }
              $output .= "<label for=\"{$tvKey}\">{$tv['caption']}</label>\n";
              $output .= $tvObj->renderInput($docId);
              $output .= "<br />";
              $output .= "\n\n";
          }
      }
      $output .= "<input type=\"submit\" name=\"xpublish\" value=\"Publish\" />\n";
      $output .= "<input type=\"reset\" name=\"xreset\" value=\"Reset Form\" />\n";
      $output .= "</form>\n\n\n";
      
      return $output;


      In the meantime, there is another snippet that allows publishing with TV’s without the need for the new xPDO layer...I just need to remember what it’s called...
        • 19004
        • 33 Posts
        I’m able to wait a few days I’d say. I am trying to limit the amount of work that the person managing the site will have to do, but it isn’t critical that I have it ready right away. Let me know if you do find that other snippet, though. If it fits my needs I may just go with that, unless there are drawbacks.

        Thanks a bunch