If Statements running all three codes - statements

using System;
using System.Drawing;
namespace Group_cafe7
{
class Program
{
static void Main(string[] args)
{
string name, milk, cap, latt, amer;
int choice;
char sizeC, sizeL, sizeA;
double cappucciniosmall, cappucciniomedium, cappucciniolarge, lattesmall, lattemedium, lattelarge, americanosmall, americanomedium, americanolarge, discount;
Console.WriteLine("Hello, Please enter your name: ");
name = Console.ReadLine();
Console.WriteLine(" Please enter your number of choice ");
Console.WriteLine(" 1.Cappuccinio 2. Latte 3. Americano ");
choice = Convert.ToInt32(Console.ReadLine());
if (choice == 1)
{
Console.WriteLine(" Choose size of choice: ");
Console.WriteLine(" S, M, L ");
sizeC = Convert.ToChar(Console.ReadLine());
cappucciniosmall = 13.50;
cappucciniomedium = 18.00;
cappucciniolarge = 25.50;
if (sizeC == 'S')
{
Console.WriteLine(" Add Hotmilk yes/no ");
milk = Console.ReadLine();
Console.WriteLine(" Cappuccinio " + cappucciniosmall);
cappucciniosmall = Convert.ToDouble(Console.ReadLine());
}
if (sizeC == 'M')
{
Console.WriteLine(" Add Hotmilk yes/no ");
milk = (Console.ReadLine());
Console.WriteLine(" Cappuccinio " + cappucciniomedium);
cappucciniomedium = Convert.ToDouble(Console.ReadLine());
}
if (sizeC == 'L')
{
Console.WriteLine(" Add Hotmilk yes/no ");
milk = (Console.ReadLine());
Console.WriteLine(" Cappuccinio " + cappucciniolarge);
cappucciniolarge = Convert.ToDouble(Console.ReadLine());
}
Console.WriteLine("Hello, Please enter your name: ");
name = Console.ReadLine();
Console.WriteLine(" Please enter your number of choice ");
Console.WriteLine(" 1.Cappuccinio 2. Latte 3. Americano ");
choice = Convert.ToInt32(Console.ReadLine());
}
else if (choice == 2)
{
Console.WriteLine(" Choose size of choice: ");
Console.WriteLine(" S, M, L ");
sizeL = Convert.ToChar(Console.ReadLine());
lattesmall = 15.50;
lattemedium = 20.00;
lattelarge = 27.50;
if (sizeL == 'S')
{
Console.WriteLine(" Add Hotmilk yes/no ");
milk = Console.ReadLine();
Console.WriteLine(" Latte " + lattesmall);
lattesmall = Convert.ToDouble(Console.ReadLine());
}
if (sizeL == 'M')
{
Console.WriteLine(" Add Hotmilk yes/no ");
milk = (Console.ReadLine());
Console.WriteLine(" Latte " + lattemedium);
lattemedium = Convert.ToDouble(Console.ReadLine());
}
if (sizeL == 'L')
{
Console.WriteLine(" Add Hotmilk yes/no ");
milk = (Console.ReadLine());
Console.WriteLine(" Latte " + lattelarge);
lattelarge = Convert.ToDouble(Console.ReadLine());
}
else if (choice == 3)
{
}
Console.WriteLine(" Choose size of choice: ");
Console.WriteLine(" S, M, L ");
sizeA = Convert.ToChar(Console.ReadLine());
americanosmall = 17.50;
americanomedium = 22.00;
americanolarge = 29.50;
if (sizeA == 'S')
{
Console.WriteLine(" Add Hotmilk yes/no ");
milk = Console.ReadLine();
Console.WriteLine(" Americano " + americanosmall);
americanosmall = Convert.ToDouble(Console.ReadLine());
}
if (sizeA == 'M')
{
Console.WriteLine(" Add Hotmilk yes/no ");
milk = (Console.ReadLine());
Console.WriteLine(" Americano " + americanomedium);
americanomedium = Convert.ToDouble(Console.ReadLine());
}
if (sizeA == 'L')
{
Console.WriteLine(" Add Hotmilk yes/no ");
milk = (Console.ReadLine());
Console.WriteLine(" Americano " + americanolarge);
americanolarge = Convert.ToDouble(Console.ReadLine());
}
}
}
}
}

Related

if statement in public void won't run

