OSDN Git Service

CHANGE: ITEMクラスを静的メソッド集として整備
authorsakamocchi <o-takashi@sakamocchi.jp>
Fri, 9 Mar 2012 04:02:11 +0000 (13:02 +0900)
committersakamocchi <o-takashi@sakamocchi.jp>
Fri, 9 Mar 2012 04:02:11 +0000 (13:02 +0900)
ITEMクラスを利用する際、コアスクリプトはインスタンスを作成せずに静的メソッドを利用しています。その方針に従い、クラス内メソッドをstatic宣言しました。

そのため、コンストラクトしてインスタンスを利用するようなプラグインではエラーが発生します。この部分では下位互換性を破棄し、プラグインの実装変更に期待します。

この他、コードクリーンアップやTODOの追加などを行いました。

nucleus/libs/ITEM.php

index cac28be..e4e05e2 100644 (file)
@@ -13,7 +13,7 @@
 /**
  * @license http://nucleuscms.org/license.txt GNU General Public License
  * @copyright Copyright (C) 2002-2009 The Nucleus Group
- * @version $Id: ITEM.php 1593 2011-11-01 18:42:03Z gregorlove $
+ * @version $Id: ITEM.php 1668 2012-02-19 14:36:44Z sakamocchi $
  */
 
 /**
  */
 class ITEM
 {
-
        /**
-        * Item ID (int)
+        * ITEM::$actiontypes
+        * actiontype list for handling items
+        * 
+        * @static
         */
-       public $itemid;
-
-
+       static private $actiontypes
+               = array('addnow', 'adddraft', 'addfuture', 'edit', 'changedate', 'backtodrafts', 'delete');
+       
        /**
+        * ITEM::__construct()
         * Creates a new ITEM object
-        * @param int $item_id
+        * 
+        * @deprecated
+        * @param void
+        * @return void
         */
-       public function __construct($item_id)
+       public function __construct()
        {
-               $this->itemid = $item_id;
+               return;
        }
-
-
+       
        /**
         * Returns one item with the specific itemid
         *
@@ -47,12 +52,12 @@ class ITEM
         * @param bool $allow_future
         * @return mixed
         */
-       public function getitem($item_id, $allow_draft, $allow_future)
+       static public function getitem($item_id, $allow_draft, $allow_future)
        {
                global $manager;
-
+               
                $item_id = intval($item_id);
-
+               
                $query = 'SELECT ' .
                        '`i`.`idraft` AS `draft`, ' .
                        '`i`.`inumber` AS `itemid`, ' .
@@ -70,24 +75,24 @@ class ITEM
                        'FROM `%s` AS `i`, `%s` AS `m`, `%s` AS `b` ' .
                        'WHERE `i`.`inumber` = %d ' .
                        'AND `i`.`iauthor` = `m`.`mnumber` ' .
-                       'AND `i`.`iblog` = `b`.`bnumber` ';
-
+                       'AND `i`.`iblog` = `b`.`bnumber`';
+               
                $query = sprintf($query, sql_table('item'), sql_table('member'), sql_table('blog'), $item_id);
-
+               
                if ( !$allow_draft )
                {
                        $query .= 'AND `i`.`idraft` = 0 ';
                }
-
+               
                if ( !$allow_future )
                {
                        $blog =& $manager->getBlog(getBlogIDFromItemID($item_id));
                        $query .= 'AND `i`.`itime` <= ' . mysqldate($blog->getCorrectTime());
                }
-
+               
                $query .= ' LIMIT 1';
                $result = sql_query($query);
-
+               
                if ( sql_num_rows($result) == 1 )
                {
                        $aItemInfo = sql_fetch_assoc($result);
@@ -98,79 +103,70 @@ class ITEM
                {
                        return 0;
                }
-
        }
-
-
+       
        /**
+        * ITEM::createFromRequest()
         * Tries to create an item from the data in the current request (comes from
         * bookmarklet or admin area
         *
-        * Returns an array with status info:
-        * status = 'added', 'error', 'newcategory'
-        *
         * @static
+        * @param       void
+        * @return      array   (status = added/error/newcategory, message)
         */
-       function createFromRequest()
+       static public function createFromRequest()
        {
                global $member, $manager;
-
-               $i_author = $member->getID();
-               $i_body = postVar('body');
-               $i_title = postVar('title');
-               $i_more = postVar('more');
-               $i_actiontype = postVar('actiontype');
-               $i_closed = intPostVar('closed');
-               $i_hour = intPostVar('hour');
-               $i_minutes = intPostVar('minutes');
-               $i_month = intPostVar('month');
-               $i_day = intPostVar('day');
-               $i_year = intPostVar('year');
-               $i_catid = postVar('catid');
-               $i_draftid = intPostVar('draftid');
-
+               
+               /*
+                * TODO: these values from user agent should be validated but not implemented yet
+                */
+               $i_author               = $member->getID();
+               $i_body                 = postVar('body');
+               $i_title                = postVar('title');
+               $i_more                 = postVar('more');
+               $i_actiontype   = postVar('actiontype');
+               $i_closed               = intPostVar('closed');
+               $i_hour                 = intPostVar('hour');
+               $i_minutes              = intPostVar('minutes');
+               $i_month                = intPostVar('month');
+               $i_day                  = intPostVar('day');
+               $i_year                 = intPostVar('year');
+               $i_catid                = postVar('catid');
+               $i_draftid              = intPostVar('draftid');
+               
                if ( !$member->canAddItem($i_catid) )
                {
                        return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
                }
-
-               if (!$i_actiontype)
-                       $i_actiontype = 'addnow';
-
-               switch ( $i_actiontype )
+               
+               if ( !in_array($i_actiontype, self::$actiontypes) )
                {
-                       case 'adddraft':
-                               $i_draft = 1;
-                       break;
-
-                       case 'addfuture':
-                       case 'addnow':
-                       default:
-                               $i_draft = 0;
-                       break;
+                       $i_actiontype = 'addnow';
                }
-
+               
+               $i_draft = (integer) ( $i_actiontype == 'adddraft' );
+               
                if ( !trim($i_body) )
                {
                        return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
                }
-
+               
                // create new category if needed
-               if ( strstr($i_catid,'newcat') )
+               if ( i18n::strpos($i_catid, 'newcat') )
                {
                        // get blogid
                        list($i_blogid) = sscanf($i_catid, "newcat-%d");
-
+                       
                        // create
                        $blog =& $manager->getBlog($i_blogid);
                        $i_catid = $blog->createNewCategory();
-
+                       
                        // show error when sth goes wrong
                        if ( !$i_catid )
                        {
                                return array('status' => 'error','message' => 'Could not create new category');
                        }
-
                }
                else
                {
@@ -178,24 +174,23 @@ class ITEM
                        $i_blogid = getBlogIDFromCatID($i_catid);
                        $blog =& $manager->getBlog($i_blogid);
                }
-
+               
                if ( $i_actiontype == 'addfuture' )
                {
                        $posttime = mktime($i_hour, $i_minutes, 0, $i_month, $i_day, $i_year);
-
+                       
                        // make sure the date is in the future, unless we allow past dates
                        if ( (!$blog->allowPastPosting()) && ($posttime < $blog->getCorrectTime()) )
                        {
                                $posttime = $blog->getCorrectTime();
                        }
-
                }
                else
                {
                        // time with offset, or 0 for drafts
                        $posttime = $i_draft ? 0 : $blog->getCorrectTime();
                }
-
+               
                if ( $posttime > $blog->getCorrectTime() )
                {
                        $posted = 0;
@@ -205,9 +200,9 @@ class ITEM
                {
                        $posted = 1;
                }
-
+               
                $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft, $posted);
-
+               
                //Setting the itemOptions
                $aOptions = requestArray('plugoption');
                NucleusPlugin::apply_plugin_options($aOptions, $itemid);
@@ -223,13 +218,13 @@ class ITEM
                                )
                        )
                );
