示例#1
0
 /**
  * Replace media in all layouts.
  * @param <type> $oldMediaId
  * @param <type> $newMediaId
  */
 private function ReplaceMediaInAllLayouts($replaceInLayouts, $replaceBackgroundImages, $oldMediaId, $newMediaId)
 {
     $count = 0;
     Debug::LogEntry('audit', sprintf('Replacing mediaid %s with mediaid %s in all layouts', $oldMediaId, $newMediaId), 'module', 'ReplaceMediaInAllLayouts');
     try {
         $dbh = PDOConnect::init();
         // Some update statements to use
         $sth = $dbh->prepare('SELECT lklayoutmediaid, regionid FROM lklayoutmedia WHERE mediaid = :media_id AND layoutid = :layout_id');
         $sth_update = $dbh->prepare('UPDATE lklayoutmedia SET mediaid = :media_id WHERE lklayoutmediaid = :lklayoutmediaid');
         // Loop through a list of layouts this user has access to
         foreach ($this->user->LayoutList() as $layout) {
             $layoutId = $layout['layoutid'];
             // Does this layout use the old media id?
             $sth->execute(array('media_id' => $oldMediaId, 'layout_id' => $layoutId));
             $results = $sth->fetchAll();
             if (count($results) <= 0) {
                 continue;
             }
             Debug::LogEntry('audit', sprintf('%d linked media items for layoutid %d', count($results), $layoutId), 'module', 'ReplaceMediaInAllLayouts');
             // Create a region object for later use (new one each time)
             $layout = new Layout();
             $region = new region($this->db);
             // Loop through each media link for this layout
             foreach ($results as $row) {
                 // Get the LKID of the link between this layout and this media.. could be more than one?
                 $lkId = $row['lklayoutmediaid'];
                 $regionId = $row['regionid'];
                 if ($regionId == 'background') {
                     Debug::Audit('Replacing background image');
                     if (!$replaceBackgroundImages) {
                         continue;
                     }
                     // Straight swap this background image node.
                     if (!$layout->EditBackgroundImage($layoutId, $newMediaId)) {
                         return false;
                     }
                 } else {
                     if (!$replaceInLayouts) {
                         continue;
                     }
                     // Get the Type of this media
                     if (!($type = $region->GetMediaNodeType($layoutId, '', '', $lkId))) {
                         continue;
                     }
                     // Create a new media node use it to swap the nodes over
                     Debug::LogEntry('audit', 'Creating new module with MediaID: ' . $newMediaId . ' LayoutID: ' . $layoutId . ' and RegionID: ' . $regionId, 'region', 'ReplaceMediaInAllLayouts');
                     try {
                         $module = ModuleFactory::createForMedia($type, $newMediaId, $this->db, $this->user);
                     } catch (Exception $e) {
                         Debug::Error($e->getMessage());
                         return false;
                     }
                     // Sets the URI field
                     if (!$module->SetRegionInformation($layoutId, $regionId)) {
                         return false;
                     }
                     // Get the media xml string to use in the swap.
                     $mediaXmlString = $module->AsXml();
                     // Swap the nodes
                     if (!$region->SwapMedia($layoutId, $regionId, $lkId, $oldMediaId, $newMediaId, $mediaXmlString)) {
                         return false;
                     }
                 }
                 // Update the LKID with the new media id
                 $sth_update->execute(array('media_id' => $newMediaId, 'lklayoutmediaid' => $row['lklayoutmediaid']));
                 $count++;
             }
         }
     } catch (Exception $e) {
         Debug::LogEntry('error', $e->getMessage());
         if (!$this->IsError()) {
             $this->SetError(1, __('Unknown Error'));
         }
         return false;
     }
     Debug::LogEntry('audit', sprintf('Replaced media in %d layouts', $count), 'module', 'ReplaceMediaInAllLayouts');
 }