コード例 #1
0
ファイル: helper_functions.php プロジェクト: vanilla/vanilla
/**
 * Converts addon info into a media item.
 *
 * @param $addonName
 * @param $addonInfo
 * @param $isEnabled
 * @param $addonType
 * @param $filter
 */
function writeAddonMedia($addonName, $addonInfo, $isEnabled, $addonType, $filter)
{
    $capitalCaseSheme = new \Vanilla\Utility\CapitalCaseScheme();
    $addonInfo = $capitalCaseSheme->convertArrayKeys($addonInfo, ['RegisterPermissions']);
    $screenName = Gdn_Format::display(val('Name', $addonInfo, $addonName));
    $description = Gdn_Format::html(t(val('Name', $addonInfo, $addonName) . ' Description', val('Description', $addonInfo, '')));
    $id = Gdn_Format::url($addonName) . '-addon';
    $media = new MediaItemModule($screenName, '', $description, 'li', ['id' => $id]);
    $media->setView('media-addon');
    // Icon
    $addon = Gdn::addonManager()->lookupAddon($addonName);
    $iconPath = '';
    if ($addon) {
        $iconPath = $addon->getIcon();
    }
    if (!$iconPath) {
        $iconPath = val('IconUrl', $addonInfo, 'applications/dashboard/design/images/addon-placeholder.png');
    }
    $media->setImage($iconPath);
    // Settings button
    $settingsUrl = $isEnabled ? val('SettingsUrl', $addonInfo, '') : '';
    $settingsPopupClass = val('UsePopupSettings', $addonInfo, true) ? ' js-modal' : '';
    if ($settingsUrl != '') {
        $attr['class'] = 'btn btn-icon-border' . $settingsPopupClass;
        $attr['aria-label'] = sprintf(t('Settings for %s'), $screenName);
        $attr['data-reload-page-on-save'] = false;
        $media->addButton(dashboardSymbol('settings'), $settingsUrl, $attr);
    }
    // Toggle
    if ($addonType === 'locales') {
        $action = $isEnabled ? 'disable' : 'enable';
    } else {
        $action = $filter;
    }
    if ($isEnabled) {
        $label = sprintf(t('Disable %s'), $screenName);
    } else {
        $label = sprintf(t('Enable %s'), $screenName);
    }
    $url = '/settings/' . $addonType . '/' . $action . '/' . $addonName;
    $media->setToggle(slugify($addonName), $isEnabled, $url, $label);
    // Meta
    $info = [];
    // Requirements
    $requiredApplications = val('RequiredApplications', $addonInfo, false);
    $requiredPlugins = val('RequiredPlugins', $addonInfo, false);
    $requirements = [];
    if (is_array($requiredApplications)) {
        foreach ($requiredApplications as $requiredApplication => $versionInfo) {
            $requirements[] = sprintf(t('%1$s Version %2$s'), $requiredApplication, $versionInfo);
        }
    }
    if (is_array($requiredPlugins)) {
        foreach ($requiredPlugins as $requiredPlugin => $versionInfo) {
            $requirements[] = sprintf(t('%1$s Version %2$s'), $requiredPlugin, $versionInfo);
        }
    }
    if (!empty($requirements)) {
        $requirementsMeta = sprintf(t('Requires: %s'), implode(', ', $requirements));
        $info[] = $requirementsMeta;
    }
    // Authors
    $author = val('Author', $addonInfo, '');
    $authors = [];
    // Check if singular author is set
    if ($author) {
        $authorUrl = val('AuthorUrl', $addonInfo, '');
        if ($authorUrl) {
            $authors[] = anchor($author, $authorUrl);
        } else {
            $authors[] = $author;
        }
    }
    // Check for multiple authors
    foreach (val('Authors', $addonInfo, []) as $author) {
        if (val('Homepage', $author)) {
            $authors[] = anchor(val('Name', $author), val('Homepage', $author));
        } else {
            $authors[] = val('Name', $author);
        }
    }
    if ($authors) {
        $authors = implode(', ', $authors);
        $info[] = sprintf(t('Created by %s'), $authors);
    }
    // Version Info
    $version = Gdn_Format::display(val('Version', $addonInfo, ''));
    $newVersion = val('NewVersion', $addonInfo, '');
    $upgrade = $newVersion != '' && version_compare($newVersion, $version, '>');
    if ($version != '') {
        $info[] = sprintf(t('Version %s'), $version);
    }
    $pluginUrl = val('PluginUrl', $addonInfo, '');
    if ($upgrade && $pluginUrl) {
        $info[] = anchor(printf(t('%1$s version %2$s is available.'), $screenName, $newVersion), combinePaths(array($pluginUrl, 'find', urlencode($screenName)), '/'));
    }
    if ($pluginUrl != '') {
        $info[] = anchor(t('Visit Site'), $pluginUrl);
    }
    // Extra meta in addon array
    if ($meta = val('Meta', $addonInfo)) {
        foreach ($meta as $key => $value) {
            if (is_numeric($key)) {
                $info[] = $value;
            } else {
                $info[] = t($key) . ': ' . $value;
            }
        }
    }
    $media->setMeta($info);
    echo $media;
}
コード例 #2
0
 /**
  * Calculate an old info array from a new {@link Addon}.
  *
  * This is a backwards compatibility function only and should not be used for new code.
  *
  * @param Addon $addon The addon to calculate.
  * @return array Returns an info array.
  * @deprecated
  */
 public static function calcOldInfoArray(Addon $addon)
 {
     $info = $addon->getInfo();
     $capitalCaseSheme = new \Vanilla\Utility\CapitalCaseScheme();
     $info = $capitalCaseSheme->convertArrayKeys($info, ['RegisterPermissions']);
     // This is the basic information from scanPluginFile().
     $name = $addon->getInfoValue('keyRaw', $addon->getKey());
     $info['Index'] = $name;
     $info['ClassName'] = $addon->getPluginClass();
     $info['PluginFilePath'] = $addon->getClassPath($addon->getPluginClass(), Addon::PATH_FULL);
     $info['PluginRoot'] = $addon->path();
     touchValue('Name', $info, $name);
     touchValue('Folder', $info, $name);
     $info['Dir'] = $addon->path('', Addon::PATH_ADDON);
     if ($icon = $addon->getIcon(Addon::PATH_ADDON)) {
         $info['IconUrl'] = $icon;
     }
     // This is some additional information from indexSearchPath().
     if ($info['PluginFilePath']) {
         $info['RealFile'] = realpath($info['PluginFilePath']);
     }
     $info['RealRoot'] = realpath($info['PluginRoot']);
     $info['SearchPath'] = dirname($addon->path());
     // Rejoin the authors.
     $names = [];
     $emails = [];
     $homepages = [];
     foreach ($addon->getInfoValue('authors', []) as $author) {
         if (isset($author['name'])) {
             $names[] = $author['name'];
         }
         if (isset($author['email'])) {
             $emails[] = $author['email'];
         }
         if (isset($author['homepage'])) {
             $homepages[] = $author['homepage'];
         }
     }
     if (!empty($names)) {
         $info['Author'] = implode(', ', $names);
     }
     if (!empty($emails)) {
         $info['AuthorEmail'] = implode(', ', $emails);
     }
     if (!empty($homepages)) {
         $info['AuthorUrl'] = implode(', ', $homepages);
     }
     unset($info['Authors']);
     // Themes have their own particular fields.
     if ($addon->getType() === Addon::TYPE_THEME) {
         $info['ThemeRoot'] = $addon->path('');
         if (file_exists($addon->path('about.php'))) {
             $info['AboutFile'] = $addon->path('about.php');
             $info['RealAboutFile'] = realpath($info['AboutFile']);
         }
         if ($hooksClass = $addon->getPluginClass()) {
             $info['HooksFile'] = $addon->getClassPath($hooksClass);
             $info['RealHooksFile'] = realpath($info['HooksFile']);
         }
         if ($screenshot = $addon->getIcon(Addon::PATH_ADDON)) {
             if (basename($screenshot) === 'mobile.png') {
                 $info['MobileScreenshotUrl'] = $screenshot;
             } else {
                 $info['ScreenshotUrl'] = $screenshot;
             }
         }
     }
     return $info;
 }