-
+               
                if ( $i_draftid > 0 )
                {
                        // delete permission is checked inside ITEM::delete()
-                       ITEM::delete($i_draftid);
+                       self::delete($i_draftid);
                }
-
+               
                // success
                if ( $i_catid != intRequestVar('catid') )
                {
@@ -239,73 +234,78 @@ class ITEM
                {
                        return array('status' => 'added', 'itemid' => $itemid);
                }
-
        }
-
-
+       
        /**
-         * Updates an item
-         *
-         * @static
-         */
-       function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0)
+        * ITEM::update()
+        * Updates an item
+        *
+        * @static
+        * @param       integer $itemid item id
+        * @param       integer $catid  category id
+        * @param       string  $title  title
+        * @param       string  $body   body text
+        * @param       string  $more   more text
+        * @param       boolean $closed closed or not
+        * @param       boolean $wasdraft       previously draft or not
+        * @param       boolean $publish        published or not
+        * @param       timestamp       $timestamp      timestamp
+        * @return      void
+        * 
+        */
+       static public function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0)
        {
                global $manager;
-
-               $itemid = intval($itemid);
-
-               // make sure value is 1 or 0
-               if ( $closed != 1 )
-               {
-                       $closed = 0;
-               }
-
+               
+               $itemid = (integer) $itemid;
+               $closed = (boolean) $closed;
+               
                // get destination blogid
                $new_blogid = getBlogIDFromCatID($catid);
                $old_blogid = getBlogIDFromItemID($itemid);
-
+               
                // move will be done on end of method
                if ( $new_blogid != $old_blogid )
                {
                        $moveNeeded = 1;
                }
-
+               
                $blog =& $manager->getBlog($new_blogid);
-
+               
                // begin if: convert line breaks to <br/>
                if ( $blog->convertBreaks() )
                {
                        $body = addBreaks($body);
                        $more = addBreaks($more);
-               } // end if
-
+               }
+               
                // call plugins
                $manager->notify('PreUpdateItem', array(
-                       'itemid' => $itemid,
-                       'title' => &$title,
-                       'body' => &$body,
-                       'more' => &$more,
-                       'blog' => &$blog,
-                       'closed' => &$closed,
-                       'catid' => &$catid
+                       'itemid'        => $itemid,
+                       'title'         => &$title,
+                       'body'          => &$body,
+                       'more'          => &$more,
+                       'blog'          => &$blog,
+                       'closed'        => &$closed,
+                       'catid'         => &$catid
                        )
                );
