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

    the transport package of our MODx extra installs a few TVs. Uninstalling the extra I'd like to prevent the deletion of one of these TVs. How can this be done?

    Am I on the right way to put this one TV in the install-case of the TV resolver of the transport package?

    The current content of the resolve.tvs.php

    if ($object->xpdo) {
        switch ($options[xPDOTransport::PACKAGE_ACTION]) {
            case xPDOTransport::ACTION_INSTALL:
            case xPDOTransport::ACTION_UPGRADE:
                /** @var modX $modx */
                $modx =& $object->xpdo;
                $modelPath = $modx->getOption('myextra.core_path',null,$modx->getOption('core_path').'components/myextra/').'model/';
    
                /** @var modTemplateVar $tv */
                $tv = $modx->getObject('modTemplateVar',array(
                    'name' => 'TemplateVar1',
                    'name' => 'TemplateVar2',
                    'name' => 'TemplateVar3',
                    'name' => 'TemplateVar4',
                    'name' => 'TemplateVar5',
                    'name' => 'TemplateVar6',
                ));
                if ($tv) {
                    $templates = array('sample.Template');
                    foreach ($templates as $templateName) {
                        /** @var modTemplate $template */
                        $template = $modx->getObject('modTemplate',array('templatename' => $templateName));
                        if ($template) {
                            /** @var modTemplateVarTemplate $templateVarTemplate */
                            $templateVarTemplate = $modx->getObject('modTemplateVarTemplate',array(
                                'templateid' => $template->get('id'),
                                'tmplvarid' => $tv->get('id'),
                            ));
                            if (!$templateVarTemplate) {
                                $templateVarTemplate = $modx->newObject('modTemplateVarTemplate');
                                $templateVarTemplate->set('templateid',$template->get('id'));
                                $templateVarTemplate->set('tmplvarid',$tv->get('id'));
                                $templateVarTemplate->save();
                            }
                        }
                    }
                }
                break;
        }
    }
    return true;
    


    How to modify this code to prevent deletion of the "TemplateVar1"?

    Thanks in advance!
    Martin
      Freelancer @bitego http://www.bitego.com
      ---
      GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
      More infos here: http://www.bitego.com/extras/goodnews/
      • 10208 ☆ A M B ☆
      • 1,780 Posts
      My TV's always return, with data after I reinstall the package.

      I've seen this with MIGX and Articles. Articles even returns... the actual articles.

      Maybe there's something in their code that will clue you into how it's accomplished?
        Frogabog- MODX Websites in Portland Oregon
        "Do yourself a favor and get a copy of "MODX - The Official Guide" by Bob Ray. Read it.
        Having server issues? These guys have MODX Hosting perfected - SkyToaster
        • 10378
        • 375 Posts
        Quote from: frogabog at Jan 02, 2013, 03:30 PM
        My TV's always return, with data after I reinstall the package.

        I've seen this with MIGX and Articles. Articles even returns... the actual articles.

        Maybe there's something in their code that will clue you into how it's accomplished?

        Hi frogabog,

        The problem isn't during upgrade of the package. This works as expected! None of the TVs are overwritten - only new one are added.
        But I want to prevent the deletion of a TV during uninstallation of the package.

        For instance - custom tables in a package aren't deleted during uninstallation.
          Freelancer @bitego http://www.bitego.com
          ---
          GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
          More infos here: http://www.bitego.com/extras/goodnews/
          • 10208 ☆ A M B ☆
          • 1,780 Posts
          Yep, I meant after uninstalling the package entirely (even removing it) and then reinstalling.

          I can't be certain, but I've seen a lot of resources/TV's left over after uninstalling various packages. If you've got a test site, maybe try to uninstall your package and see what is left over? Maybe it's just the TV's I create that remain, or perhaps it's because I've got the TV's in other categories?
            Frogabog- MODX Websites in Portland Oregon
            "Do yourself a favor and get a copy of "MODX - The Official Guide" by Bob Ray. Read it.
            Having server issues? These guys have MODX Hosting perfected - SkyToaster
            • 3749
            • 24,544 Posts
            I can never remember the exact details, so you'll have to experiment, but I think if you set this in the attributes array of build.transport.php for the TVs, they won't be removed.

            xPDOTransport::UPDATE_OBJECT => false,


            Another option might be to create the TVs using $modx->newObject() in the resolver rather than in build.transport.php. In that case, PM might not know to delete them.

            As a last resort, if you know the values of the TVs (or could save them to a file), you could recreate them in the uninstall section of the resolver.

            BTW, your resolver seems kind of muddled to me and I don't think the first part of it will do anything. getObject() will only get a single object, so you'd have to set up the array outside of the loop and call getObject() for each one. If they're all in the same category (as they should be), this would be simpler:

            $categoryId = 12;
            $tvs = $modx->getCollection('modTemplateVar', array('category' => $categoryId));
            

            Also, if there is only one template and you retrieve it with getObject(), you can do this:


            $template = $modx->getObject('modTemplate', array('templatename' => 'myTemplate'));
            
            if ($template) {
              $tvts = $template->getMany('TemplateVarTemplates');
            }
            [ed. note: BobRay last edited this post 13 years, 9 months ago.]
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 10378
              • 375 Posts
              Quote from: BobRay at Jan 02, 2013, 04:39 PM
              BTW, your resolver seems kind of muddled to me and I don't think the first part of it will do anything. getObject() will only get a single object, so you'd have to set up the array outside of the loop and call getObject() for each one. If they're all in the same category (as they should be), this would be simpler:

              $categoryId = 12;
              $tvs = $modx->getCollection('modTemplateVar', array('category' => $categoryId));
              

              Also, if there is only one template and you retrieve it with getObject(), you can do this:


              $template = $modx->getObject('modTemplate', array('templatename' => 'myTemplate'));
              
              if ($template) {
                $tvts = $template->getMany('TemplateVarTemplates');
              }

              Hi Bob, thanks for your help,

              the MODx rtfm doesn't have an example on how to build a tv resolver, so I borrowed it from the Articles plugin which I thought it should be fine, as it is written by splittingred. ;-)

              I don't know exactly what you mean with your first code example. Why should I hard code the categoryID? At the installation time I don't know the ID of the category!?

              Regarding your second code sample - I'd like to hold things universal. Possibly I'd like to add another template at later time...
                Freelancer @bitego http://www.bitego.com
                ---
                GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
                More infos here: http://www.bitego.com/extras/goodnews/
                • 10378
                • 375 Posts
                Quote from: BobRay at Jan 02, 2013, 04:39 PM
                I can never remember the exact details, so you'll have to experiment, but I think if you set this in the attributes array of build.transport.php for the TVs, they won't be removed.

                xPDOTransport::UPDATE_OBJECT => false,


                Another option might be to create the TVs using $modx->newObject() in the resolver rather than in build.transport.php. In that case, PM might not know to delete them.

                The problem is I only want to preserve one single TV, all others can be deleted.
                  Freelancer @bitego http://www.bitego.com
                  ---
                  GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
                  More infos here: http://www.bitego.com/extras/goodnews/
                  • 3749
                  • 24,544 Posts
                  All the methods I described could be used for that. With the UPDATE_OBJECT option, you'd have to put that TV in a separate vehicle.

                  I think another way would be to have a validator attached to the first vehicle that gets installed (before the one with the TVs), and in the UNINSTALL section, save the TV values to a file. Then, in a resolver connected to the last vehicle in the package, read the values and recreate the TV and the TV values for all resources.
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting