Swiftriver\Core\Modules\DataContext\Mongo_V1\DataContext::SaveContent PHP Method

SaveContent() public static method

Given a set of content items, this method will persist them to the data store, if they already exists then this method should update the values in the data store.
public static SaveContent ( Content[] $content )
$content Swiftriver\Core\ObjectModel\Content[]
    public static function SaveContent($content)
    {
        $logger = \Swiftriver\Core\Setup::GetLogger();
        $db = self::MongoDatabase();
        $logger->log("Core::Modules::DataContext::Mongo_V1::DataContext::SaveContent [Method Invoked]", \PEAR_LOG_DEBUG);
        if (!\is_array($content) || \count($content) < 1) {
            $logger->log("Core::Modules::DataContext::Mongo_V1::DataContext::SaveContent [No Content Supplied]", \PEAR_LOG_DEBUG);
            $logger->log("Core::Modules::DataContext::Mongo_V1::DataContext::SaveContent [Mrethod Finished]", \PEAR_LOG_DEBUG);
            return;
        }
        try {
            $logger->log("Core::Modules::DataContext::Mongo_V1::DataContext::SaveContent [START: Looping through content]", \PEAR_LOG_DEBUG);
            foreach ($content as $item) {
                $source = $item->source;
                $sourceParams = (array) $source;
                $sourceParams["date_utc"] = \date("Y-m-d h:i:s", $sourceParams["date"]);
                $sourceParams["date_day"] = \date("d", $sourceParams["date"]);
                $sourceParams["date_month"] = \date("m", $sourceParams["date"]);
                $sourceParams["date_year"] = \date("Y", $sourceParams["date"]);
                $sourceParams["date_day_of_year"] = \date("z", $sourceParams["date"]);
                $sourceParams["channelId"] = $source->parent;
                $logger->log("Core::Modules::DataContext::Mongo_V1::DataContext::SaveContent [START: Saving content source]", \PEAR_LOG_DEBUG);
                // Tally to see if the source id already exists
                $source_result = $db->get_where("sources", array("id" => $source->id));
                $result = null;
                if (count($source_result) > 0) {
                    $db->where("id", $source->id);
                    $result = $db->update("sources", $sourceParams);
                } else {
                    $result = $db->insert("sources", $sourceParams);
                }
                if ($result != TRUE) {
                    $logger->log("Core::Modules::DataContext::Mongo_V1::DataContext::SaveContent [An Exception was thrown by the MongoDB framwork]", \PEAR_LOG_ERR);
                    $logger->log("Core::Modules::DataContext::Mongo_V1::DataContext::SaveContent [Could not save the source information]", \PEAR_LOG_ERR);
                }
                $db->where(array());
                $logger->log("Core::Modules::DataContext::Mongo_V1::DataContext::SaveContent [END: Saving content source]", \PEAR_LOG_DEBUG);
                $contentParams = (array) $item;
                $contentParams["date_utc"] = \date("Y-m-d h:i:s", $contentParams["date"]);
                $contentParams["date_day"] = \date("d", $contentParams["date"]);
                $contentParams["date_month"] = \date("m", $contentParams["date"]);
                $contentParams["date_year"] = \date("Y", $contentParams["date"]);
                $contentParams["date_day_of_year"] = \date("z", $contentParams["date"]);
                $contentParams["sourceId"] = $source->id;
                // Tally to see if content item already exists
                $content_result = $db->get_where("content", array("id" => $contentParams->id));
                if (count($content_result) > 0) {
                    $db->where("id", $contentParams->id);
                    $result = $db->update("content", $contentParams);
                } else {
                    $result = $db->insert("content", $contentParams);
                }
                $db->where(array());
                $logger->log("Core::Modules::DataContext::Mongo_V1::DataContext::SaveContent [START: Saving content]", \PEAR_LOG_DEBUG);
                if ($result != TRUE) {
                    $logger->log("Core::Modules::DataContext::Mongo_V1::DataContext::SaveContent [An Exception was thrown by the MongoDB framwork]", \PEAR_LOG_ERR);
                    $logger->log("Core::Modules::DataContext::Mongo_V1::DataContext::SaveContent [Could not save content item]", \PEAR_LOG_ERR);
                }
                $logger->log("Core::Modules::DataContext::Mongo_V1::DataContext::SaveContent [END: Saving content]", \PEAR_LOG_DEBUG);
                $logger->log("Core::Modules::DataContext::Mongo_V1::DataContext::SaveContent [START: Looping through content tags]", \PEAR_LOG_DEBUG);
                if (is_array($item->tags) && count($item->tags) > 0) {
                    $db->delete("content_tags", array("id" => $item->id));
                    foreach ($item->tags as $tag) {
                        $tagParams = array("contentId" => $item->id, "tagId" => \md5(\strtolower($tag->text)), "tagType" => $tag->type, "tagText" => \strtolower($tag->text));
                        $logger->log("Core::Modules::DataContext::Mongo_V1::DataContext::SaveContent [START: Saving Tag]", \PEAR_LOG_DEBUG);
                        $tag_result = $db->get_where("tags", array("id" => $tagParams["tagId"]));
                        if (count($tag_result) < 1) {
                            $result = $db->insert("tags", array("id" => $tagParams["tagId"], "type" => $tag->type, "text" => $tagParams["tagText"]));
                        } else {
                            $result = TRUE;
                        }
                        if ($result === FALSE) {
                            $logger->log("Core::Modules::DataContext::Mongo_V1::DataContext::SaveContent [An Exception was thrown by the MongoDB framwork]", \PEAR_LOG_ERR);
                            $logger->log("Core::Modules::DataContext::Mongo_V1::DataContext::SaveContent [Unable to save the tag]", \PEAR_LOG_ERR);
                        }
                        $content_tags = $db->get_where("content_tags", array("contentId" => $item->id, "tagId" => \md5(\strtolower($tag->text))));
                        if (\count($content_tags) < 1) {
                            $result = $db->insert("content_tags", array("contentId" => $item->id, "tagId" => \md5(\strtolower($tag->text))));
                            if ($result === FALSE) {
                                $logger->log("Core::Modules::DataContext::Mongo_V1::DataContext::SaveContent [An Exception was thrown by the MongoDB framwork]", \PEAR_LOG_ERR);
                                $logger->log("Core::Modules::DataContext::Mongo_V1::DataContext::SaveContent [Unable to save the content tag]", \PEAR_LOG_ERR);
                            }
                        }
                        $logger->log("Core::Modules::DataContext::Mongo_V1::DataContext::SaveContent [END: Saving Tag]", \PEAR_LOG_DEBUG);
                    }
                }
                $logger->log("Core::Modules::DataContext::Mongo_V1::DataContext::SaveContent [END: Looping through content tags]", \PEAR_LOG_DEBUG);
            }
            $logger->log("Core::Modules::DataContext::Mongo_V1::DataContext::SaveContent [END: Looping through content]", \PEAR_LOG_DEBUG);
            $db = null;
        } catch (\MongoException $e) {
            $logger->log("Core::Modules::DataContext::Mongo_V1::DataContext::ListAllChannels [An Exception was thrown:]", \PEAR_LOG_ERR);
            $logger->log("Core::Modules::DataContext::Mongo_V1::DataContext::ListAllChannels [{$e}]", \PEAR_LOG_ERR);
        }
        $logger->log("Core::Modules::DataContext::Mongo_V1::DataContext::SaveContent [Method Finished]", \PEAR_LOG_DEBUG);
    }