-
+               
                // update item itself
                $query =  'UPDATE ' . sql_table('item')
-                          . ' SET'
-                          . " ibody = '" . sql_real_escape_string($body) . "',"
-                          . " ititle = '" . sql_real_escape_string($title) . "',"
-                          . " imore = '" . sql_real_escape_string($more) . "',"
-                          . " iclosed = " . intval($closed) . ","
-                          . " icat = " . intval($catid);
-
+                               . ' SET'
+                               . " ibody = '" . sql_real_escape_string($body) . "',"
+                               . " ititle = '" . sql_real_escape_string($title) . "',"
+                               . " imore = '" . sql_real_escape_string($more) . "',"
+                               . " iclosed = " . intval($closed) . ","
+                               . " icat = " . intval($catid);
+               
                // if we received an updated timestamp that is in the past, but past posting is not allowed, reject that date change (timestamp = 0 will make sure the current date is kept)
                if ( (!$blog->allowPastPosting()) && ($timestamp < $blog->getCorrectTime()) )
                {
                        $timestamp = 0;
-               } // end if
-
+               }
+               
                // begin if: post is in the future
                if ( $timestamp > $blog->getCorrectTime(time()) )
                {
@@ -316,28 +316,27 @@ class ITEM
                {
                        $isFuture = 0;
                        $query .= ', iposted = 1';
-               } // end if
-
+               }
+               
                if ( $wasdraft && $publish )
                {
                        // set timestamp to current date only if it's not a future item
                        // draft items have timestamp == 0
                        // don't allow timestamps in the past (unless otherwise defined in blogsettings)
                        $query .= ', idraft = 0';
-
+                       
                        if ( $timestamp == 0 )
                        {
                                $timestamp = $blog->getCorrectTime();
                        }
-
+                       
                        // send new item notification
                        if ( !$isFuture && $blog->getNotifyAddress() && $blog->notifyOnNewItem() )
                        {
                                $blog->sendNewItemNotification($itemid, $title, $body);
                        }
-
-               } // end if
-
+               }
+               
                // save back to drafts
                if ( !$wasdraft && !$publish )
                {
@@ -345,27 +344,27 @@ class ITEM
                        // set timestamp back to zero for a draft
                        $query .= ', itime = ' . mysqldate($timestamp);
                }
-
+               
                // update timestamp when needed
                if ( $timestamp != 0 )
                {
                        $query .= ', itime = ' . mysqldate($timestamp);
                }
-
+               
                // make sure the correct item is updated
                $query .= ' WHERE inumber = ' . $itemid;
-
+               
                // off we go!
                sql_query($query);
-
+               
                $manager->notify('PostUpdateItem', array('itemid' => $itemid));
-
+               
                // when needed, move item and comments to new blog
                if ( $moveNeeded )
                {
-                       ITEM::move($itemid, $catid);
+                       self::move($itemid, $catid);
                }
