/**
  * This function checks that if the given extension has provided Fields,
  * Data Sources or Events, that they aren't in use before the extension
  * is uninstalled or disabled. This prevents exceptions from occurring when
  * accessing an object that was using something provided by this Extension
  * can't anymore because it has been removed.
  *
  * @param Extension $obj
  *  An extension object
  * @return boolean
  */
 private static function __canUninstallOrDisable(Extension $obj)
 {
     $extension_handle = strtolower(preg_replace('/^extension_/i', NULL, get_class($obj)));
     $about = self::about($extension_handle);
     // Fields:
     if (is_dir(EXTENSIONS . "/{$extension_handle}/fields")) {
         foreach (glob(EXTENSIONS . "/{$extension_handle}/fields/field.*.php") as $file) {
             $type = preg_replace(array('/^field\\./i', '/\\.php$/i'), NULL, basename($file));
             if (FieldManager::isFieldUsed($type)) {
                 throw new Exception(__('The field ‘%s’, provided by the Extension ‘%s’, is currently in use.', array(basename($file), $about['name'])) . ' ' . __("Please remove it from your sections prior to uninstalling or disabling."));
             }
         }
     }
     // Data Sources:
     if (is_dir(EXTENSIONS . "/{$extension_handle}/data-sources")) {
         foreach (glob(EXTENSIONS . "/{$extension_handle}/data-sources/data.*.php") as $file) {
             $handle = preg_replace(array('/^data\\./i', '/\\.php$/i'), NULL, basename($file));
             if (PageManager::isDataSourceUsed($handle)) {
                 throw new Exception(__('The Data Source ‘%s’, provided by the Extension ‘%s’, is currently in use.', array(basename($file), $about['name'])) . ' ' . __("Please remove it from your pages prior to uninstalling or disabling."));
             }
         }
     }
     // Events
     if (is_dir(EXTENSIONS . "/{$extension_handle}/events")) {
         foreach (glob(EXTENSIONS . "/{$extension_handle}/events/event.*.php") as $file) {
             $handle = preg_replace(array('/^event\\./i', '/\\.php$/i'), NULL, basename($file));
             if (PageManager::isEventUsed($handle)) {
                 throw new Exception(__('The Event ‘%s’, provided by the Extension ‘%s’, is currently in use.', array(basename($file), $about['name'])) . ' ' . __("Please remove it from your pages prior to uninstalling or disabling."));
             }
         }
     }
     // Text Formatters
     if (is_dir(EXTENSIONS . "/{$extension_handle}/text-formatters")) {
         foreach (glob(EXTENSIONS . "/{$extension_handle}/text-formatters/formatter.*.php") as $file) {
             $handle = preg_replace(array('/^formatter\\./i', '/\\.php$/i'), NULL, basename($file));
             if (FieldManager::isTextFormatterUsed($handle)) {
                 throw new Exception(__('The Text Formatter ‘%s’, provided by the Extension ‘%s’, is currently in use.', array(basename($file), $about['name'])) . ' ' . __("Please remove it from your fields prior to uninstalling or disabling."));
             }
         }
     }
 }