Adding content to a new Standard Page Asset

Hey guys,


Back on the document importer system asset.



I'm able to select a file, access it and suck the text out (currently I'm just working on a text file).

I'm able to create a page_standard asset, name it, link it to a parent and have it appear in the asset manager where I would expect it to appear.



All this is happening as one process. No multiple screens/dialogs etc.



At this point I have the content (text) in $source_data and a page_standard object available to me. This is how the page_standard object looks;

    page_standard Object
(
   [_tmp] => Array
       (
           [use_system_version] =>
           [attributes] => Array
               (
                   [name] => asset_attribute_text Object
                       (
                           [_tmp] => Array
                               (
                               )
                            [id] => 145
                            [name] => name
                            [uniq] => 0
                            [description] => The full name of the page
                            [_params] => Array
                                (
                                    [max_length] => 0
                                )

                            [_default_value] => 
                            [_edit_params] => Array
                                (
                                    [width] => 0
                                    [height] => 1
                                )

                            [value] => GoldDoc2
                            [processed] => 
                        )

                    [short_name] => asset_attribute_text Object
                        (
                            [_tmp] => Array
                                (
                                )

                            [id] => 146
                            [name] => short_name
                            [uniq] => 0
                            [description] => The short name of the page (often used in menus)
                            [_params] => Array
                                (
                                    [max_length] => 0
                                )

                            [_default_value] => 
                            [_edit_params] => Array
                                (
                                    [width] => 0
                                    [height] => 1
                                )

                            [value] => GoldDoc2
                            [processed] => 
                        )

                )

            [paths] => Array
                (
                    [0] => golddoc2
                )

        )

    [id] => 798
    [version] => 0.0.8
    [name] => GoldDoc2
    [short_name] => GoldDoc2
    [status] => 2
    [languages] => 
    [charset] => 
    [force_secure] => 0
    [created] => 1100756712
    [created_userid] => 5
    [updated] => 1100756720
    [updated_userid] => 5
    [published] => 
    [published_userid] => 
    [_is_cacheable] => 
    [vars] => Array
        (
            [name] => Array
                (
                    [attributeid] => 145
                    [type] => text
                    [value] => GoldDoc2
                )

            [short_name] => Array
                (
                    [attributeid] => 146
                    [type] => text
                    [value] => GoldDoc2
                )

        )

    [_available_keywords] => Array
        (
        )

    [_weightings] => 
    [data_path_suffix] => assets/page_standard/798
    [data_path] => /var/www/mysource_matrix_3-1-0-RC1/data/private/assets/page_standard/798
    [data_path_public] => /var/www/mysource_matrix_3-1-0-RC1/data/public/assets/page_standard/798
    [_ser_attrs] => 
)</pre><br />

My questions;
How do access the newly created Page Content for this page?

How do I add/update/delete Content DIV assets in this Page Content asset?

Once I can do this I'll be ready to rip open the OOo/StarOffice/Word2k3 files and suck content out of those.

Hmm…


Will check out &$GLOBALS['SQ_SYSTEM']->am->getChildren($assetid) in the morning…



Hopefully I've just halfway answered my first question… :slight_smile:

Check out http://forums.matrix.squiz.net/index.php?showtopic=369


It sounds like we’re working on something similar. I’m building a script to migrate content between two cms (currently matrix is the only target). I can create the new site, import users, files and create standard pages but I still need to add the code to set the HTML for them. Soon… :rolleyes:

Heh :slight_smile: I saw that post. That's was scary. For what I'm trying to do that looked to be a ridiculous amount of work.

I think I've got this cracked. This might also apply to you too, buggy.


The situation I'm in has a Standard page having just been created and the content is being added at the same time. This isn't the same as adding content later. We should be able to safely assume that no one else has managed to get into the Asset Manager, aquire a lock, open the WYSIWYG editor, edit something and commit the changes in the fraction of a second the script needs to do this part. Buggy, I'm thinking that the import script you're working on isn't going to be used that often also. Suck in the data and run the site from MSM from then on?



Anyway, this is what I've got;

            // OK.  At this point we have a blank Standard Page asset hanging off
            // the selected parent asset.  Now we need to take the content we got
            // from the selected file and insert it in the Content DIV in this
            // new page_standard item.
            // Grab the child assets of the Standard Page ($page->id) we just made
            $page_children = &$GLOBALS['SQ_SYSTEM']->am->getChildren($page->id);
            // This appears to have returned an array of all children and their 
            // descendants also.  This is inthe form [AssetID]=>"asset_type"
            foreach ($page_children as $child_id => $child_type) {
              // In this case (newly created asset) the asset we want is the last
              // on the list.  Normally array_pop(array_reverse($page_children))
              // would giv us what we want but this doesn't appear to be a keyed
              // array.  reversing it resets the numbering.  This is the less 
              // than elegant fallback option.
              if ($child_type == "content_type_wysiwyg") {
                break; // out of this loop
              }
            }
            
            // We have the ID and the Type of asset.  Fetch it.
            $child = &$GLOBALS['SQ_SYSTEM']->am->getAsset($child_id, $child_type);
    
            // Fortunatly the content all lives in teh "html" attribute.  All we
            // need to do is update that and save it.
            // During testing I'm using a plain text file for the contents of
            // $source_data.
            if ($child->setAttrValue("html", nl2br($source_data))) {
              // We've managed to set the "html" attribute ok.  now we need to 
              // Lock the asset to save it.
              if ($GLOBALS['SQ_SYSTEM']->am->acquireLock($child_id, 'attributes')) {
                // You really need an explaination for this line?
                $child->saveAttributes();
                $GLOBALS['SQ_SYSTEM']->am->releaseLock($child_id, 'attributes');
              } else {
                // Bugger
                trigger_error('Unable to aquire Lock on the new pages Content DIV', E_USER_ERROR);
              }
            } else {
              // Bugger
              trigger_error('Unable to set html content.', E_USER_ERROR);
            }


Please excuse the comment:code ratio. ;)

Basically the magick bits are;
    $child->setAttrValue("html", nl2br($source_data))
    $GLOBALS['SQ_SYSTEM']->am->acquireLock($child_id, 'attributes')
    $child->saveAttributes()


Is this what you were after Buggy?

I would think you'd need to acquire the lock before setting the attributes, incase someone comes along (hey, it could happen!) and aquires the lock from you. :slight_smile:

Unless they somehow hijack my session while I'm running the script then the only crucial point would be the saving of the data. :slight_smile:


I found that I didn't need to lock anything until I actually tried saving the attributes. So lock/save/release seemed sensible as it locked up the asset for the least amount of time.

I don't think locking is a big issue for what I'm doing. The whole point is that I run my script to import an entire site. No one should be editing the site while I import it. I realise that gaining locks is important but I'm struggling enough with what I'm doing. Perhaps when there's more developer documentation I'll fix it.

Heh :slight_smile: I wish I had that sort of time…