OSDN Git Service

Merge branch 'skinnable-master'
[nucleus-jp/nucleus-next.git] / nucleus / libs / ITEM.php
index c17a925..0fce570 100644 (file)
@@ -1,3 +1,606 @@
+<<<<<<< HEAD
+<?php\r
+\r
+/*\r
+ * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
+ * Copyright (C) 2002-2012 The Nucleus Group\r
+ *\r
+ * This program is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU General Public License\r
+ * as published by the Free Software Foundation; either version 2\r
+ * of the License, or (at your option) any later version.\r
+ * (see nucleus/documentation/index.html#license for more info)\r
+ */\r
+/**\r
+ * @license http://nucleuscms.org/license.txt GNU General Public License\r
+ * @copyright Copyright (C) 2002-2012 The Nucleus Group\r
+ * @version $Id: ITEM.php 1668 2012-02-19 14:36:44Z sakamocchi $\r
+ */\r
+\r
+/**\r
+ * A class representing an item\r
+ *\r
+ */\r
+class Item\r
+{\r
+       /**\r
+        * Item::$actiontypes\r
+        * actiontype list for handling items\r
+        * \r
+        * @static\r
+        */\r
+       static private $actiontypes = array(\r
+               'addnow', 'adddraft', 'addfuture', 'edit',\r
+               'changedate', 'backtodrafts', 'delete'\r
+       );\r
+       \r
+       /**\r
+        * Item::$itemid\r
+        * item id\r
+        * @deprecated\r
+        */\r
+       public $itemid;\r
+       \r
+       /**\r
+        * Item::__construct()\r
+        * Creates a new ITEM object\r
+        * \r
+        * @deprecated\r
+        * @param integer       $item_id        id for item\r
+        * @return void\r
+        */\r
+       public function __construct($item_id)\r
+       {\r
+               $this->itemid = $item_id;\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Item::getitem()\r
+        * Returns one item with the specific itemid\r
+        * \r
+        * @static\r
+        * @param int $item_id\r
+        * @param bool $allow_draft\r
+        * @param bool $allow_future\r
+        * @return mixed\r
+        */\r
+       static public function getitem($item_id, $allow_draft, $allow_future)\r
+       {\r
+               global $manager;\r
+               \r
+               $item_id = (integer) $item_id;\r
+               \r
+               $query = 'SELECT ' .\r
+                       'i.idraft AS draft, ' .\r
+                       'i.inumber AS itemid, ' .\r
+                       'i.iclosed AS closed, ' .\r
+                       'i.ititle AS title, ' .\r
+                       'i.ibody AS body, ' .\r
+                       'm.mname AS author, ' .\r
+                       'i.iauthor AS authorid, ' .\r
+                       'i.itime, ' .\r
+                       'i.imore AS more, ' .\r
+                       'i.ikarmapos AS karmapos, ' .\r
+                       'i.ikarmaneg AS karmaneg, ' .\r
+                       'i.icat AS catid, ' .\r
+                       'i.iblog AS blogid ' .\r
+                       'FROM %s AS i, %s AS m, %s AS b ' .\r
+                       'WHERE i.inumber = %d ' .\r
+                       'AND i.iauthor = m.mnumber ' .\r
+                       'AND i.iblog = b.bnumber ';\r
+               \r
+               $query = sprintf($query, sql_table('item'), sql_table('member'), sql_table('blog'), $item_id);\r
+               \r
+               if ( !$allow_draft )\r
+               {\r
+                       $query .= "AND i.idraft = 0 ";\r
+               }\r
+               \r
+               if ( !$allow_future )\r
+               {\r
+                       $blog =& $manager->getBlog(getBlogIDFromItemID($item_id));\r
+                       $query .= 'AND i.itime <= ' . DB::formatDateTime($blog->getCorrectTime());\r
+               }\r
+               \r
+               $query .= ' LIMIT 1';\r
+               $result = DB::getResult($query);\r
+               \r
+               if ( $result->rowCount() == 1 )\r
+               {\r
+                       $aItemInfo = $result->fetch(PDO::FETCH_ASSOC);\r
+                       $aItemInfo['timestamp'] = strtotime($aItemInfo['itime']);\r
+                       return $aItemInfo;\r
+               }\r
+               return 0;\r
+       }\r
+       \r
+       /**\r
+        * Item::createFromRequest()\r
+        * Tries to create an item from the data in the current request (comes from\r
+        * bookmarklet or admin area\r
+        *\r
+        * @static\r
+        * @param       void\r
+        * @return      array   (status = added/error/newcategory, message)\r
+        * \r
+        */\r
+       static public function createFromRequest()\r
+       {\r
+               global $member, $manager;\r
+               \r
+               /*\r
+                * TODO: these values from user agent should be validated but not implemented yet\r
+                */\r
+               $i_author               = $member->getID();\r
+               $i_body                 = postVar('body');\r
+               $i_title                = postVar('title');\r
+               $i_more                 = postVar('more');\r
+               $i_actiontype   = postVar('actiontype');\r
+               $i_closed               = intPostVar('closed');\r
+               $i_hour                 = intPostVar('hour');\r
+               $i_minutes              = intPostVar('minutes');\r
+               $i_month                = intPostVar('month');\r
+               $i_day                  = intPostVar('day');\r
+               $i_year                 = intPostVar('year');\r
+               $i_catid                = postVar('catid');\r
+               $i_draftid              = intPostVar('draftid');\r
+               \r
+               if ( !$member->canAddItem($i_catid) )\r
+               {\r
+                       return array('status' => 'error', 'message' => _ERROR_DISALLOWED);\r
+               }\r
+               \r
+               if ( !in_array($i_actiontype, self::$actiontypes) )\r
+               {\r
+                       $i_actiontype = 'addnow';\r
+               }\r
+               \r
+               $i_draft = (integer) ( $i_actiontype == 'adddraft' );\r
+               \r
+               if ( !trim($i_body) )\r
+               {\r
+                       return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);\r
+               }\r
+               \r
+               // create new category if needed\r
+               if ( i18n::strpos($i_catid, 'newcat') === 0 )\r
+               {\r
+                       // get blogid\r
+                       list($i_blogid) = sscanf($i_catid, "newcat-%d");\r
+                       \r
+                       // create\r
+                       $blog =& $manager->getBlog($i_blogid);\r
+                       $i_catid = $blog->createNewCategory();\r
+                       \r
+                       // show error when sth goes wrong\r
+                       if ( !$i_catid )\r
+                       {\r
+                               return array('status' => 'error','message' => 'Could not create new category');\r
+                       }\r
+               }\r
+               else\r
+               {\r
+                       // force blogid (must be same as category id)\r
+                       $i_blogid = getBlogIDFromCatID($i_catid);\r
+                       $blog =& $manager->getBlog($i_blogid);\r
+               }\r
+               \r
+               if ( $i_actiontype == 'addfuture' )\r
+               {\r
+                       $posttime = mktime($i_hour, $i_minutes, 0, $i_month, $i_day, $i_year);\r
+                       \r
+                       // make sure the date is in the future, unless we allow past dates\r
+                       if ( (!$blog->allowPastPosting()) && ($posttime < $blog->getCorrectTime()) )\r
+                       {\r
+                               $posttime = $blog->getCorrectTime();\r
+                       }\r
+               }\r
+               else\r
+               {\r
+                       if ( !$i_draft )\r
+                       {\r
+                               $posttime = $blog->getCorrectTime();\r
+                       }\r
+                       else\r
+                       {\r
+                               $posttime = 0;\r
+                       }\r
+               }\r
+               \r
+               if ( $posttime > $blog->getCorrectTime() )\r
+               {\r
+                       $posted = 0;\r
+                       $blog->setFuturePost();\r
+               }\r
+               else\r
+               {\r
+                       $posted = 1;\r
+               }\r
+               \r
+               $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft, $posted);\r
+               \r
+               //Setting the itemOptions\r
+               $aOptions = requestArray('plugoption');\r
+               NucleusPlugin::apply_plugin_options($aOptions, $itemid);\r
+               $data = array(\r
+                       'context'       => 'item',\r
+                       'itemid'        => $itemid,\r
+                       'item'          => array(\r
+                               'title'         => $i_title,\r
+                               'body'          => $i_body,\r
+                               'more'          => $i_more,\r
+                               'closed'        => $i_closed,\r
+                               'catid'         => $i_catid\r
+                       )\r
+               );\r
+               \r
+               $manager->notify('PostPluginOptionsUpdate', $data);\r
+               \r
+               if ( $i_draftid > 0 )\r
+               {\r
+                       // delete permission is checked inside Item::delete()\r
+                       self::delete($i_draftid);\r
+               }\r
+               \r
+               // success\r
+               if ( $i_catid != intRequestVar('catid') )\r
+               {\r
+                       return array('status' => 'newcategory', 'itemid' => $itemid, 'catid' => $i_catid);\r
+               }\r
+               \r
+               return array('status' => 'added', 'itemid' => $itemid);\r
+       }\r
+       \r
+       /**\r
+        * Item::update()\r
+        * Updates an item\r
+        *\r
+        * @static\r
+        * @param       integer $itemid item id\r
+        * @param       integer $catid  category id\r
+        * @param       string  $title  title\r
+        * @param       string  $body   body text\r
+        * @param       string  $more   more text\r
+        * @param       boolean $closed closed or not\r
+        * @param       boolean $wasdraft       previously draft or not\r
+        * @param       boolean $publish        published or not\r
+        * @param       timestamp       $timestamp      timestamp\r
+        * @return      void\r
+        */\r
+       static public function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0)\r
+       {\r
+               global $manager;\r
+               \r
+               $itemid = (integer) $itemid;\r
+               $closed = (boolean) $closed;\r
+               \r
+               // get destination blogid\r
+               $new_blogid = getBlogIDFromCatID($catid);\r
+               $old_blogid = getBlogIDFromItemID($itemid);\r
+               \r
+               // move will be done on end of method\r
+               $moveNeeded = 0;\r
+               if ( $new_blogid != $old_blogid )\r
+               {\r
+                       $moveNeeded = 1;\r
+               }\r
+               \r
+               $blog =& $manager->getBlog($new_blogid);\r
+               \r
+               // begin if: convert line breaks to <br/>\r
+               if ( $blog->convertBreaks() )\r
+               {\r
+                       $body = addBreaks($body);\r
+                       $more = addBreaks($more);\r
+               }\r
+               \r
+               // call plugins\r
+               $data = array(\r
+                       'itemid'        => $itemid,\r
+                       'title'         => &$title,\r
+                       'body'          => &$body,\r
+                       'more'          => &$more,\r
+                       'blog'          => &$blog,\r
+                       'closed'        => &$closed,\r
+                       'catid'         => &$catid\r
+               );\r
+               $manager->notify('PreUpdateItem', $data);\r
+               \r
+               // update item itself\r
+               $query =  'UPDATE ' . sql_table('item')\r
+                               . ' SET'\r
+                               . ' ibody = ' . DB::quoteValue($body) . ','\r
+                               . ' ititle = ' . DB::quoteValue($title) . ','\r
+                               . ' imore = ' . DB::quoteValue($more) . ','\r
+                               . ' iclosed = ' . intval($closed) . ','\r
+                               . ' icat = ' . intval($catid);\r
+               \r
+               // if we received an updated timestamp that is in the past, but past posting is not allowed,\r
+               // reject that date change (timestamp = 0 will make sure the current date is kept)\r
+               if ( (!$blog->allowPastPosting()) && ($timestamp < $blog->getCorrectTime()) )\r
+               {\r
+                       $timestamp = 0;\r
+               }\r
+               \r
+               // begin if: post is in the future\r
+               if ( $timestamp > $blog->getCorrectTime(time()) )\r
+               {\r
+                       $isFuture = 1;\r
+                       $query .= ', iposted = 0';\r
+               }\r
+               else\r
+               {\r
+                       $isFuture = 0;\r
+                       $query .= ', iposted = 1';\r
+               }\r
+               \r
+               if ( $wasdraft && $publish )\r
+               {\r
+                       // set timestamp to current date only if it's not a future item\r
+                       // draft items have timestamp == 0\r
+                       // don't allow timestamps in the past (unless otherwise defined in blogsettings)\r
+                       $query .= ', idraft = 0';\r
+                       \r
+                       if ( $timestamp == 0 )\r
+                       {\r
+                               $timestamp = $blog->getCorrectTime();\r
+                       }\r
+                       \r
+                       // send new item notification\r
+                       if ( !$isFuture && $blog->getNotifyAddress() && $blog->notifyOnNewItem() )\r
+                       {\r
+                               $blog->sendNewItemNotification($itemid, $title, $body);\r
+                       }\r
+               }\r
+               \r
+               // save back to drafts\r
+               if ( !$wasdraft && !$publish )\r
+               {\r
+                       $query .= ', idraft = 1';\r
+                       // set timestamp back to zero for a draft\r
+                       $query .= ', itime = ' . DB::formatDateTime($timestamp);\r
+               }\r
+               \r
+               // update timestamp when needed\r
+               if ( $timestamp != 0 )\r
+               {\r
+                       $query .= ', itime = ' . DB::formatDateTime($timestamp);\r
+               }\r
+               \r
+               // make sure the correct item is updated\r
+               $query .= ' WHERE inumber = ' . $itemid;\r
+               \r
+               // off we go!\r
+               DB::execute($query);\r
+               \r
+               $manager->notify('PostUpdateItem', array('itemid' => $itemid));\r
+               \r
+               // when needed, move item and comments to new blog\r
+               if ( $moveNeeded )\r
+               {\r
+                       self::move($itemid, $catid);\r
+               }\r
+               \r
+               //update the itemOptions\r
+               $aOptions = requestArray('plugoption');\r
+               NucleusPlugin::apply_plugin_options($aOptions);\r
+               $data = array(\r
+                       'context'       => 'item',\r
+                       'itemid'        => $itemid,\r
+                       'item'          => array(\r
+                               'title'         => $title,\r
+                               'body'          => $body,\r
+                               'more'          => $more,\r
+                               'closed'        => $closed,\r
+                               'catid'         => $catid\r
+                       )\r
+               );\r
+               $manager->notify('PostPluginOptionsUpdate', $data);\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Item::move()\r
+        * Move an item to another blog (no checks)\r
+        *\r
+        * @static\r
+        * @param       integer $itemid\r
+        * @param       integer $new_catid\r
+        * @return      void\r
+        */\r
+       static public function move($itemid, $new_catid)\r
+       {\r
+               global $manager;\r
+               \r
+               $itemid = (integer) $itemid;\r
+               $new_catid = (integer) $new_catid;\r
+               $new_blogid = getBlogIDFromCatID($new_catid);\r
+               \r
+               $data = array(\r
+                       'itemid'                => $itemid,\r
+                       'destblogid'    => $new_blogid,\r
+                       'destcatid'             => $new_catid\r
+               );\r
+               $manager->notify('PreMoveItem', $data);\r
+               \r
+               // update item table\r
+               $query = "UPDATE %s SET iblog=%d, icat=%d WHERE inumber=%d";\r
+               $query = sprintf($query, sql_table('item'), $new_blogid, $new_catid, $itemid);\r
+               DB::execute($query);\r
+               \r
+               // update comments\r
+               $query = "UPDATE %s SET cblog=%d WHERE citem=%d";\r
+               $query = sprintf($query, sql_table('comment'), $new_blogid, $itemid);\r
+               DB::execute($query);\r
+               \r
+               $data = array(\r
+                       'itemid'                => $itemid,\r
+                       'destblogid'    => $new_blogid,\r
+                       'destcatid'             => $new_catid\r
+               );\r
+               $manager->notify('PostMoveItem', $data);\r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * Item::delete()\r
+        * Deletes an item\r
+        * \r
+        * @param       integer $itemid\r
+        * @return      void\r
+        */\r
+       static public function delete($itemid)\r
+       {\r
+               global $manager, $member;\r
+               \r
+               $itemid = (integer) $itemid;\r
+               \r
+               // check permission\r
+               if ( !$member->canAlterItem($itemid) )\r
+               {\r
+                       return 1;\r
+               }\r
+               \r
+               $manager->notify('PreDeleteItem', array('itemid' => $itemid));\r
+               \r
+               // delete item\r
+               $query = "DELETE FROM %s WHERE inumber=%d;";\r
+               $query = sprintf($query, sql_table('item'), $itemid);\r
+               DB::execute($query);\r
+               \r
+               // delete the comments associated with the item\r
+               $query = "DELETE FROM %s WHERE citem=%d;";\r
+               $query = sprintf($query, sql_table('comment'), $itemid);\r
+               DB::execute($query);\r
+               \r
+               // delete all associated plugin options\r
+               NucleusPlugin::delete_option_values('item', $itemid);\r
+               \r
+               $manager->notify('PostDeleteItem', array('itemid' => $itemid));\r
+               \r
+               return 0;\r
+       }\r
+       \r
+       /**\r
+        * Item::exists()\r
+        * Returns true if there is an item with the given ID\r
+        *\r
+        * @static\r
+        * @param       integer $itemid\r
+        * @param       boolean $future\r
+        * @param       boolean $draft\r
+        * @return      boolean exists or not\r
+        */\r
+       static public function exists($itemid, $future, $draft)\r
+       {\r
+               global $manager;\r
+               \r
+               $itemid = (integer) $itemid;\r
+               \r
+               $query = 'SELECT * FROM %s WHERE inumber=%d';\r
+               $query = sprintf($query, sql_table('item'), $itemid);\r
+               \r
+               if ( !$future )\r
+               {\r
+                       $blogid = getBlogIDFromItemID($itemid);\r
+                       if ( !$blogid )\r
+                       {\r
+                               return 0;\r
+                       }\r
+                       $blog =& $manager->getBlog($blogid);\r
+                       $query .= ' AND itime<=' . DB::formatDateTime($blog->getCorrectTime());\r
+               }\r
+               \r
+               if ( !$draft )\r
+               {\r
+                       $query .= ' AND idraft=0';\r
+               }\r
+               \r
+               $result = DB::getResult($query);\r
+               return ( $result->rowCount() != 0 );\r
+       }\r
+       \r
+       /**\r
+        * Item::createDraftFromRequest()\r
+        * Tries to create an draft from the data\r
+        *  in the current request (comes from bookmarklet or admin area)\r
+        *   Used by xmlHTTPRequest AutoDraft\r
+        *\r
+        * Returns an array with status info:\r
+        * status = 'added', 'error', 'newcategory'\r
+        *\r
+        * @static\r
+        * @param       void\r
+        * @return      array   (status = added/error/newcategory, message)\r
+        */\r
+       static public function createDraftFromRequest()\r
+       {\r
+               global $member, $manager;\r
+               \r
+               /*\r
+                * TODO: these values from user agent should be validated but not implemented yet\r
+                */\r
+               $i_author       = $member->getID();\r
+               $i_body         = postVar('body');\r
+               $i_title        = postVar('title');\r
+               $i_more         = postVar('more');\r
+               $i_closed       = intPostVar('closed');\r
+               $i_catid        = postVar('catid');\r
+               $i_draft        = 1;\r
+               $type           = postVar('type');\r
+               $i_draftid      = intPostVar('draftid');\r
+               \r
+               if ( $type == 'edit' )\r
+               {\r
+                       $itemid = intPostVar('itemid');\r
+                       $i_blogid = getBlogIDFromItemID($itemid);\r
+               }\r
+               else\r
+               {\r
+                       $i_blogid = intPostVar('blogid');\r
+               }\r
+               \r
+               if ( !$member->canAddItem($i_catid) )\r
+               {\r
+                       return array('status' => 'error', 'message' => _ERROR_DISALLOWED);\r
+               }\r
+               \r
+               if ( !trim($i_body) )\r
+               {\r
+                       return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);\r
+               }\r
+               \r
+               // create new category if needed\r
+               if ( i18n::strpos($i_catid,'newcat') === 0 )\r
+               {\r
+                       // Set in default category\r
+                       $blog =& $manager->getBlog($i_blogid);\r
+                       $i_catid = $blog->getDefaultCategory();\r
+               }\r
+               else\r
+               {\r
+                       // force blogid (must be same as category id)\r
+                       $i_blogid = getBlogIDFromCatID($i_catid);\r
+                       $blog =& $manager->getBlog($i_blogid);\r
+               }\r
+               \r
+               $posttime = 0;\r
+               \r
+               if ( $i_draftid > 0 )\r
+               {\r
+                       self::update($i_draftid, $i_catid, $i_title, $i_body, $i_more, $i_closed, 1, 0, 0);\r
+                       $itemid = $i_draftid;\r
+               }\r
+               else\r
+               {\r
+                       $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft);\r
+               }\r
+               \r
+               return array('status' => 'added', 'draftid' => $itemid);\r
+       }\r
+}\r
+=======
 <?php
 
 /*
 /**
  * @license http://nucleuscms.org/license.txt GNU General Public License
  * @copyright Copyright (C) 2002-2009 The Nucleus Group
- * @version $Id: ITEM.php 1668 2012-02-19 14:36:44Z sakamocchi $
+ * @version $Id: ITEM.php 1877 2012-06-17 07:40:11Z sakamocchi $
  */
 
 /**
  * A class representing an item
  *
  */
