We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38255
    • 8 Posts
    The task for me was to store file on the server, put path to TV and then if need remove the file and the TV. Removing snippet looks like that:
    <?php
    //getting the tv object
    $tvToOperateWith = $modx->getObject('modTemplateVar', $TVid) or die('could not get an object');
    
    // removing file which is pointed by the tv
    
    // trying to get media source parameters
    $mediaSourceOfThisTv = $tvToOperateWith->getSource(); //is object, http://api.modx.com/revolution/2.2/db_core_model_modx_modtemplatevar.class.html#\modTemplateVar
    $propertiesOfMS = $mediaSourceOfThisTv->getPropertyList(); //is array
    $basePathMS = $propertiesOfMS['basePath'];
    
    $basePath = $mediaSourceOfThisTv->getOption('base_path');
    $pathToFileToBeRemoved = $basePath . $basePathMS . $tvToOperateWith->getValue($resID); //getting the value of tv which belongs to resource with ID == $resID
    
    $modx->getService('fileHandler','modFileHandler');
    $fileToBeRemoved = $modx->fileHandler->make($pathToFileToBeRemoved);
    if (!is_object($fileToBeRemoved) || !($fileToBeRemoved instanceof modFile)) die('Not a file!');
    
     
    if (!$fileToBeRemoved->remove()) {
       die('Could not remove file.');
    }
    
    // file removed successfully, cleaning the TV
    $tvToOperateWith->setValue($resID, '');
    $tvToOperateWith->save();
    
    return $pathToFileToBeRemoved;
    


    Can I replace the lines to get the 'basePath' property of Media Source
    $mediaSourceOfThisTv = $tvToOperateWith->getSource();
    $propertiesOfMS = $mediaSourceOfThisTv->getPropertyList();
    $basePathMS = $propertiesOfMS['basePath'];
    

    to look like simple $tvToOperateWith->getSource()->getBasePath()? Because there certainly is such a way, I'm sure, but for the whole night I couldn't figure out how to do this. [ed. note: barsrf last edited this post 14 years, 5 months ago.]
      • 28215
      • 4,149 Posts
      Try this:
      $tvValue = $tv->getValue($resourceId);
      $source = $tv->getSource();
      $finalPath = $source->getBasePath().'/'.ltrim($tvValue,'/');
      
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 38255
        • 8 Posts
        Thank you, Shaun, for quick answer. The matter is that
        $source->getBasePath()
        returns or nothing, or something unexpected. I've tried to check what it returns by
        gettype($source->getBasePath())

        but it causes some error that I can't catch.
        method_exists($source->getBasePath())
        returns TRUE.
        I use Revolution 2.2.0pl2.

        is_subclass_of($source, 'modFileMediaSources'); //returns true
        method_exists($source, 'getBases'); //returns true
        

        I tried to check if $source is an instance of modFileMediaSources. If I haven't mistaken, getBases is a specific method of this class? But again, after all of this
        $source->getBases();

        causes some error. [ed. note: barsrf last edited this post 14 years, 5 months ago.]
          • 28215
          • 4,149 Posts
          What error does it cause?
            shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
            • 38255
            • 8 Posts
            Honestly I don't know exactly. Just nothing happens, and seems that snippet doesn't work. Where can I see what is happening when this $source->getBases() executes?
              • 38255
              • 8 Posts
              May be it's that?
              [2012-04-18 07:14:09] (ERROR @ ...core/model/modx/sources/modfilemediasource.class.php : 39) PHP warning: Missing argument 1 for modFileMediaSource::getBases(), called in ...core/cache/includes/elements/modsnippet/44.include.cache.php on line 17 and defined
                • 28215
                • 4,149 Posts
                Hm, maybe. Try passing ->getBases('');
                  shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                  • 38255
                  • 8 Posts
                  The same again. Still nothing happens, and error.log is clear for that.
                  I tried to comment code in modFileMediaSource.class.php, and it seems that bad things happens after
                          $path = $this->fileHandler->sanitizePath($path)

                  in getBases function.

                    • 38255
                    • 8 Posts
                    It seems that I solved this issue for me.
                    The things come much better if put
                    $source->initialize();
                    // before
                    $source->getBases('');
                    // and others $source->gets
                    

                    Thank you, Shaun, for great work!
                      • 41592 ☆ A M B ☆
                      • 83 Posts
                      hi guys, How can I set properties to a media sources?
                      I trying to import with my 3CMP but I cannot set any properties.


                      $swingMS['crociere_listview_img'] = $modx->newObject('sources.modMediaSource');
                      $swingMS['crociere_listview_img']->fromArray(array(
                                  'name' => 'Crociere ListView Path',
                                  'description' => '',
                                  'class_key' => 'sources.modFileMediaSource',
                      'properties'=>array('
                      a:3:{s:8:\"basePath\";
                      a:6:{s:4:\"name\";s:8:\"basePath\";s:4:\"desc\";s:23:\"prop_file.basePath_desc\";s:4:\"type\";s:9:\"textfield\";s:7:\"options\";
                      a:0:{}s:5:\"value\";s:57:\"assets/components/swingtime/images/crociere_listview_img/\";s:7:\"lexicon\";s:11:\"core:source\";}s:7:\"baseUrl\";
                      a:6:{s:4:\"name\";s:7:\"baseUrl\";s:4:\"desc\";s:22:\"prop_file.baseUrl_desc\";s:4:\"type\";s:9:\"textfield\";s:7:\"options\";
                      a:0:{}s:5:\"value\";s:57:\"assets/components/swingtime/images/crociere_listview_img/\";s:7:\"lexicon\";s:11:\"core:source\";}i:9;
                      a:6:{s:4:\"name\";i:9;s:4:\"desc\";s:0:\"\";s:4:\"type\";s:9:\"textfield\";s:7:\"options\";
                      a:0:{}s:5:\"value\";
                      a:9:{s:4:\"name\";N;s:4:\"desc\";s:0:\"\";s:5:\"xtype\";s:9:\"textfield\";s:7:\"options\";
                      a:0:{}s:5:\"value\";N;s:7:\"lexicon\";s:0:\"\";s:10:\"overridden\";i:2;s:10:\"desc_trans\";s:0:\"\";s:4:\"menu\";N;}s:7:\"lexicon\";N;}}')
                      
                              ),'',true);
                      


                      thx to all
                        Francesco Mussoni | MODX Ambassador | Skype: solidusite2 | Email: [email protected]