Exemplo n.º 1
0
    public override CommandResult Run()
    {
      using (var cs = new ContextSwitcher(Context, Path))
      {
        if (cs.Result.Status != CommandStatus.Success)
          return cs.Result;

        var languages = new List<Language>();
        languages.Add(Context.CurrentItem.Language);

        if (AllLanguages)
        {
          languages.Clear();
          languages.AddRange(Context.CurrentItem.Languages);
        }

        var versions = new List<int>();
        versions.Add(Context.CurrentItem.Version.Number);

        if (OtherVersions)
        {
          versions.Clear();
          versions.AddRange(Context.CurrentItem.Versions.GetVersionNumbers().Select(x => x.Number));
          versions.Remove(Context.CurrentItem.Version.Number);

          if (AllLanguages)
          {
            // Other languages may have different version numbers. Need to add those extras
            foreach (var language in Context.CurrentItem.Languages)
            {
              var langItem = Context.CurrentItem.Database.GetItem(Context.CurrentItem.ID, language);
              foreach (var version in langItem.Versions.GetVersionNumbers())
              {
                if (!versions.Contains(version.Number) && version.Number != Context.CurrentItem.Version.Number)
                  versions.Add(version.Number);
              }
            }
          }
        }

        var count = 0;

        foreach (var language in languages)
        {
          foreach (var version in versions)
          {
            var versionItem = Context.CurrentItem.Database.GetItem(Context.CurrentItem.ID, language, new Version(version));
            if (versionItem != null)
            {
              versionItem.Versions.RemoveVersion();
              count++;
            }
          }
        }

        return new CommandResult(CommandStatus.Success, "Deleted {0} version{1}".FormatWith(count, count == 1 ? string.Empty : "s"));
      }
    }