示例#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');
 }
示例#2
0
 /**
  * Add Existing Media from the Library
  * @param [int] $user [A user object for the currently logged in user]
  * @param [int] $layoutId  [The LayoutID to Add on]
  * @param [int] $regionId  [The RegionID to Add on]
  * @param [array] $mediaList [A list of media ids from the library that should be added to to supplied layout/region]
  */
 public function AddFromLibrary($user, $layoutId, $regionId, $mediaList)
 {
     Debug::LogEntry('audit', 'IN', 'Region', 'AddFromLibrary');
     try {
         $dbh = PDOConnect::init();
         // Check that some media assignments have been made
         if (count($mediaList) == 0) {
             return $this->SetError(25006, __('No media to assign'));
         }
         // Loop through all the media
         foreach ($mediaList as $mediaId) {
             Debug::LogEntry('audit', 'Assigning MediaID: ' . $mediaId);
             $mediaId = Kit::ValidateParam($mediaId, _INT);
             // Get the type from this media
             $sth = $dbh->prepare('SELECT type FROM media WHERE mediaID = :mediaid');
             $sth->execute(array('mediaid' => $mediaId));
             if (!($row = $sth->fetch())) {
                 $this->ThrowError(__('Error getting type from a media item.'));
             }
             $mod = Kit::ValidateParam($row['type'], _WORD);
             try {
                 // Create the media object without any region and layout information
                 $module = ModuleFactory::createForMedia($mod, $mediaId, null, $user);
             } catch (Exception $e) {
                 return $this->SetError($e->getMessage());
             }
             // Check we have permissions to use this media (we will use this to copy the media later)
             if (!$module->auth->view) {
                 return $this->SetError(__('You have selected media that you no longer have permission to use. Please reload Library form.'));
             }
             if (!$module->SetRegionInformation($layoutId, $regionId)) {
                 return $this->SetError($module->GetErrorMessage());
             }
             if (!$module->UpdateRegion()) {
                 return $this->SetError($module->GetErrorMessage());
             }
             // Need to copy over the permissions from this media item & also the delete permission
             $security = new LayoutMediaGroupSecurity($this->db);
             $security->Link($layoutId, $regionId, $mediaId, $user->getGroupFromID($user->userid, true), $module->auth->view, $module->auth->edit, 1);
         }
         // Update layout status
         $layout = new Layout($this->db);
         $layout->SetValid($layoutId, true);
         return true;
     } catch (Exception $e) {
         Debug::LogEntry('error', $e->getMessage());
         if (!$this->IsError()) {
             $this->SetError(1, __('Unknown Error'));
         }
         return false;
     }
 }