It maybe a very stupid question, but my public void with if statement doesn't run and I just can't find the answer why. I tried debugging, looking for similar topics in the web, but nothing helped. The funny thing in a video course which I am currently studying where a similar task is the codes runs although there are some differences which aren't relevant.
public class Program
{
static int InputYear;
static int YearDiv2;
static int YearDiv4;
static int YearDiv100;
static int YearDiv400;
string Yes = "Yes";
string No = "No";
string DivBy = "Divisible by ";
string LeapYear = "Leap Year: ";
public static void Main(string[] args)
{
//Section 5 Task
Console.WriteLine("Please enter a year value:");
InputYear = Convert.ToInt32(Console.ReadLine());
YearDiv2 = InputYear % 2;
//Console.WriteLine(YearDiv2);
YearDiv4 = InputYear % 4;
YearDiv100 = InputYear % 100;
YearDiv400 = InputYear % 400;
Console.WriteLine($"Year entered: {InputYear}");
}
public void YearNotOdd()
{
Console.WriteLine("It runs");
if (InputYear == 0)
{
if (YearDiv4 == 0 && YearDiv100 == 0 && YearDiv400 == 0)
{
Console.WriteLine(#"{DivBy} 4: {Yes}");
Console.WriteLine(#"{DivBy} 100: {Yes}");
Console.WriteLine(#"{DivBy} 400: {Yes}");
Console.WriteLine(LeapYear + Yes);
}
else if(YearDiv4 == 0 && YearDiv100 == 0 && YearDiv400 != 0)
{
Console.WriteLine(#"{DivBy} 4: {Yes}");
Console.WriteLine(#"{DivBy} 100: {Yes}");
Console.WriteLine(#"{DivBy} 400: {No}");
Console.WriteLine(LeapYear + No);
}
else
{
Console.WriteLine(#"{DivBy} 4: {No}");
Console.WriteLine(#"{DivBy} 100: {No}");
Console.WriteLine(#"{DivBy} 400: {No}");
Console.WriteLine(LeapYear + No);
}
}
else
{
Console.WriteLine(#"{DivBy} 4: {No}");
Console.WriteLine(#"{DivBy} 100: {No}");
Console.WriteLine(#"{DivBy} 400: {No}");
Console.WriteLine(LeapYear + No);
}
}
}
You'll need to use the method YearNotOdd() somewhere, if you create a new method, then it needs to be called somewhere to be used.
To try it out, you should put the YearNotOdd() in your Main() method.
For example:
public static void Main(string[] args)
{
//Section 5 Task
Console.WriteLine("Please enter a year value:");
InputYear = Convert.ToInt32(Console.ReadLine());
YearDiv2 = InputYear % 2;
//Console.WriteLine(YearDiv2);
YearDiv4 = InputYear % 4;
YearDiv100 = InputYear % 100;
YearDiv400 = InputYear % 400;
Console.WriteLine($"Year entered: {InputYear}");
YearNotOdd(); //place it here
}
You might need to add static to the YearNotOdd() for this.

What is wrong with this code ? I am extracting values form a predefined 2-dimensional array

#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
struct Pokemon
{
string type1, type2;
};
struct Trainer
{
Pokemon poke1, poke2;
int score;
};
class BPI
{
Trainer red, blue;
public:
void DataAccept()
{
// Trainer RED
{
// Pokemon 1
cout<<"Please enter 3 letter type codes for 1st Pokemon of Red Trainer IN UPPERCASE :"<<endl;
cin>>red.poke1.type1>>red.poke1.type2;
// Pokemon 2
cout<<"Please enter 3 letter type codes for 2nd Pokemon of Red Trainer IN UPPERCASE :"<<endl;
cin>>red.poke2.type1>>red.poke2.type2;
}
cout<<endl;
// Trainer BLUE
{
// Pokemon 1
cout<<"Please enter 3 letter type codes for 1st Pokemon of Blue Trainer IN UPPERCASE :"<<endl;
cin>>blue.poke1.type1>>blue.poke1.type2;
// Pokemon 2
cout<<"Please enter 3 letter type codes for 2nd Pokemon of Blue Trainer IN UPPERCASE :"<<endl;
cin>>blue.poke2.type1>>blue.poke2.type2;
}
}
void process()
{
int kci;
int i;
for (i = 1;i <17;i++)
{
string tk_red; string tk_blue;
switch(i)
{
case(1): {tk_red = red.poke1.type1; tk_blue = blue.poke1.type1;}
case(2): {tk_red = red.poke1.type1; tk_blue = blue.poke1.type2;}
case(3): {tk_red = red.poke1.type1; tk_blue = blue.poke2.type1;}
case(4): {tk_red = red.poke1.type1; tk_blue = blue.poke2.type2;}
case(5): {tk_red = red.poke1.type2; tk_blue = blue.poke1.type1;}
case(6): {tk_red = red.poke1.type2; tk_blue = blue.poke1.type2;}
case(7): {tk_red = red.poke1.type2; tk_blue = blue.poke2.type1;}
case(8): {tk_red = red.poke1.type2; tk_blue = blue.poke2.type2;}
case(9): {tk_red = red.poke2.type1; tk_blue = blue.poke1.type1;}
case(10): {tk_red = red.poke2.type1; tk_blue = blue.poke1.type2;}
case(11): {tk_red = red.poke2.type1; tk_blue = blue.poke2.type1;}
case(12): {tk_red = red.poke2.type1; tk_blue = blue.poke2.type2;}
case(13): {tk_red = red.poke2.type2; tk_blue = blue.poke1.type1;}
case(14): {tk_red = red.poke2.type2; tk_blue = blue.poke1.type2;}
case(15): {tk_red = red.poke2.type2; tk_blue = blue.poke2.type1;}
case(16): {tk_red = red.poke2.type2; tk_blue = blue.poke2.type2;}
}
for (kci = 0; kci < 19; kci ++)
{
state(tk_red,tk_blue);
}
}
}
int code(string type)
{
if (type == "NIL")
{
return 0;
}
else if (type == "NOR")
{
return 1;
}
else if (type == "FIR")
{
return 2;
}
else if (type == "WAT")
{
return 3;
}
else if (type == "GRA")
{
return 4;
}
else if (type == "ELE")
{
return 3;
}
else if (type == "PSY")
{
return 11;
}
else if (type == "GHO")
{
return 14;
}
else if (type == "DAR")
{
return 16;
}
else if (type == "FAI")
{
return 18;
}
else if (type == "ICE")
{
return 5;
}
else if (type == "DRA")
{
return 15;
}
else if (type == "GRO")
{
return 9;
}
else if (type == "ROC")
{
return 13;
}
else if (type == "STE")
{
return 17;
}
else if (type == "FLY")
{
return 10;
}
else if (type == "POI")
{
return 8;
}
else if (type == "FIG")
{
return 7;
}
else if (type == "BUG")
{
return 12;
}
}
void state(string kred,string kblue)
{
int typechart[20][20] =
{
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,-1,-2,0,0,-1,0},
{0,0,-1,-1,0,2,2,0,0,0,0,0,2,-1,0,-1,0,2,0},
{0,0,2,-1,0,-1,0,0,0,2,0,0,0,2,0,-1,0,0,0},
{0,0,0,2,-1,-1,0,0,0,-2,2,0,0,0,0,0,-2,0,0},
{0,0,-1,2,0,-1,0,0,-1,2,-1,0,-1,2,0,-1,0,-1,0},
{0,0,-1,-1,0,2,-1,0,0,2,2,0,0,0,0,2,0,-1,0},
{0,2,0,0,0,0,2,0,-1,0,-1,-1,-1,2,-2,0,2,2,-1},
{0,0,0,0,0,2,0,0,-1,-1,0,0,0,-1,-1,0,0,-2,2},
{0,0,2,0,2,-1,0,0,2,0,-2,0,-1,2,0,0,0,2,0},
{0,0,0,0,-1,2,0,2,0,0,0,0,2,-1,0,0,0,-1,0},
{0,0,0,0,0,0,0,2,2,0,0,-1,0,0,0,0,-2,-1,0},
{0,0,-1,0,0,2,0,-1,-1,0,-1,2,0,0,-1,0,2,-1,-1},
{0,0,2,0,0,0,2,-1,0,-1,2,0,2,0,0,0,0,-1,0},
{0,-2,0,0,0,0,0,0,0,0,0,2,0,0,2,0,-1,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,-1,-2},
{0,0,0,0,0,0,0,-1,0,0,0,2,0,0,2,0,-1,0,-1},
{0,0,-1,-1,-1,0,2,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,-1,0,0,0,0,0,2,-1,0,0,0,0,0,2,2,-1,0}
};
red.score += typechart[code(kred)][code(kblue)];
blue.score += typechart[code(kblue)][code(kred)];
}
};
int main()
{
BPI ob;
ob.DataAccept();
ob.process();
return 0;
}
This will accept 8 strings in 2 teams, them match them up, & modify the score accordingly, then it will print the score.
But it returns 0 for both trainers.
NOTE : I'm a pokemon freak, so I'm writing a program that compares the type advantages of the trainers.
You are missing break statements.
switch(i)
{
case(1): {tk_red = red.poke1.type1; tk_blue = blue.poke1.type1;}
case(2): {tk_red = red.poke1.type1; tk_blue = blue.poke1.type2;}
...
tk_red and tk_blue will always have values as per case(16).
You are showing the score nowhere. try add at the end of the process method something like cout<
A little advice, avoid the usage of all those elseif, use insead a switch loop as u did on the method before

Why is not saving the changes in the variable?

I'm kind of noobie when it comes to OOP, so I'm probably making a mistake, but I can't find it, here's the part of the code that fails:
The util file is just a file with the error messages
In main.cc:
int main(){
Ship imperialDestroyer(IMPERIAL);
Ship rebelShip(REBEL);
cout<<imperialDestroyer<<endl; //this just print the ship, you can ignore it
cout<<rebelShip<<endl;
imperialDestroyer.improveFighter(); //this is what fails
cout<<imperialDestroyer<<endl;
}
In Ship.cc:
bool Ship::improveFighter(){
int num, cantidad, cost;
char option, respuesta;
bool improved = false;
cout << "Select fighter number: ";
cin >> num;
num = num-1;
if(num > this->fleet.getNumFighters() || num < 0)
Util::error(WRONG_NUMBER);
else{
cout << "What to improve (v/a/s)?";
cin>>option;
if(option!='v' && option!='a' && option!='s')
Util::error(UNKNOWN_OPTION);
else{
cout << "Amount: ";
cin >> cantidad;
if(option == 'v')
cost = 2 * cantidad;
else if(option == 'a')
cost = 3 * cantidad;
else if(option == 's')
cost = (cantidad + 1) / 2;
if(this->fleet.getCredits() < cost)
Util::error(NO_FUNDS);
else{
cout << "That will cost you "<< cost <<" credits. Confirm? (y/n)";
cin >> respuesta;
if(respuesta == 'y'){
this->fleet.improveFighter(num, option, cantidad, cost);
improved = true;
}
}
}
}
return improved;
}
In Fleet.cc:
void Fleet::improveFighter(int nf, char feature, int amount, int cost){
if(feature == 'v'){
getFighter(nf).increaseVelocity(amount);
}
else if(feature == 'a'){
getFighter(nf).increaseAttack(amount);
}
else if(feature == 's'){
getFighter(nf).increaseShield(amount);
}
}
}
In Fighter.cc:
Fighter Fleet::getFighter(int n) const{
return fighters[n];
}
void Fleet::improveFighter(int nf, char feature, int amount, int cost){
if(feature == 'v'){
getFighter(nf).increaseVelocity(amount);
}
else if(feature == 'a'){
getFighter(nf).increaseAttack(amount);
}
else if(feature == 's'){
getFighter(nf).increaseShield(amount);
}
}
For some reason when I try to improve some feature, it won't get saved.
Fighter Fleet::getFighter(int n) const
{
return fighters[n];
}
This returns a copy of the Fighter at position n. Modifying a copy won't affect the original.
You could return a Fighter& (i.e. a reference), but since your function is const, that won't work. You're going to have to decide what you want this function to be.

STUCK! Validation with integers, character to integer conversion, ASCII and MIDI

Ok so my C++ knowledge is so little, i've been slowly piecing together a code but in all honesty i'm surprised i've got this far.
Just to outline my task. The user is asked to enter in several notes (musical notes, C-B including Sharps, across 9 octaves) to create a melody line and then again but a bass line. After a note has been entered, a note length must also be
#‎include‬ <iostream>
#include <string>
#include <vector>
using namespace std;
int notenumber;
struct noteStorage
{
string noteName;
int midiNumber;
int noteLength;
};
///////////////////////////////////////////////////////////////////////////////////////////////
//VALIDATION FOR NOTE NAME
bool ValidateNote(string note)
{
// Step 1: If note name length is less than 2 OR more than 3, return false
if (note.length() <2 || note.length() >3)
{
cout<<"Note length must be 2 or 3 characters\n";
return false;
}
//Step 2: If true, the note must be/(or be) between A and G
else if(tolower(note[0])<'a' || tolower(note[0]) >'g')
{
cout<<"Note must be A-G\n";
return false;
}
//Step 3: If true, the last character must be a digit
else if(isdigit(note[note.length()-1]) == false)
{
cout<<"Last character must be a digit\n";
return false;
}
//Step 4: If note length is 3 note[1] (character 2) must be '#'.
else if(note.length() == 3 && note[1] != '#')
{
"Invalid sharp note\n";
return false;
}
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////
//VALIDATION FOR NOTE LENGTH
bool ValidateNoteLength (int length)
//Step 1 - If notelength is not a digit, return FALSE
{
if (length == false)
{
cout<<"Note length must be a digit/number, please re-enter";
return false;
}
//Step 2 - If notelength is less than or equal to 0 or more than 16, return FALSE
if (length <= 0 || length > 16)
{
cout<<"Note length value cannot be less than 1 or more than 16, please re-enter";
return false;
}
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
int CalculateNoteNumber(string tempName)
{
int Octave;
int Note;
tempName[0] = toupper(tempName[0]);
Octave = ((tempName[tempName.length()-1]) -48) * 12;
if (tempName.length() == 2)
{
if(tempName[0] == 'C')
{
return notenumber = 0;
}
else if(tempName[0] == 'D')
{
return notenumber = 2;
}
else if(tempName[0] == 'E')
{
return notenumber = 4;
}
else if(tempName[0] == 'F')
{
return notenumber = 5;
}
else if(tempName[0] == 'G')
{
return notenumber = 7;
}
else if(tempName[0] == 'A')
{
return notenumber = 9;
}
else
{
return notenumber = 11;
}
}
else if (tempName.length() == 3)
{
if(tempName[0] == 'C')
{
return notenumber = 1;
}
else if(tempName[0] == 'D')
{
return notenumber = 3;
}
else if(tempName[0] == 'F')
{
return notenumber = 6;
}
else if(tempName[0] == 'G')
{
return notenumber = 8;
}
else
{
return notenumber = 10;
}
}
int main();
{
noteStorage noteData[8];
//string note;
for (int i = 0; i < 8; i++)
{
cout<<"Please enter note: " << i << ": ";
while (1)
{
string tempName;
cin>>tempName;
int noteNumber = CalculateNoteNumber(tempName);
if (ValidateNote(tempName) == true)
{
noteData[i].noteName = tempName;
break;
}
else
{
cout << "Please enter correctly: ";
}
} //end first while
cout<<"Please enter note length: ";
while (1)
{
int tempLength;
cin>>tempLength;
if (ValidateNoteLength(tempLength) == true)
{
noteData[i].noteLength = tempLength;
break;
}
else
{
cout << "Please enter correctly: ";
}
}//end while 2
cout<<"Thank you\n";
} //end for
cout<<"Your note and note lengths are: "<<endl;
for (int i = 0; i < 8; i++)
{
cout<<noteData[i].noteName<<"Length: ";
cout<<noteData[i].noteLength<<endl;
}
system("pause");
return 0;
}
entered (with a value in milliseconds). Once the note names and note lengths have been entered, the console then converts the notenames to the corresponding midi numbers, and outputs said midi numbers, note length and note names back to the user.
I've been having the same problem for two days now; everytime I build the solution it comes back with the same error:
"Fatal error C1075, end of file found before last brace '{' was
matched".
If anyone could point me the right way to solving this it would be much appreciated!!
You missed a } at the end of int CalculateNoteNumber(string tempName).
You will also have to remove the ; after int main() for your program to compile.
If you would have formatted your code properly you could have fixed these errors on your own.

sitecore workbox does not prompt for comment when approving multiple items

I have a sitecore workflow. When I approve an item with the Approve button right next to the item it asks for comment. When I approve multiple items using the Approve (selected) or Approve (all) buttons it does not ask for comment. Why is that? Can I make it ask for comment and associate that comment with all the items that being approved? Thanks
In order to enable comments for Selected or All items you need to:
Copy Website\sitecore\shell\Applications\Workbox\Workbox.xml to Website\sitecore\shell\Override\ directory and change <CodeBeside Type="..." /> to <CodeBeside Type="My.Assembly.Namespace.WorkboxForm,My.Assembly" /> where My.Assembly and My.Assembly.Namespace matches your project.
Create your own class WorkboxForm that will override Sitecore WorkboxForm and match Type from point 1. and use the edited code of WorkBox form:
using Sitecore;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Exceptions;
using Sitecore.Globalization;
using Sitecore.Resources;
using Sitecore.Shell.Framework.CommandBuilders;
using Sitecore.Text;
using Sitecore.Web;
using Sitecore.Web.UI.HtmlControls;
using Sitecore.Web.UI.Sheer;
using Sitecore.Web.UI.XmlControls;
using Sitecore.Workflows;
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
namespace My.Assembly.Namespace
{
public class WorkboxForm : Sitecore.Shell.Applications.Workbox.WorkboxForm
{
private NameValueCollection stateNames;
public void CommentMultiple(ClientPipelineArgs args)
{
Assert.ArgumentNotNull(args, "args");
if (!args.IsPostBack)
{
Context.ClientPage.ClientResponse.Input("Enter a comment:", string.Empty);
args.WaitForPostBack();
}
else if (args.Result.Length > 2000)
{
Context.ClientPage.ClientResponse.ShowError(new Exception(string.Format("The comment is too long.\n\nYou have entered {0} characters.\nA comment cannot contain more than 2000 characters.", args.Result.Length)));
Context.ClientPage.ClientResponse.Input("Enter a comment:", string.Empty);
args.WaitForPostBack();
}
else
{
if (args.Result == null || args.Result == "null" || args.Result == "undefined")
return;
IWorkflowProvider workflowProvider = Context.ContentDatabase.WorkflowProvider;
if (workflowProvider == null)
return;
IWorkflow workflow = workflowProvider.GetWorkflow(Context.ClientPage.ServerProperties["workflowid"] as string);
if (workflow == null)
return;
string stateId = (string)Context.ClientPage.ServerProperties["ws"];
string command = (string)Context.ClientPage.ServerProperties["command"];
string[] itemRefs = ((string)Context.ClientPage.ServerProperties["ids"] ?? string.Empty).Split(new[] { "::" }, StringSplitOptions.RemoveEmptyEntries);
bool flag = false;
int num = 0;
foreach (string itemRef in itemRefs)
{
string[] strArray = itemRef.Split(new[] { ',' });
Item items = Context.ContentDatabase.Items[strArray[0], Language.Parse(strArray[1]), Sitecore.Data.Version.Parse(strArray[2])];
if (items != null)
{
WorkflowState state = workflow.GetState(items);
if (state.StateID == stateId)
{
try
{
workflow.Execute(command, items, args.Result, true, new object[0]);
}
catch (WorkflowStateMissingException)
{
flag = true;
}
++num;
}
}
}
if (flag)
SheerResponse.Alert("One or more items could not be processed because their workflow state does not specify the next step.", new string[0]);
if (num == 0)
{
Context.ClientPage.ClientResponse.Alert("There are no selected items.");
}
else
{
UrlString urlString = new UrlString(WebUtil.GetRawUrl());
urlString["reload"] = "1";
Context.ClientPage.ClientResponse.SetLocation(urlString.ToString());
}
}
}
public override void HandleMessage(Message message)
{
Assert.ArgumentNotNull(message, "message");
switch (message.Name)
{
case "workflow:sendselectednew":
SendSelectedNew(message);
return;
case "workflow:sendallnew":
SendAllNew(message);
return;
}
base.HandleMessage(message);
}
protected override void DisplayState(IWorkflow workflow, WorkflowState state, DataUri[] items, System.Web.UI.Control control, int offset, int pageSize)
{
Assert.ArgumentNotNull(workflow, "workflow");
Assert.ArgumentNotNull(state, "state");
Assert.ArgumentNotNull(items, "items");
Assert.ArgumentNotNull(control, "control");
if (items.Length <= 0)
return;
int num = offset + pageSize;
if (num > items.Length)
num = items.Length;
for (int index = offset; index < num; ++index)
{
Item obj = Context.ContentDatabase.Items[items[index]];
if (obj != null)
CreateItem(workflow, obj, control);
}
Border border1 = new Border();
border1.Background = "#e9e9e9";
Border border2 = border1;
control.Controls.Add(border2);
border2.Margin = "0px 4px 0px 16px";
border2.Padding = "2px 8px 2px 8px";
border2.Border = "1px solid #999999";
foreach (WorkflowCommand workflowCommand in WorkflowFilterer.FilterVisibleCommands(workflow.GetCommands(state.StateID)))
{
XmlControl xmlControl1 = (XmlControl) Resource.GetWebControl("WorkboxCommand");
Assert.IsNotNull(xmlControl1, "workboxCommand is null");
xmlControl1["Header"] = (workflowCommand.DisplayName + " " + Translate.Text("(selected)"));
xmlControl1["Icon"] = workflowCommand.Icon;
xmlControl1["Command"] = ("workflow:sendselectednew(command=" + workflowCommand.CommandID + ",ws=" + state.StateID + ",wf=" + workflow.WorkflowID + ")");
border2.Controls.Add(xmlControl1);
XmlControl xmlControl2 = (XmlControl) Resource.GetWebControl("WorkboxCommand");
Assert.IsNotNull(xmlControl2, "workboxCommand is null");
xmlControl2["Header"] = (workflowCommand.DisplayName + " " + Translate.Text("(all)"));
xmlControl2["Icon"] = workflowCommand.Icon;
xmlControl2["Command"] = ("workflow:sendallnew(command=" + workflowCommand.CommandID + ",ws=" + state.StateID + ",wf=" + workflow.WorkflowID + ")");
border2.Controls.Add(xmlControl2);
}
}
private static void CreateCommand(IWorkflow workflow, WorkflowCommand command, Item item, XmlControl workboxItem)
{
Assert.ArgumentNotNull(workflow, "workflow");
Assert.ArgumentNotNull(command, "command");
Assert.ArgumentNotNull(item, "item");
Assert.ArgumentNotNull(workboxItem, "workboxItem");
XmlControl xmlControl = (XmlControl) Resource.GetWebControl("WorkboxCommand");
Assert.IsNotNull(xmlControl, "workboxCommand is null");
xmlControl["Header"] = command.DisplayName;
xmlControl["Icon"] = command.Icon;
CommandBuilder commandBuilder = new CommandBuilder("workflow:send");
commandBuilder.Add("id", item.ID.ToString());
commandBuilder.Add("la", item.Language.Name);
commandBuilder.Add("vs", item.Version.ToString());
commandBuilder.Add("command", command.CommandID);
commandBuilder.Add("wf", workflow.WorkflowID);
commandBuilder.Add("ui", command.HasUI);
commandBuilder.Add("suppresscomment", command.SuppressComment);
xmlControl["Command"] = commandBuilder.ToString();
workboxItem.AddControl(xmlControl);
}
private void CreateItem(IWorkflow workflow, Item item, System.Web.UI.Control control)
{
Assert.ArgumentNotNull(workflow, "workflow");
Assert.ArgumentNotNull(item, "item");
Assert.ArgumentNotNull(control, "control");
XmlControl workboxItem = (XmlControl) Resource.GetWebControl("WorkboxItem");
Assert.IsNotNull(workboxItem, "workboxItem is null");
control.Controls.Add(workboxItem);
StringBuilder stringBuilder = new StringBuilder(" - (");
Language language = item.Language;
stringBuilder.Append(language.CultureInfo.DisplayName);
stringBuilder.Append(", ");
stringBuilder.Append(Translate.Text("version"));
stringBuilder.Append(' ');
stringBuilder.Append(item.Version);
stringBuilder.Append(")");
Assert.IsNotNull(workboxItem, "workboxItem");
workboxItem["Header"] = item.DisplayName;
workboxItem["Details"] = (stringBuilder).ToString();
workboxItem["Icon"] = item.Appearance.Icon;
workboxItem["ShortDescription"] = item.Help.ToolTip;
workboxItem["History"] = GetHistory(workflow, item);
workboxItem["HistoryMoreID"] = Control.GetUniqueID("ctl");
workboxItem["HistoryClick"] = ("workflow:showhistory(id=" + item.ID + ",la=" + item.Language.Name + ",vs=" + item.Version + ",wf=" + workflow.WorkflowID + ")");
workboxItem["PreviewClick"] = ("Preview(\"" + item.ID + "\", \"" + item.Language + "\", \"" + item.Version + "\")");
workboxItem["Click"] = ("Open(\"" + item.ID + "\", \"" + item.Language + "\", \"" + item.Version + "\")");
workboxItem["DiffClick"] = ("Diff(\"" + item.ID + "\", \"" + item.Language + "\", \"" + item.Version + "\")");
workboxItem["Display"] = "none";
string uniqueId = Control.GetUniqueID(string.Empty);
workboxItem["CheckID"] = ("check_" + uniqueId);
workboxItem["HiddenID"] = ("hidden_" + uniqueId);
workboxItem["CheckValue"] = (item.ID + "," + item.Language + "," + item.Version);
foreach (WorkflowCommand command in WorkflowFilterer.FilterVisibleCommands(workflow.GetCommands(item)))
CreateCommand(workflow, command, item, workboxItem);
}
private string GetHistory(IWorkflow workflow, Item item)
{
Assert.ArgumentNotNull(workflow, "workflow");
Assert.ArgumentNotNull(item, "item");
WorkflowEvent[] history = workflow.GetHistory(item);
string str;
if (history.Length > 0)
{
WorkflowEvent workflowEvent = history[history.Length - 1];
string text = workflowEvent.User;
string name = Context.Domain.Name;
if (text.StartsWith(name + "\\", StringComparison.OrdinalIgnoreCase))
text = StringUtil.Mid(text, name.Length + 1);
str = string.Format(Translate.Text("{0} changed from <b>{1}</b> to <b>{2}</b> on {3}."),
StringUtil.GetString(new[]
{
text,
Translate.Text("Unknown")
}), GetStateName(workflow, workflowEvent.OldState),
GetStateName(workflow, workflowEvent.NewState),
DateUtil.FormatDateTime(workflowEvent.Date, "D", Context.User.Profile.Culture));
}
else
str = Translate.Text("No changes have been made.");
return str;
}
private static DataUri[] GetItems(WorkflowState state, IWorkflow workflow)
{
Assert.ArgumentNotNull(state, "state");
Assert.ArgumentNotNull(workflow, "workflow");
ArrayList arrayList = new ArrayList();
DataUri[] items = workflow.GetItems(state.StateID);
if (items != null)
{
foreach (DataUri index in items)
{
Item obj = Context.ContentDatabase.Items[index];
if (obj != null && obj.Access.CanRead() && (obj.Access.CanReadLanguage() && obj.Access.CanWriteLanguage()) && (Context.IsAdministrator || obj.Locking.CanLock() || obj.Locking.HasLock()))
arrayList.Add(index);
}
}
return arrayList.ToArray(typeof(DataUri)) as DataUri[];
}
private string GetStateName(IWorkflow workflow, string stateID)
{
Assert.ArgumentNotNull(workflow, "workflow");
Assert.ArgumentNotNull(stateID, "stateID");
if (stateNames == null)
{
stateNames = new NameValueCollection();
foreach (WorkflowState workflowState in workflow.GetStates())
stateNames.Add(workflowState.StateID, workflowState.DisplayName);
}
return StringUtil.GetString(new[] {stateNames[stateID], "?"});
}
private void SendAll(Message message)
{
Assert.ArgumentNotNull(message, "message");
IWorkflowProvider workflowProvider = Context.ContentDatabase.WorkflowProvider;
if (workflowProvider == null)
return;
string workflowID = message["wf"];
string stateID = message["ws"];
IWorkflow workflow = workflowProvider.GetWorkflow(workflowID);
if (workflow == null)
return;
WorkflowState state = workflow.GetState(stateID);
DataUri[] items = GetItems(state, workflow);
Assert.IsNotNull(items, "uris is null");
string comments = state != null ? state.DisplayName : string.Empty;
bool flag = false;
foreach (DataUri index in items)
{
Item obj = Context.ContentDatabase.Items[index];
if (obj != null)
{
try
{
workflow.Execute(message["command"], obj, comments, true, new object[0]);
}
catch (WorkflowStateMissingException)
{
flag = true;
}
}
}
if (flag)
SheerResponse.Alert("One or more items could not be processed because their workflow state does not specify the next step.", new string[0]);
UrlString urlString = new UrlString(WebUtil.GetRawUrl());
urlString["reload"] = "1";
Context.ClientPage.ClientResponse.SetLocation(urlString.ToString());
}
private void SendAllNew(Message message)
{
Assert.ArgumentNotNull(message, "message");
IWorkflowProvider workflowProvider = Context.ContentDatabase.WorkflowProvider;
if (workflowProvider == null)
return;
string workflowID = message["wf"];
string stateID = message["ws"];
IWorkflow workflow = workflowProvider.GetWorkflow(workflowID);
if (workflow == null)
return;
if (message["ui"] != "1")
{
if (message["suppresscomment"] != "1")
{
string ids = String.Empty;
WorkflowState state = workflow.GetState(stateID);
DataUri[] items = GetItems(state, workflow);
foreach (DataUri dataUri in items)
{
ids += dataUri.ItemID + "," + dataUri.Language + "," + dataUri.Version.Number + "," + "::";
}
if (String.IsNullOrEmpty(ids))
{
Context.ClientPage.ClientResponse.Alert("There are no selected items.");
return;
}
Context.ClientPage.ServerProperties["ids"] = ids;
Context.ClientPage.ServerProperties["language"] = message["la"];
Context.ClientPage.ServerProperties["version"] = message["vs"];
Context.ClientPage.ServerProperties["command"] = message["command"];
Context.ClientPage.ServerProperties["ws"] = message["ws"];
Context.ClientPage.ServerProperties["workflowid"] = workflowID;
Context.ClientPage.Start(this, "CommentMultiple");
return;
}
}
// no comment - use the old SendAll
SendAll(message);
}
private void SendSelectedNew(Message message)
{
Assert.ArgumentNotNull(message, "message");
IWorkflowProvider workflowProvider = Context.ContentDatabase.WorkflowProvider;
if (workflowProvider == null)
return;
string workflowID = message["wf"];
IWorkflow workflow = workflowProvider.GetWorkflow(workflowID);
if (workflow == null)
return;
if (message["ui"] != "1")
{
if (message["suppresscomment"] != "1")
{
string ids = String.Empty;
foreach (string str2 in Context.ClientPage.ClientRequest.Form.Keys)
{
if (str2 != null && str2.StartsWith("check_", StringComparison.InvariantCulture))
{
ids += Context.ClientPage.ClientRequest.Form["hidden_" + str2.Substring(6)] + "::";
}
}
if (String.IsNullOrEmpty(ids))
{
Context.ClientPage.ClientResponse.Alert("There are no selected items.");
return;
}
Context.ClientPage.ServerProperties["ids"] = ids;
Context.ClientPage.ServerProperties["language"] = message["la"];
Context.ClientPage.ServerProperties["version"] = message["vs"];
Context.ClientPage.ServerProperties["command"] = message["command"];
Context.ClientPage.ServerProperties["ws"] = message["ws"];
Context.ClientPage.ServerProperties["workflowid"] = workflowID;
Context.ClientPage.Start(this, "CommentMultiple");
return;
}
}
// no comment - use the old SendSelected
SendSelected(message);
}
private void SendSelected(Message message, string comment = null)
{
Assert.ArgumentNotNull(message, "message");
IWorkflowProvider workflowProvider = Context.ContentDatabase.WorkflowProvider;
if (workflowProvider == null)
return;
string workflowID = message["wf"];
string str1 = message["ws"];
IWorkflow workflow = workflowProvider.GetWorkflow(workflowID);
if (workflow == null)
return;
int num = 0;
bool flag = false;
foreach (string str2 in Context.ClientPage.ClientRequest.Form.Keys)
{
if (str2 != null && str2.StartsWith("check_", StringComparison.InvariantCulture))
{
string[] strArray = Context.ClientPage.ClientRequest.Form["hidden_" + str2.Substring(6)].Split(new [] { ',' });
Item obj = Context.ContentDatabase.Items[strArray[0], Language.Parse(strArray[1]), Sitecore.Data.Version.Parse(strArray[2])];
if (obj != null)
{
WorkflowState state = workflow.GetState(obj);
if (state.StateID == str1)
{
try
{
workflow.Execute(message["command"], obj, comment ?? state.DisplayName, true, new object[0]);
}
catch (WorkflowStateMissingException)
{
flag = true;
}
++num;
}
}
}
}
if (flag)
SheerResponse.Alert("One or more items could not be processed because their workflow state does not specify the next step.", new string[0]);
if (num == 0)
{
Context.ClientPage.ClientResponse.Alert("There are no selected items.");
}
else
{
UrlString urlString = new UrlString(WebUtil.GetRawUrl());
urlString["reload"] = "1";
Context.ClientPage.ClientResponse.SetLocation(urlString.ToString());
}
}
}
}