Deleteing Assets

Hey all,


Struggling a little here. I think I've located the right functions though. This is the code I'm currently working with.


            foreach ($page_children as $asset_id => $asset_type) {
              echo "Fetch link for $asset_id";
              if ($this_link = &$GLOBALS['SQ_SYSTEM']->am->getLinkByID($asset_id)) {
                echo "Got link!.  This would be deleted.
";
                print_r($this_link);
                echo "
";
            echo "Delete $asset_id";
            //$GLOBALS['SQ_SYSTEM']->am->deleteAssetLink($this_link["linkid"]);
            echo "Done $asset_id";
          } else {
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo "bugger<pre>";
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print_r($this_link);
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo "</pre>";
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;trigger_error("bugger", E_USER_ERROR);
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
 &nbsp; &nbsp; &nbsp; &nbsp;}</pre><br />

This is the structure I'm working on;
[img]http://www.babel.com.au/gold/d.php/msm_sample1.png[/img]

What I'm trying to do is delete "anything" and "Content DIV". I'm able to narrow it down to just these assets fine. I have the following being used for the loop in the above code snippet.
    Array
    (
        [905] => bodycopy_div
        [881] => bodycopy_div
    )


The first time I ran the code I got no feedback when 905 was worked on and an error (I missed it, sorry) on 881. I assumed the lack of feedback meant no error occured. I was expecting 905 ("anything") to have disappeared from the asset manager. However, the above image is the current state (after reloading the AM.

Now when I run the same code I get the following;
    The destination assetid (page_standard) that was selected is: 879
WORKING: page_standard
The destination asset is a page_standard. We need to delete the content and insert the new content.

AssetManager :
children:

Array
(
 &nbsp; &nbsp;[905] => bodycopy_div
 &nbsp; &nbsp;[881] => bodycopy_div
)

Fetch link for 905
bugger


MySource Error
File: &nbsp;[SYSTEM_ROOT]/packages/babelext/system/doc_importer/doc_importer_edit_fns.inc &nbsp;Line: &nbsp;234
Message: &nbsp;bugger
Backtrace: &nbsp;Show</pre><br />

This suggests that the link no longer exists. If that's the case why is the asset still appearing in the Asset Manager?

Or have I approached this all wrong?

Gday mate,


This line is going to have a problem.

        if ($this_link = &$GLOBALS['SQ_SYSTEM']->am->getLinkByID($asset_id)) {


Asset_Manager::getLinkByID() is expecting an linkid not an assetid.

So you are going to probably need to do something like this :
NOTE: I assume you are dealing with standard pages - correct ???

    $bodycopy = &$standard_page->getBodycopy();
    // get the links to the containers (DIV or TABLEs) 
    $container_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_TYPE_2, 'bodycopy_container', false);
foreach ($container_links as $container_data) {
 &nbsp; &nbsp;if (!$bodycopy->deleteLink($container_data['linkid'])) {
 &nbsp; &nbsp; &nbsp; &nbsp;trigger_error('bugger', E_USER_ERROR); // or what ever
 &nbsp; }
}</pre><br />

Also note that the we are calling deleteLink() fn of the bodycopy asset rather than the deleteAssetLink() in the asset manager. This is the way it should be done (granted that Asset_Manager::deleteAssetLink() doesn't exactly say it should only be called by deleteLink()).

Reason behind this is that if the asset needs to do any extra processing on a delete it can.

That seems to have done the trick.


Yep. Spotted deleteAssetLink() about 2-3 minutes after making the post. Should have made a comment on that.



Thanks Blair.