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

    I am trying to figure out a way to do a couple things here:
    1) When a resource is edited use a plugin to strip_tags from the resource content
    2) I am using form customization to hide a category of template variables so a user can not view or edit them. However if someone was tricky enough they could post the variables on their own to change the value. I would like to create a plugin which I am guessing would be on the same hook as #1 to stop editing of those template variables.

    With regards to #2 I know you can restrict category access based on usergroups for this however I still need to load these template variables so I can use them in the mgr for the UI so this is not possible. When restricted to Load only they still have the ability to edit them via the resource editing.

    Any help is appreciated.
      • 38970
      • 59 Posts
      It seems that I could use OnBeforeDocFormSave and put a plugin their to strip the tags, however how do you go about accessing the variables being Posted.
        • 38970
        • 59 Posts
        It seems that you can not change or check to see if a Template Variable has been modified using this hook, am I right regarding this ?
          • 3749
          • 24,544 Posts
          IIRC, the resource fields are in the $scriptProperties array (in the ['data'] member).

          Yes, you can check to see if a TV has been modified, but it's tricky.

          This is from memory, but I think it's correct. Get the new TV value with $resource->getTVValue(), get the old TV value by getting the TV object with $modx->getObject() and using $tv->getValue() or by getting the modTemplateVarResource object directly and pulling the value from there (faster): http://bobsguides.com/modx-object-quick-reference.html#modTemplateVarResource.

          The whole process is surprisingly fast, even for multiple TVs.


          ------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using.
          MODX info for everyone: http://bobsguides.com/modx.html
            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
            • 38970
            • 59 Posts
            Hey Bob,

            Ok I'll give it a shot, is there a way to return false or to stop the update process once I have determined if they have been changed or return true if they can proceed ?

            Thanks again
              • 38970
              • 59 Posts
              Here is the code that I came up with so far, however the saving box keeps coming up and keeps spinning but I cant see any errors since its ajaxing it. It not printing the value in the error log though.

              <?php
              $restrictedTvs = array(27,24,33,28,26,32,34);
              //get the new
              $prop =& $scriptProperties;
              
              //stored in the data key
              $prop = $prop['data'];
              
              
              //get the old value
              $tv = $modx->getObject('modTemplateVar',27);
              $v = $tv->getValue($modx->resource->get('id'));
              $modx->log(modX::LOG_LEVEL_ERROR, 'PROP: '.print_r($v,true));
              
                • 38970
                • 59 Posts
                I also tried:
                <?php
                $restrictedTvs = array(27,24,33,28,26,32,34);
                //get the new
                $prop =& $scriptProperties;
                
                //stored in the data key
                $prop = $prop['data'];
                
                
                //get the old value
                $tv = $modx->getObject('modTemplateVarResource', array('contentid' => $resource->get('id'),'tmplvarid' => 27));
                $modx->log(modX::LOG_LEVEL_ERROR, 'PROP: '.print_r($tv->getOne('value'),true));
                


                However as soon as omit tmplvarid it works fine with it saving keeps on spinning.
                  • 38970
                  • 59 Posts
                  This Code Works
                  
                  <?php
                  $modx->log(modX::LOG_LEVEL_ERROR, 'RuNNING PLUGIN');
                  $restrictedTV = array(27,24,33,28,26,32,34);
                  $restrictedTVcats = array(21);
                  //get the new
                  $prop =& $scriptProperties;
                  
                  //stored in the data key
                  $prop = $prop['data'];
                  
                  foreach ($restrictedTV as $value) {
                      $tv = $modx->getObject('modTemplateVar',$value); 
                      $V = $tv->getValue($resource->get('id'));
                      $newV =  $resource->get('tv'.$value);
                      //make sure they match the database
                      if(trim($V) != trim($newV)){
                          $modx->log(modX::LOG_LEVEL_ERROR, 'Restricted TV:'.$newV.'/DB TV:'. $V.' UserID: '.$modx->user->get('id'));
                          return array('error' =>'Invalid Save Fields');
                      }
                              
                  }
                  
                  


                  However when I return an array to error the save out it still saves properly.

                  I looked at the modprocessor.class.php to try and figure it out but now I'm stuck
                  public function fireBeforeSaveEvent() {
                          $preventSave = false;
                          if (!empty($this->beforeSaveEvent)) {
                              /** @var boolean|array $OnBeforeFormSave */
                              $OnBeforeFormSave = $this->modx->invokeEvent($this->beforeSaveEvent,array(
                                  'mode'  => modSystemEvent::MODE_NEW,
                                  'data' => $this->object->toArray(),
                                  $this->primaryKeyField => 0,
                                  $this->objectType => &$this->object,
                              ));                        
                              if (is_array($OnBeforeFormSave)) {
                                  $preventSave = false;  
                                  foreach ($OnBeforeFormSave as $msg) {
                                      if (!empty($msg)) {
                                          $preventSave .= $msg."\n";
                                      }
                                  }
                              } else {
                                  $preventSave = $OnBeforeFormSave;
                              }
                          }
                          return $preventSave;
                      }
                  
                    • 38970
                    • 59 Posts
                    OK i got it
                    
                    <?php
                    $modx->log(modX::LOG_LEVEL_ERROR, 'RuNNING PLUGIN');
                    $restrictedTV = array(27,24,33,28,26,32,34);
                    $restrictedTVcats = array(21);
                    //get the new
                    $prop =& $scriptProperties;
                    
                    //stored in the data key
                    $prop = $prop['data'];
                    
                    foreach ($restrictedTV as $value) {
                        $tv = $modx->getObject('modTemplateVar',$value); 
                        $V = $tv->getValue($resource->get('id'));
                        $newV =  $resource->get('tv'.$value);
                        //make sure they match the database
                        if(trim($V) != trim($newV)){
                    								//this will PrevenSave and return an error box
                    								$modx->event->output('Error You Fucker');
                    								//this will be sent to the Error Log
                            return 'Restricted TV:'.$newV.'/DB TV:'. $V.' UserID: '.$modx->user->get('id');
                        }
                                
                    }
                    

                    took me a bit to figure out that you had to set the output and then what you returned will go in your error log
                      • 38970
                      • 59 Posts
                      Ok Now I am trying to use strip tags on content and everything posted because html tags wont belong in any of the resource's these members have access to:

                      <?php
                          if ($modx->user->isMember('PayingMembers') ) {
                              $prop = $scriptProperties;
                              $prop = $prop['data'];
                              foreach($prop as $key => $p){
                                  $c = strip_tags($p);
                                  $k = substr($key, 0, 2);
                                  if($k == 'tv'){
                      												$modx->log(modX::LOG_LEVEL_ERROR, 'TV VAL'.$key.' '.$k  ); 
                                      $resource->setTVValue($key,$c); 
                      																$modx->log(modX::LOG_LEVEL_ERROR, 'TV SETTING '.$c );
                                  }else{
                                      $resource->set($key,$c);     
                                  }
                      
                              }
                      }
                      


                      It works fine for everything except Template Variables. Any thoughts ?