/** * 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. * * @param \Swiftriver\Core\ObjectModel\Content[] $content */ public static function SaveContent($content) { //initiate the redbean dal contoller $rb = RedBeanController::RedBean(); //loop throught each oitem of content foreach ($content as $item) { $potentials = RedBeanController::Finder()->where("contentitems", "textid = :id limit 1", array(":id" => $item->GetId())); if (isset($potentials) && is_array($potentials)) { //set the content to be the one from the DB $i = $potentials[1]; //1 base - how silly ?!?!?! } else { //tell the dal what table we are dealing with $i = $rb->dispense("contentitems"); //copy over the properties $i->textId = $item->GetId(); } $i->state = $item->GetState(); $i->title = $item->GetTitle(); $i->link = $item->GetLink(); //comit the content to the DB $rb->store($i); //First remove all existing text $textToRemove = RedBeanController::GetRelatedBeans($i, "content_text"); if (isset($textToRemove) && is_array($textToRemove)) { foreach ($textToRemove as $ttr) { $rb->trash($ttr); } } //Then add the nest text foreach ($item->GetText() as $text) { //initiare the db table $t = $rb->dispense("content_text"); //extratc the text $t->text = $text; //store the text $rb->store($t); //Assocate the text with the content RedBeanController::Associate($t, $i); } //first remove the existing tags $tagsToRemove = RedBeanController::GetRelatedBeans($i, "content_tags"); if (isset($tagsToRemove) && is_array($tagsToRemove)) { foreach ($tagsToRemove as $ttr) { $rb->trash($ttr); } } //then add all new tags foreach ($item->GetTags() as $tag) { //initiate the tags db table $t = $rb->dispense("content_tags"); //get the tag properties $t->type = $tag->GetType(); $t->text = $tag->GetText(); //store the tag $rb->store($t); //Associate the tag with the content RedBeanController::Associate($t, $i); } //first remove all existing difcollection and their difs $difCollectionsToRemove = RedBeanController::GetRelatedBeans($i, "dif_collections"); if (isset($difCollectionsToRemove) && is_array($difCollectionsToRemove)) { foreach ($difCollectionsToRemove as $dctr) { $rb->trash($dctr); } } //loop through the DFICollections foreach ($item->GetDifs() as $collection) { //initiate the dif collection db table $c = $rb->dispense("dif_collections"); //Get the properties $c->name = $collection->GetName(); //store the collection $rb->store($c); //Associate the collection with the contet RedBeanController::Associate($c, $i); //Loop through the difs foreach ($collection->GetDifs() as $dif) { //initiate the dif db table $d = $rb->dispense("difs"); //Get the properties $d->type = $dif->GetType(); $d->value = $dif->GetValue(); //store the dif $rb->store($d); //associate the dif with the collection RedBeanController::Associate($d, $c); } } } }
/** * Given an array of content is's, this function will * fetch the content objects from the data store. * * @param string[] $ids * @return \Swiftriver\Core\ObjectModel\Content[] */ public static function GetContent($ids, $orderby = null) { $logger = \Swiftriver\Core\Setup::GetLogger(); $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetContent [Method invoked]", \PEAR_LOG_DEBUG); //if no $orderby is sent if (!$orderby || $orderby == null) { $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetContent [No Order By clause set, setting to 'date desc']", \PEAR_LOG_DEBUG); //Set it to the default - date DESC $orderby = "date desc"; } //set up the return array $content = array(); //If the $ids array is blank or empty, return the empty array if (!isset($ids) || !is_array($ids) || count($ids) < 1) { $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetContent [No IDs sent to method]", \PEAR_LOG_DEBUG); $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetContent [Method finsiehd]", \PEAR_LOG_DEBUG); return $content; } $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetContent [START: Building the RedBean Query]", \PEAR_LOG_DEBUG); //set up the array to hold the ids $queryIds = array(); //start to build the sql $query = "textId in ("; /*//for each content item, add to the query and the ids array for($i=0; $i<count($ids); $i++) { $query .= ":id$i,"; $queryIds[":id$i"] = $ids[$i]; }*/ $counter = 0; foreach ($ids as $id) { $query .= ":id{$counter},"; $queryIds[":id{$counter}"] = $id; $counter++; } //tidy up the query $query = rtrim($query, ",") . ") order by " . $orderby; $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetContent [END: Building the RedBean Query]", \PEAR_LOG_DEBUG); $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetContent [START: Running RedBean Query]", \PEAR_LOG_DEBUG); //Get the finder $finder = RedBeanController::Finder(); //Find the content $dbContent = $finder->where("content", $query, $queryIds); $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetContent [FINISHED: Running RedBean Query]", \PEAR_LOG_DEBUG); //set up the return array $content = array(); //set up the red bean $rb = RedBeanController::RedBean(); $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetContent [START: Constructing Content and Source items]", \PEAR_LOG_DEBUG); //loop through the db content foreach ($dbContent as $key => $dbItem) { //get the associated source $s = reset($rb->batch("source", RedBeanController::GetRelatedBeans($dbItem, "source"))); //Create the source from the db json $source = \Swiftriver\Core\ObjectModel\ObjectFactories\SourceFactory::CreateSourceFromJSON($s->json); //get the json for the content $json = $dbItem->json; //create the content $item = \Swiftriver\Core\ObjectModel\ObjectFactories\ContentFactory::CreateContent($source, $json); //add it to the array $content[] = $item; } $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetContent [END: Constructing Content and Source items]", \PEAR_LOG_DEBUG); $logger->log("Core::Modules::DataContext::MySQL_V1::DataContext::GetContent [Method finished]", \PEAR_LOG_DEBUG); //return the content return $content; }