-class ITEM
+class Item
 {
        /**
-        * ITEM::$actiontypes
+        * Item::$actiontypes
         * actiontype list for handling items
         * 
         * @static
         */
-       static private $actiontypes
-               = array('addnow', 'adddraft', 'addfuture', 'edit', 'changedate', 'backtodrafts', 'delete');
+       static private $actiontypes = array(
+               'addnow', 'adddraft', 'addfuture', 'edit',
+               'changedate', 'backtodrafts', 'delete'
+       );
        
        /**
-        * ITEM::__construct()
+        * Item::$itemid
+        * item id
+        * @deprecated
+        */
+       public $itemid;
+       
+       /**
+        * Item::__construct()
         * Creates a new ITEM object
         * 
         * @deprecated
-        * @param void
+        * @param integer       $item_id        id for item
         * @return void
         */
-       public function __construct()
+       public function __construct($item_id)
        {
+               $this->itemid = $item_id;
                return;
        }
        
        /**
-        * ITEM::getitem()
+        * Item::getitem()
         * Returns one item with the specific itemid
-        *
+        * 
+        * @static
         * @param int $item_id
         * @param bool $allow_draft
         * @param bool $allow_future
         * @return mixed
-        * 
         */
        static public function getitem($item_id, $allow_draft, $allow_future)
        {
                global $manager;
                
-               $item_id = intval($item_id);
+               $item_id = (integer) $item_id;
                
                $query = 'SELECT ' .
                        'i.idraft AS draft, ' .
@@ -89,26 +702,24 @@ class ITEM
                if ( !$allow_future )
                {
                        $blog =& $manager->getBlog(getBlogIDFromItemID($item_id));
-                       $query .= "AND `i`.`itime` <= '" . i18n::formatted_datetime('mysql', $blog->getCorrectTime()) ."'";
+                       $query .= 'AND i.itime <= ' . DB::formatDateTime($blog->getCorrectTime());
                }
                
                $query .= ' LIMIT 1';
-               $result = sql_query($query);
+               $result = DB::getResult($query);
                
-               if ( sql_num_rows($result) == 1 )
-               {
-                       $aItemInfo = sql_fetch_assoc($result);
-                       $aItemInfo['timestamp'] = strtotime($aItemInfo['itime']);
-                       return $aItemInfo;
-               }
-               else
+               if ( $result->rowCount() != 1 )
                {
                        return 0;
                }
+               
+               $aItemInfo = $result->fetch(PDO::FETCH_ASSOC);
+               $aItemInfo['timestamp'] = strtotime($aItemInfo['itime']);
+               return $aItemInfo;
        }
        
        /**
-        * ITEM::createFromRequest()
+        * Item::createFromRequest()
         * Tries to create an item from the data in the current request (comes from
         * bookmarklet or admin area
         *
@@ -156,7 +767,7 @@ class ITEM
                }
                
                // create new category if needed
-               if ( i18n::strpos($i_catid, 'newcat') )
+               if ( i18n::strpos($i_catid, 'newcat') === 0 )
                {
                        // get blogid
                        list($i_blogid) = sscanf($i_catid, "newcat-%d");
@@ -190,8 +801,14 @@ class ITEM
                }
                else
                {
-                       // time with offset, or 0 for drafts
-                       $posttime = $i_draft ? 0 : $blog->getCorrectTime();
+                       if ( !$i_draft )
+                       {
+                               $posttime = $blog->getCorrectTime();
+                       }
+                       else
+                       {
+                               $posttime = 0;
+                       }
                }
                
                if ( $posttime > $blog->getCorrectTime() )
@@ -209,22 +826,23 @@ class ITEM
                //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
-                               )
+               $data = array(
+                       'context'       => 'item',
+                       'itemid'        => $itemid,
+                       'item'          => array(
+                               'title'         => $i_title,
+                               'body'          => $i_body,
+                               'more'          => $i_more,
+                               'closed'        => $i_closed,
+                               'catid'         => $i_catid
                        )
                );
                
+               $manager->notify('PostPluginOptionsUpdate', $data);
+               
                if ( $i_draftid > 0 )
                {
-                       // delete permission is checked inside ITEM::delete()
+                       // delete permission is checked inside Item::delete()
                        self::delete($i_draftid);
                }
                
@@ -233,14 +851,12 @@ class ITEM
                {
                        return array('status' => 'newcategory', 'itemid' => $itemid, 'catid' => $i_catid);
                }
-               else
-               {
-                       return array('status' => 'added', 'itemid' => $itemid);
-               }
+               
+               return array('status' => 'added', 'itemid' => $itemid);
        }
        
        /**
-        * ITEM::update()
+        * Item::update()
         * Updates an item
         *
         * @static
@@ -254,7 +870,6 @@ class ITEM
         * @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)
        {
@@ -284,7 +899,7 @@ class ITEM
                }
                
                // call plugins
-               $manager->notify('PreUpdateItem', array(
+               $data = array(
                        'itemid'        => $itemid,
                        'title'         => &$title,
                        'body'          => &$body,
@@ -292,19 +907,20 @@ class ITEM
                        'blog'          => &$blog,
                        'closed'        => &$closed,
                        'catid'         => &$catid
-                       )
                );
+               $manager->notify('PreUpdateItem', $data);
                
                // 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);
+                               . ' ibody = ' . DB::quoteValue($body) . ','
+                               . ' ititle = ' . DB::quoteValue($title) . ','
+                               . ' imore = ' . DB::quoteValue($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 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;
@@ -346,22 +962,23 @@ class ITEM
                {
                        $query .= ', idraft = 1';
                        // set timestamp back to zero for a draft
-                       $query .= ", itime = '" . i18n::formatted_datetime('mysql', $timestamp) ."'";
+                       $query .= ', itime = ' . DB::formatDateTime($timestamp);
                }
                
                // update timestamp when needed
                if ( $timestamp != 0 )
                {
-                       $query .= ", itime = '" . i18n::formatted_datetime('mysql', $timestamp) ."'";
+                       $query .= ', itime = ' . DB::formatDateTime($timestamp);
                }
                
                // make sure the correct item is updated
                $query .= ' WHERE inumber = ' . $itemid;
                
                // off we go!
-               sql_query($query);
-               
-               $manager->notify('PostUpdateItem', array('itemid' => $itemid));
+               DB::execute($query);
+
+               $data = array('itemid' => $itemid);
+               $manager->notify('PostUpdateItem', $data);
                
                // when needed, move item and comments to new blog
                if ( $moveNeeded )
@@ -372,23 +989,23 @@ class ITEM
                //update the itemOptions
                $aOptions = requestArray('plugoption');
                NucleusPlugin::apply_plugin_options($aOptions);
-               $manager->notify('PostPluginOptionsUpdate', array(
-                       'context' => 'item',
-                       'itemid' => $itemid,
-                       'item' => array(
-                               'title' => $title,
-                               'body' => $body,
-                               'more' => $more,
-                               'closed' => $closed,
-                               'catid' => $catid
-                               )
+               $data = array(
+                       'context'       => 'item',
+                       'itemid'        => $itemid,
+                       'item'          => array(
+                               'title'         => $title,
+                               'body'          => $body,
+                               'more'          => $more,
+                               'closed'        => $closed,
+                               'catid'         => $catid
                        )
                );
+               $manager->notify('PostPluginOptionsUpdate', $data);
                return;
        }
        
        /**
-        * ITEM::move()
+        * Item::move()
         * Move an item to another blog (no checks)
         *
         * @static
@@ -404,38 +1021,34 @@ class ITEM
                $new_catid      = (integer) $new_catid;
                $new_blogid     = getBlogIDFromCatID($new_catid);
                
-               $manager->notify(
-                       'PreMoveItem',
-                       array(
-                               'itemid' => $itemid,
-                               'destblogid' => $new_blogid,
-                               'destcatid' => $new_catid
-                       )
+               $data = array(
+                       'itemid'                => $itemid,
+                       'destblogid'    => $new_blogid,
+                       'destcatid'             => $new_catid
                );
+               $manager->notify('PreMoveItem', $data);
                
                // update item table
                $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);
+               DB::execute($query);
                
                // update comments
                $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(
-                               'itemid' => $itemid,
-                               'destblogid' => $new_blogid,
-                               'destcatid' => $new_catid
-                       )
+               DB::execute($query);
+               
+               $data = array(
+                       'itemid'                => $itemid,
+                       'destblogid'    => $new_blogid,
+                       'destcatid'             => $new_catid
                );
+               $manager->notify('PostMoveItem', $data);
                return;
        }
        
        /**
-        * ITEM::delete()
+        * Item::delete()
         * Deletes an item
         * 
         * @param       integer $itemid
@@ -452,29 +1065,30 @@ class ITEM
                {
                        return 1;
                }
-               
-               $manager->notify('PreDeleteItem', array('itemid' => $itemid));
+
+               $data = array('itemid' => $itemid);
+               $manager->notify('PreDeleteItem', $data);
                
                // delete item
-               $query = "DELETE FROM %s WHERE inumber=%d";
+               $query = "DELETE FROM %s WHERE inumber=%d;";
                $query = sprintf($query, sql_table('item'), $itemid);
-               sql_query($query);
+               DB::execute($query);
                
                // delete the comments associated with the item
-               $query = "DELETE FROM %s WHERE citem=%d";
+               $query = "DELETE FROM %s WHERE citem=%d;";
                $query = sprintf($query, sql_table('comment'), $itemid);
-               sql_query($query);
+               DB::execute($query);
                
                // delete all associated plugin options
                NucleusPlugin::delete_option_values('item', $itemid);
                
-               $manager->notify('PostDeleteItem', array('itemid' => $itemid));
+               $manager->notify('PostDeleteItem', $data);
                
                return 0;
        }
        
        /**
-        * ITEM::exists()
+        * Item::exists()
         * Returns true if there is an item with the given ID
         *
         * @static
@@ -482,14 +1096,15 @@ class ITEM
         * @param       boolean $future
         * @param       boolean $draft
         * @return      boolean exists or not
-        * 
         */
        static public function exists($itemid, $future, $draft)
        {
                global $manager;
                
                $itemid = (integer) $itemid;
-               $query = 'select * FROM '.sql_table('item').' WHERE inumber='.$itemid;
+               
+               $query = 'SELECT * FROM %s WHERE inumber=%d';
+               $query = sprintf($query, sql_table('item'), $itemid);
                
                if ( !$future )
                {
@@ -499,18 +1114,20 @@ class ITEM
                                return 0;
                        }
                        $blog =& $manager->getBlog($blogid);
-                       $query .= " and itime<='" . i18n::formatted_datetime('mysql', $blog->getCorrectTime()) ."'";
+                       $query .= ' AND itime<=' . DB::formatDateTime($blog->getCorrectTime());
                }
+               
                if ( !$draft )
                {
-                       $query .= ' and idraft=0';
+                       $query .= ' AND idraft=0';
                }
-               $result = sql_query($query);
-               return ( sql_num_rows($result) != 0 );
+               
+               $result = DB::getResult($query);
+               return ( $result->rowCount() != 0 );
        }
        
        /**
-        * ITEM::createDraftFromRequest()
+        * Item::createDraftFromRequest()
         * Tries to create an draft from the data
         *  in the current request (comes from bookmarklet or admin area)
         *   Used by xmlHTTPRequest AutoDraft
@@ -521,7 +1138,6 @@ class ITEM
         * @static
         * @param       void
         * @return      array   (status = added/error/newcategory, message)
-        *
         */
        static public function createDraftFromRequest()
        {
@@ -538,7 +1154,7 @@ class ITEM
                $i_catid        = postVar('catid');
                $i_draft        = 1;
                $type           = postVar('type');
-               $i_draftid = intPostVar('draftid');
+               $i_draftid      = intPostVar('draftid');
                
                if ( $type == 'edit' )
                {
@@ -561,7 +1177,7 @@ class ITEM
                }
                
                // create new category if needed
-               if ( strstr($i_catid, 'newcat') )
+               if ( i18n::strpos($i_catid,'newcat') === 0 )
                {
                        // Set in default category
                        $blog =& $manager->getBlog($i_blogid);
@@ -589,3 +1205,4 @@ class ITEM
                return array('status' => 'added', 'draftid' => $itemid);
        }
 }
+>>>>>>> skinnable-master