Zikula: A Flexible Open Source Content Management System
home | forum | contact us

Dizkus

Bottom
Adding news items from your module?
  • Posted: 22.06.2006, 00:55
     
    jediping
    rank:
    Helper Helper
    registered:
     November 2004
    Status:
    offline
    last visit:
    12.03.07
    Posts:
    387
    The PTB have decreed that I need to be able to add news items via the module I spend most of my time beating up. I took a look at the "AddStory" module. The biggest problem is that, if I call the postStory function, all I'll get back is a boolean saying if the story was posted. I need the story's ID so I can put it in a table. Short of coding it in my own module, is there a function I can pnMod(API)Func into?
  • Posted: 27.06.2006, 18:39
     
    alarconcepts
    rank:
    Professional Professional
    registered:
     April 2004
    Status:
    offline
    last visit:
    21.01.08
    Posts:
    2723
    From adminapi.php in the Example module, there's an example of return the just-inserted id (in the 'create' function):

    Code

    // Get the ID of the item that we inserted.  It is possible, although
        // very unlikely, that this is different from $nextId as obtained
        // above, but it is better to be safe than sorry in this situation
        $tid = $dbconn->PO_Insert_ID($exampletable, $examplecolumn['tid']);


    --
    Photography | PHP | Other
  • Posted: 27.06.2006, 20:43
     
    jediping
    rank:
    Helper Helper
    registered:
     November 2004
    Status:
    offline
    last visit:
    12.03.07
    Posts:
    387
    I could do that, but my concern would be that someone else might have posted a news article in the meantime (if the person entering the information in the module gets interrupted, for example). I ended up coding the function to add the item to the news myself. Now I can borrow the same code for other use in the future, should I need it.
  • Posted: 27.06.2006, 21:58
     
    rank:
    Moderator Moderator
    registered:
     March 2002
    Status:
    offline
    last visit:
    26.08.08
    Posts:
    7720

    jediping

    is there a function I can pnMod(API)Func into?


    Not yet... This is one of the main features that will be provided by .80. PostNuke will be fully API compliant allowing all modules to inter-communicate.

    -Mark
  • Posted: 27.06.2006, 22:01
     
    jediping
    rank:
    Helper Helper
    registered:
     November 2004
    Status:
    offline
    last visit:
    12.03.07
    Posts:
    387
    I figured as much. [Inigo Montoya voice]I hate waiting[/voice] :D
  • Posted: 28.07.2006, 14:19
     
    SiCk949
    rank:
    Freshman Freshman
    registered:
     June 2006
    Status:
    offline
    last visit:
    30.10.06
    Posts:
    28
    And why not a simple select of id last item? just after the insert
  • Posted: 28.07.2006, 20:33
     
    jediping
    rank:
    Helper Helper
    registered:
     November 2004
    Status:
    offline
    last visit:
    12.03.07
    Posts:
    387
    I do end up using the PO_Insert_ID thing in my code, but because the AddStory module has a redirect at the end of the adding, I think it would hijack my process. In addition to it not returning the story ID. :)

    For those curious, I'll include my function that I ended up creating. Maybe it'll help someone else, until .8 :)

    Code

    <?php
    // add a new travel item to a project in the database

    function projects_travelapi_create($args){
        // Get arguments from argument array
        extract($args);
       
        // Argument check
        if ((!isset($projid)) ||
            (!isset($dates)) ||
            (!isset($locations)) ||
            (!isset($people)) ||
            (!isset($itin))) {
            pnSessionSetVar('errormsg', _MODARGSERROR);
            return false;
        }

        // Security check
        if (!pnSecAuthAction(0, 'projects:projects:', "$projid::", ACCESS_EDIT)) {
            pnSessionSetVar('errormsg', _MODULENOAUTH);
            return false;
        }

        // Get datbase setup
        $dbconn =& pnDBGetConn(true);
        $pntable =& pnDBGetTables();
        $traveltable = $pntable['project_travel'];
        $travelcolumn = &$pntable['project_travel_column'];
        $projecttable = $pntable['project_projects'];
        $projectcolumn = &$pntable['project_projects_column'];
        $storytable=$pntable['stories'];
        $storycolumn=&$pntable['stories_column'];

        $projectinfo=pnModAPIFunc('projects','projects','get',array('projid'=>$projid));
       
        // Get next ID in table
        $nextId = $dbconn->GenId($traveltable);
        $username=pnUserGetVar('uname');
        $userid=pnUserGetVar('uid');
       
        $projecturl=pnModURL('projects', 'projects', 'display', array ('projid' => $projid));

        $bodytext="<b>Travel dates</b><br>".$dates." <br>
                    <b>Locations</b><br>"
    .$locations." <br>
                    <b>People</b><br>"
    .$people." <br>
                    <b>Itinerary</b><br>"
    .$itin."<br>
                    <a href=\""
    .$projecturl."\">Project page</a>";
       
        $title='Travel for '. $projectinfo['title'];

       
        //PostNuke-ify the variables.
        list($userid, $title, $bodytext) = pnVarPrepForStore($userid, $title, $bodytext);

        // Add item to the DB
       
        // change the cid to the travel category id when you upload to the live site.
        // change the cid to the travel topic id when you upload to the live site. (or the other way around. I forget now.)
        $sql = "INSERT INTO $storytable (
                    $storycolumn[sid],
                    $storycolumn[catid],
                    $storycolumn[aid],
                    $storycolumn[title],
                    $storycolumn[time],
                    $storycolumn[hometext],
                    $storycolumn[bodytext],
                    $storycolumn[comments],
                    $storycolumn[counter],
                    $storycolumn[topic],
                    $storycolumn[informant],
                    $storycolumn[notes],
                    $storycolumn[ihome],
                    $storycolumn[themeoverride],
                    $storycolumn[language],
                    $storycolumn[withcomm],
                    $storycolumn[format_type])
                VALUES (
                  '"
    .(int)$nextId."',
                  '0',
                  '"
    .$userid."',
                  '"
    .$title."',
                  NOW(),
                  ' ',
                  '"
    .$bodytext."',
                  '0',
                  '0',
                  '8',
                  '"
    .$username."',
                  'Please use the project page to update this entry.',
                  '0',
                  ' ',
                  ' ',
                  '0',
                  '0')"
    ;
        $dbconn->Execute($sql);
       
        // Check for an error with the database code
        if ($dbconn->ErrorNo() != 0) {
            pnSessionSetVar('errormsg', _STORYCREATEFAILED);
            return false;
        }

        // Get the ID of the item that we inserted.  
        $storyid = $dbconn->PO_Insert_ID($storytable, $storycolumn['sid']);
       
        $nextId = $dbconn->GenId($traveltable);

        //PostNuke-ify the variables.
        list($projid, $storyid) = pnVarPrepForStore($projid, $storyid);

        // Add item to the DB
        $sql = "INSERT INTO $traveltable (
                    $travelcolumn[travid],
                    $travelcolumn[projid],
                    $travelcolumn[storyid])
                VALUES (
                  '"
    .(int)$nextId."',
                  '"
    .$projid."',
                  '"
    .$storyid."')";
                 
        $dbconn->Execute($sql);
       
        // Check for an error with the database code
        if ($dbconn->ErrorNo() != 0) {
            pnSessionSetVar('errormsg', _TRAVELCREATEFAILED);
            return false;
        }

        // Get the ID of the item that we inserted.  
        $travid = $dbconn->PO_Insert_ID($traveltable, $travelcolumn['travid']);
       
        // Update the project's last-updated date.
        $updated = pnModAPIFunc('projects',
                             'projects',
                             'update_lastupdate',
                             array('projid' => $projid));
       
        if (!$updated) {
            pnSessionSetVar('errormsg', _PROJECTUPDATEFAILED);
            return false;
        }

        // Return the id of the newly created item to the calling process
        return $travid;
    }
    ?>

Main Menu

Extensions Database

Documentation

Development

Login

Donate to Zikula