-
+               
                //update the itemOptions
                $aOptions = requestArray('plugoption');
                NucleusPlugin::apply_plugin_options($aOptions);
@@ -381,22 +380,26 @@ class ITEM
                                )
                        )
                );
+               return;
        }
-
-
+       
        /**
+        * ITEM::move()
         * Move an item to another blog (no checks)
         *
         * @static
+        * @param       integer $itemid
+        * @param       integer $new_catid
+        * @return      void
         */
-       function move($itemid, $new_catid) {
+       static public function move($itemid, $new_catid)
+       {
                global $manager;
-
-               $itemid = intval($itemid);
-               $new_catid = intval($new_catid);
-
-               $new_blogid = getBlogIDFromCatID($new_catid);
-
+               
+               $itemid         = (integer) $itemid;
+               $new_catid      = (integer) $new_catid;
+               $new_blogid     = getBlogIDFromCatID($new_catid);
+               
                $manager->notify(
                        'PreMoveItem',
                        array(
@@ -405,16 +408,17 @@ class ITEM
                                'destcatid' => $new_catid
                        )
                );
-
-
+               
                // update item table
-               $query = 'UPDATE '.sql_table('item')." SET iblog=$new_blogid, icat=$new_catid WHERE inumber=$itemid";
+               $query = "UPDATE %s SET iblog=%d, icat=%d WHERE inumber=%d";
+               $query = sprintf($query, sql_table('item'), $new_blogid, $new_catid, $itemid);
                sql_query($query);
-
+               
                // update comments
-               $query = 'UPDATE '.sql_table('comment')." SET cblog=" . $new_blogid." WHERE citem=" . $itemid;
+               $query = "UPDATE %s SET cblog=%d WHERE citem=%d";
+               $query = sprintf($query, sql_table('comment'), $new_blogid, $itemid);
                sql_query($query);
-
+               
                $manager->notify(
                        'PostMoveItem',
                        array(
@@ -423,23 +427,23 @@ class ITEM
                                'destcatid' => $new_catid
                        )
                );
+               return;
        }
        
        /**
         * ITEM::delete()
         * Deletes an item
         * 
-        * @param       Void
-        * @return      Void
+        * @param       integer $itemid
+        * @return      void
         */
-       function delete($itemid)
+       static public function delete($itemid)
        {
                global $manager, $member;
                
                $itemid = (integer) $itemid;
                
-               // check to ensure only those allow to alter the item can
-               // proceed
+               // check permission
                if ( !$member->canAlterItem($itemid) )
                {
                        return 1;
@@ -448,11 +452,13 @@ class ITEM
                $manager->notify('PreDeleteItem', array('itemid' => $itemid));
                
                // delete item
-               $query = 'DELETE FROM '.sql_table('item').' WHERE inumber=' . $itemid;
+               $query = "DELETE FROM %s WHERE inumber=%d";
+               $query = sprintf($query, sql_table('item'), $itemid);
                sql_query($query);
                
                // delete the comments associated with the item
-               $query = 'DELETE FROM '.sql_table('comment').' WHERE citem=' . $itemid;
+               $query = "DELETE FROM %s WHERE citem=%d";
+               $query = sprintf($query, sql_table('comment'), $itemid);
                sql_query($query);
                
                // delete all associated plugin options
@@ -464,106 +470,117 @@ class ITEM
        }
        
        /**
+        * ITEM::exists()
         * Returns true if there is an item with the given ID
         *
         * @static
+        * @param       integer $itemid
+        * @param       boolean $future
+        * @param       boolean $draft
+        * @return      boolean exists or not
         */
-       function exists($id,$future,$draft) {
+       static public function exists($itemid, $future, $draft)
+       {
                global $manager;
-
-               $id = intval($id);
-
-               $r = 'select * FROM '.sql_table('item').' WHERE inumber='.$id;
-               if (!$future) {
-                       $bid = getBlogIDFromItemID($id);
-                       if (!$bid) return 0;
-                       $b =& $manager->getBlog($bid);
-                       $r .= ' and itime<='.mysqldate($b->getCorrectTime());
+               
+               $itemid = (integer) $itemid;
+               $query = 'select * FROM '.sql_table('item').' WHERE inumber='.$itemid;
+               
+               if ( !$future )
+               {
+                       $blogid = getBlogIDFromItemID($itemid);
+                       if ( !$blogid )
+                       {
+                               return 0;
+                       }
+                       $blog =& $manager->getBlog($blogid);
+                       $query .= ' and itime<=' . mysqldate($blog->getCorrectTime());
                }
-               if (!$draft) {
-                       $r .= ' and idraft=0';
+               if ( !$draft )
+               {
+                       $query .= ' and idraft=0';
                }
-               $r = sql_query($r);
-
-               return (sql_num_rows($r) != 0);
+               $result = sql_query($query);
+               return ( sql_num_rows($result) != 0 );
        }
-
+       
        /**
-        * Tries to create an draft from the data in the current request (comes from
-        * bookmarklet or admin area
+        * ITEM::createDraftFromRequest()
+        * Tries to create an draft from the data
+        *  in the current request (comes from bookmarklet or admin area)
+        *   Used by xmlHTTPRequest AutoDraft
         *
         * Returns an array with status info:
         * status = 'added', 'error', 'newcategory'
         *
         * @static
+        * @param       void
+        * @return      array   (status = added/error/newcategory, message)
         *
-        * Used by xmlHTTPRequest AutoDraft
         */
-       function createDraftFromRequest() {
+       static public function createDraftFromRequest()
+       {
                global $member, $manager;
-
-               $i_author = $member->getID();
-               $i_body = postVar('body');
-               $i_title = postVar('title');
-               $i_more = postVar('more');
-               //$i_actiontype = postVar('actiontype');
-               $i_closed = intPostVar('closed');
-               //$i_hour = intPostVar('hour');
-               //$i_minutes = intPostVar('minutes');
-               //$i_month = intPostVar('month');
-               //$i_day = intPostVar('day');
-               //$i_year = intPostVar('year');
-               $i_catid = postVar('catid');
-               $i_draft = 1;
-               $type = postVar('type');
-               if ($type == 'edit') {
-                       $i_blogid = getBlogIDFromItemID(intPostVar('itemid'));
-               }
-               else {
+               
+               /*
+                * TODO: these values from user agent should be validated but not implemented yet
+                */
+               $i_author       = $member->getID();
+               $i_body         = postVar('body');
+               $i_title        = postVar('title');
+               $i_more         = postVar('more');
+               $i_closed       = intPostVar('closed');
+               $i_catid        = postVar('catid');
+               $i_draft        = 1;
+               $type           = postVar('type');
+               $i_draftid = intPostVar('draftid');
+               
+               if ( $type == 'edit' )
+               {
+                       $itemid = intPostVar('itemid');
+                       $i_blogid = getBlogIDFromItemID($itemid);
+               }
+               else
+               {
                        $i_blogid = intPostVar('blogid');
                }
-               $i_draftid = intPostVar('draftid');
-
-               if (!$member->canAddItem($i_catid)) {
+               
+               if ( !$member->canAddItem($i_catid) )
+               {
                        return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
                }
-
-               if (!trim($i_body)) {
+               
+               if ( !trim($i_body) )
+               {
                        return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
                }
-
+               
                // create new category if needed
-               if (strstr($i_catid, 'newcat')) {
+               if ( strstr($i_catid, 'newcat') )
+               {
                        // Set in default category
                        $blog =& $manager->getBlog($i_blogid);
                        $i_catid = $blog->getDefaultCategory();
                }
-               else {
+               else
+               {
                        // force blogid (must be same as category id)
                        $i_blogid = getBlogIDFromCatID($i_catid);
                        $blog =& $manager->getBlog($i_blogid);
                }
-
+               
                $posttime = 0;
-
-               if ($i_draftid > 0) {
-                       ITEM::update($i_draftid, $i_catid, $i_title, $i_body, $i_more, $i_closed, 1, 0, 0);
+               
+               if ( $i_draftid > 0 )
+               {
+                       self::update($i_draftid, $i_catid, $i_title, $i_body, $i_more, $i_closed, 1, 0, 0);
                        $itemid = $i_draftid;
                }
-               else {
+               else
+               {
                        $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft);
                }
-
-               // No plugin support in AutoSaveDraft yet
-               //Setting the itemOptions
-               //$aOptions = requestArray('plugoption');
-               //NucleusPlugin::apply_plugin_options($aOptions, $itemid);
-               //$manager->notify('PostPluginOptionsUpdate',array('context' => 'item', 'itemid' => $itemid, 'item' => array('title' => $i_title, 'body' => $i_body, 'more' => $i_more, 'closed' => $i_closed, 'catid' => $i_catid)));
-
-               // success
+               
                return array('status' => 'added', 'draftid' => $itemid);
        }
-
 }
-
-?>