/**
  * This function is called from the resources index when a user uses the
  * With Selected, or Apply, menu. The type of resource is given by
  * `$resource_type`. At this time the only two valid values,
  * `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DATASOURCE`.
  *
  * The function handles 'delete', 'attach', 'detach', 'attach all',
  * 'detach all' actions.
  *
  * @param integer $resource_type
  *  Either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DATASOURCE`
  * @throws Exception
  */
 public function __actionIndex($resource_type)
 {
     $manager = ResourceManager::getManagerFromType($resource_type);
     $checked = is_array($_POST['items']) ? array_keys($_POST['items']) : null;
     $context = Administration::instance()->getPageCallback();
     if (isset($_POST['action']) && is_array($_POST['action'])) {
         /**
          * Extensions can listen for any custom actions that were added
          * through `AddCustomPreferenceFieldsets` or `AddCustomActions`
          * delegates.
          *
          * @delegate CustomActions
          * @since Symphony 2.3.2
          * @param string $context
          *  '/blueprints/datasources/' or '/blueprints/events/'
          * @param array $checked
          *  An array of the selected rows. The value is usually the ID of the
          *  the associated object.
          */
         Symphony::ExtensionManager()->notifyMembers('CustomActions', $context['pageroot'], array('checked' => $checked));
         if (is_array($checked) && !empty($checked)) {
             if ($_POST['with-selected'] == 'delete') {
                 $canProceed = true;
                 foreach ($checked as $handle) {
                     $path = call_user_func(array($manager, '__getDriverPath'), $handle);
                     // Don't allow Extension resources to be deleted. RE: #2027
                     if (stripos($path, EXTENSIONS) === 0) {
                         continue;
                     } elseif (!General::deleteFile($path)) {
                         $folder = str_replace(DOCROOT, '', $path);
                         $folder = str_replace('/' . basename($path), '', $folder);
                         $this->pageAlert(__('Failed to delete %s.', array('<code>' . basename($path) . '</code>')) . ' ' . __('Please check permissions on %s', array('<code>' . $folder . '</code>')), Alert::ERROR);
                         $canProceed = false;
                     } else {
                         $pages = ResourceManager::getAttachedPages($resource_type, $handle);
                         foreach ($pages as $page) {
                             ResourceManager::detach($resource_type, $handle, $page['id']);
                         }
                     }
                 }
                 if ($canProceed) {
                     redirect(Administration::instance()->getCurrentPageURL());
                 }
             } elseif (preg_match('/^(at|de)?tach-(to|from)-page-/', $_POST['with-selected'])) {
                 if (substr($_POST['with-selected'], 0, 6) == 'detach') {
                     $page = str_replace('detach-from-page-', '', $_POST['with-selected']);
                     foreach ($checked as $handle) {
                         ResourceManager::detach($resource_type, $handle, $page);
                     }
                 } else {
                     $page = str_replace('attach-to-page-', '', $_POST['with-selected']);
                     foreach ($checked as $handle) {
                         ResourceManager::attach($resource_type, $handle, $page);
                     }
                 }
                 if ($canProceed) {
                     redirect(Administration::instance()->getCurrentPageURL());
                 }
             } elseif (preg_match('/^(at|de)?tach-all-pages$/', $_POST['with-selected'])) {
                 $pages = PageManager::fetch(false, array('id'));
                 if (substr($_POST['with-selected'], 0, 6) == 'detach') {
                     foreach ($checked as $handle) {
                         foreach ($pages as $page) {
                             ResourceManager::detach($resource_type, $handle, $page['id']);
                         }
                     }
                 } else {
                     foreach ($checked as $handle) {
                         foreach ($pages as $page) {
                             ResourceManager::attach($resource_type, $handle, $page['id']);
                         }
                     }
                 }
                 redirect(Administration::instance()->getCurrentPageURL());
             }
         }
     }
 }
 /**
  * Given a resource type, a handle and an array of pages, this function will
  * ensure that the resource is attached to the given pages. Note that this
  * function will also remove the resource from all pages that are not provided
  * in the `$pages` parameter.
  *
  * @since Symphony 2.4
  * @param integer $type
  *  The resource type, either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DS`
  * @param string $r_handle
  *  The handle of the resource.
  * @param array $pages
  *  An array of Page ID's to attach this resource to.
  * @return boolean
  */
 public static function setPages($type, $r_handle, $pages = array())
 {
     if (!is_array($pages)) {
         $pages = array();
     }
     // Get attached pages
     $attached_pages = ResourceManager::getAttachedPages($type, $r_handle);
     $currently_attached_pages = array();
     foreach ($attached_pages as $page) {
         $currently_attached_pages[] = $page['id'];
     }
     // Attach this datasource to any page that is should be attached to
     $diff_to_attach = array_diff($pages, $currently_attached_pages);
     foreach ($diff_to_attach as $diff_page) {
         ResourceManager::attach($type, $r_handle, $diff_page);
     }
     // Remove this datasource from any page where it once was, but shouldn't be anymore
     $diff_to_detach = array_diff($currently_attached_pages, $pages);
     foreach ($diff_to_detach as $diff_page) {
         ResourceManager::detach($type, $r_handle, $diff_page);
     }
     return true;
 }
 public function __actionEdit()
 {
     if (array_key_exists('save', $_POST['action'])) {
         return $this->__formAction();
     } elseif (array_key_exists('delete', $_POST['action'])) {
         /**
          * Prior to deleting the Event file. Target file path is provided.
          *
          * @delegate EventPreDelete
          * @since Symphony 2.2
          * @param string $context
          * '/blueprints/events/'
          * @param string $file
          *  The path to the Event file
          */
         Symphony::ExtensionManager()->notifyMembers('EventPreDelete', '/blueprints/events/', array('file' => EVENTS . "/event." . $this->_context[1] . ".php"));
         if (!General::deleteFile(EVENTS . '/event.' . $this->_context[1] . '.php')) {
             $this->pageAlert(__('Failed to delete %s.', array('<code>' . $this->_context[1] . '</code>')) . ' ' . __('Please check permissions on %s.', array('<code>/workspace/events</code>')), Alert::ERROR);
         } else {
             $pages = ResourceManager::getAttachedPages(RESOURCE_TYPE_EVENT, $this->_context[1]);
             foreach ($pages as $page) {
                 ResourceManager::detach(RESOURCE_TYPE_EVENT, $this->_context[1], $page['id']);
             }
             redirect(SYMPHONY_URL . '/blueprints/events/');
         }
     }
 }
 /**
  * This function is called from the resources index when a user uses the
  * With Selected, or Apply, menu. The type of resource is given by
  * `$resource_type`. At this time the only two valid values,
  * `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DATASOURCE`.
  *
  * The function handles 'delete', 'attach', 'detach', 'attach all',
  * 'detach all' actions.
  *
  * @param integer $resource_type
  *  Either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DATASOURCE`
  */
 public function __actionIndex($resource_type)
 {
     $manager = ResourceManager::getManagerFromType($resource_type);
     if (isset($_POST['action']) && is_array($_POST['action'])) {
         $checked = $_POST['items'] ? @array_keys($_POST['items']) : NULL;
         if (is_array($checked) && !empty($checked)) {
             if ($_POST['with-selected'] == 'delete') {
                 $canProceed = true;
                 foreach ($checked as $handle) {
                     $path = call_user_func(array($manager, '__getDriverPath'), $handle);
                     if (!General::deleteFile($path)) {
                         $folder = str_replace(DOCROOT, '', $path);
                         $folder = str_replace('/' . basename($path), '', $folder);
                         $this->pageAlert(__('Failed to delete %s.', array('<code>' . basename($path) . '</code>')) . ' ' . __('Please check permissions on %s', array('<code>' . $folder . '</code>')), Alert::ERROR);
                         $canProceed = false;
                     }
                 }
                 if ($canProceed) {
                     redirect(Administration::instance()->getCurrentPageURL());
                 }
             } else {
                 if (preg_match('/^(at|de)?tach-(to|from)-page-/', $_POST['with-selected'])) {
                     if (substr($_POST['with-selected'], 0, 6) == 'detach') {
                         $page = str_replace('detach-from-page-', '', $_POST['with-selected']);
                         foreach ($checked as $handle) {
                             ResourceManager::detach($resource_type, $handle, $page);
                         }
                     } else {
                         $page = str_replace('attach-to-page-', '', $_POST['with-selected']);
                         foreach ($checked as $handle) {
                             ResourceManager::attach($resource_type, $handle, $page);
                         }
                     }
                     if ($canProceed) {
                         redirect(Administration::instance()->getCurrentPageURL());
                     }
                 } else {
                     if (preg_match('/^(at|de)?tach-all-pages$/', $_POST['with-selected'])) {
                         $pages = PageManager::fetch(false, array('id'));
                         if (substr($_POST['with-selected'], 0, 6) == 'detach') {
                             foreach ($checked as $handle) {
                                 foreach ($pages as $page) {
                                     ResourceManager::detach($resource_type, $handle, $page['id']);
                                 }
                             }
                         } else {
                             foreach ($checked as $handle) {
                                 foreach ($pages as $page) {
                                     ResourceManager::attach($resource_type, $handle, $page['id']);
                                 }
                             }
                         }
                         redirect(Administration::instance()->getCurrentPageURL());
                     }
                 }
             }
         }
     }
 }