I am trying to add a "validation" stage in Jenkinsfile based on the day of the week. If today is Sunday, validation is required, otherwise not.
the if statement is not working
here I am declaring the variable
DAY=sh(returnStdout: true, script: 'date +"%a"').trim()
and here is the stage
stage('validation') {
steps {
script {
if ( DAY == "SUN" ) {
echo "Validation is required, today is $DAY"
}
else {
echo "No validation required, today is $DAY"
}
}
}
}
and here is the output
No validation required, today is Sun
the value of the variable Day is correct, but the if statement doesnt work correctly
thanks in advance
It looks like the comparison is failing because the case of the word in DAY is different.
Try this
if ( DAY == "Sun" ) {
echo "Validation is required, today is $DAY"
}
else {
echo "No validation required, today is $DAY"
}
Another approach, to be sure of what you are comparing, is at least to transform the result in upercase. That way, your test will work unchanged.
And be sure to force an English output for date +"%a"
(on my French setup, I get "dim.", not "Sun")
DAY=sh(returnStdout: true, script: 'LANG=en_us_88591 date +"%a"').trim().toUpperCase().replaceAll('.','')
That way, your Jenkinsfile will work on any workstation, no matter its locale.
Related
I'm trying to create a function, it exists and does not exist in the search feature. Condition does not exist, will display Error 404. Condition, exists. Then it will display from search. But in the code I wrote, it only shows Error 404.
This is my model:
function search_album($keyword){
$result=$this->db->query("SELECT tbl_album.*,DATE_FORMAT(album_date,'%d %M %Y') AS date FROM tbl_album WHERE album_title LIKE '%$keyword%'");
return $result;
}
This is my controller:
function search(){
$keyword=str_replace("'", "", $this->input->post('xfilter',TRUE));
$x['data']=$this->model_album->search_album($keyword);
if(empty($x) && empty($keyword)){
$this->load->view('view_contents',$x);
}
else if (!empty($x) && !empty($keyword)){
$this->load->view('view_error_404');
}
}
I've tried from this source, but it doesn't work. Can you help me?
There are several errors in your code:
ONE: You have interchanged your if statements. Like, if the search did not return any data and the keyword was not supplied, it is supposed to display the "view_error_404", otherwise, it is supposed to load data into the view "view_contents".You did the vice versa I have corrected that for you in the code below.
TWO: You are checking if $x is empty which will never be empty as you have initialized $x['data']. Note that the model can return empty data, such that $x['data'] is empty. Instead, check if the search result is empty by replacing empty($x) with empty($x['data'])
THREE: In your model, you are returning the query builder class, but not the actual data from the query statement. instead, replace return $result; with return $result->result();
FOUR:
From your if statements, you need to add an else statement so that if the 2 conditions are never met, it can execute. With your current implementation, there is a state which will not meet first or second conditions and will lead to a blank screen.
if(empty($x['data']) && empty($keyword)){
$this->load->view('view_error_404');
}else if (!empty($x['data']) && !empty($keyword)){
$this->load->view('view_contents',$x);
}else{
$this->load->view('view_error_404'); // replace this code with your preference
}
you need to mention search function in routes.php file like this:-
$routes['search'] ='your_controller_name/search';
without knowing your function model_album->search_album($keyword) and assuming it returns a string or array, you don't need to worry about checking for the keyword, since without a keyword the function should return false or empty.
anyway you mixed up your if/else conditions, since you are checking for not empty results to return a 404, instead of empty result returning the 404.
this should do it:
if(empty($x)){
$this->load->view('view_error_404');
}
else {
$this->load->view('view_contents',$x);
}
I am validating a form with Codeigniter 4. On submission, the first thing I check is if there is anything to update. If there isn't anything to update, warning message stating there is nothing to update appears. The problem is, I have not changed anything but hasChanged returns TRUE stating it has indeed changed. Is there any easy way to echo out what has changed? Here is my code;
public function post_update($post_id)
{
$original_post = $this->model->find($post_id);
if ($this->request->getMethod() === 'post') {
// get the form data
$update_post = $this->request->getPost();
$original_post->fill($update_post);
if (!$original_post->hasChanged()){
return redirect()->back()
->with('warning', 'Nothing to Update')
->withInput();
} else {
$this->model->save($update_post)
}
} // end method
I have echo'd out $original_post before the fill and after the fill. The fields sent are different but what is being sent has not changed, as far as I can see. WOuld like a to know what hasChanged() seems as being changed.
Also, I added the below just before if (!$original_post->hasChanged() to see what has changed:
echo 'post_id'.$original_post->hasChanged('post_id');echo '</br>';
echo 'post_category_id'.$original_post->hasChanged('post_category_id');echo '</br>';
echo 'post_user_id'.$original_post->hasChanged('post_user_id');echo '</br>';
echo 'post_title'.$original_post->hasChanged('post_title');echo '</br>';
echo 'post_slug'.$original_post->hasChanged('post_slug');echo '</br>';
echo 'post_body'.$original_post->hasChanged('post_body');echo '</br>';
echo 'post_is_publish'.$original_post->hasChanged('post_is_publish');echo '</br>';
echo 'post_image'.$original_post->hasChanged('post_image');echo '</br>';
echo 'post_created_at'.$original_post->hasChanged('post_created_at');echo '</br>';
echo 'post_updated_at'.$original_post->hasChanged('post_updated_at');echo '</br>';
echo $original_post->hasChanged();
In the above, it returns empty (meaning false meaning no change) for everything except echo $original_post->hasChanged(); which comes back as 1 meaning it has changed. How can I find out what changed??? There are no more fields in my table.
How can I find out what changed???
The Entity class provides you with a pair of methods called toArray and toRawArray:
public function toArray(bool $onlyChanged = false, bool $cast = true, bool $recursive = false): array
public function toRawArray(bool $onlyChanged = false, bool $recursive = false): array
You can use the first boolean parameter to get only what the Entity thinks has changed. If you're performing any implicit casts using Entity magic, you can use the raw version to bypass them.
I will say that hasChanged uses strict comparison, which catches a lot of people (myself included) off-guard the first time they use it; you need to be careful that you're not changing the types of data either (e.g. the integer 1 to the string '1') because hasChanged will catch that.
Right now i am trying to build a script that when run will get the date and store it as a variable so down the line it can be compared with another date value to see if they match
My question is how do I do that and in what forms can you call the date command. This is a script in bash fyi, this is what I am currently trying to do
#Specific Set Variables
SpecficDate=timestamp() {
date +"%T %D"
}
SpecficStatus="Pass" #SetVariable
echo $SpecficStatus
echo $SpecficDate
Any help would be great, thank you
Using your code something like this is what you could use
SpecficStatus="Pass" #SetVariable
timestamp() { date +"%T %D"; }
SpecficDate=$(timestamp)
echo $SpecficStatus
echo $SpecficDate
Using MS Word 2010 I am trying to place an INCLUDEPICTURE field into a block of an IF statement. While both the IF statement and the INCLUDEPICTURE work correctly separate, they do not work in combination.
IF Statement:
{ IF { MERGEFIELD condition \* MERGEFORMAT } = "expression" "true" "false" \* MERGEFORMAT }
This works correctly.
INCLUDEPICTURE:
{ INCLUDEPICTURE "picture.png" }
This works correctly, too.
Combination of the two:
{ IF { MERGEFIELD condition \* MERGEFORMAT } = "expression" "{ INCLUDEPICTURE "picture.png" }" "false" \* MERGEFORMAT }
This does not work. If the IF expression is true, nothing is displayed at all.
How can I combine both the IF statement and the INCLUDEPICTURE command?
This is a well known-problem (i.e. you are right, it doesn't work).
Unfortunately, there isn't a particularly good solution - the simplest involves using a blank 1-pixel image file.
The usual starting point is to invert the nesting so that you have something more like this...
{ INCLUDEPICTURE "{ IF "{ MERGEFIELD condition }" = "expression" "picture.png" }" }" \d }
This always tries to insert a picture, and will report (and insert) an error in the case where { MERGEFIELD condition } <> "expression". The simplest resolution is to have a blank 1-pixel picture that you can include instead, e.g.
{ INCLUDEPICTURE "{ IF "{ MERGEFIELD condition }" = "expression" "picture.png" "blank1.png" }" }" \d }
It is sometimes clearer to remove the test and assignment and do it separately, particularly if there are multiple tests. In this case,
{ SET picname "{ IF "{ MERGEFIELD condition }" = "expression" "picture.png" "blank1.png" }" }
or if you prefer,
{ IF "{ MERGEFIELD condition }" = "expression" "{ SET picname "picture.png" }" "{ SET picname "blank1.png" }" }
You still need an IF nested inside the INNCLUDEPICTURE to make it work. You can use:
{ INCLUDEPICTURE "{ IF TRUE { picname } }" \d }
If you merge those nested fields to an output document, the fields will remain in the output. If you want the fields to be resolved (e.g. because you need to send the output to someone who does not have the image files) then you need something more like this:
{ IF { INCLUDEPICTURE "{ IF TRUE { picname } }" } { INCLUDEPICTURE "{ IF TRUE { picname } }" \d } }
I believe you can reduce this to
{ IF { INCLUDEPICTURE "{ picname }" } { INCLUDEPICTURE "{ IF TRUE { picname } }" \d } }
In fact, I believe you can insert the full path+name of any graphic file that you know exists instead of the first { picname }, e.g.
{ IF { INCLUDEPICTURE "the full pathname of blank1.png" } { INCLUDEPICTURE "{ IF TRUE { picname } }" \d } }
But you should check that those work for you.
EDIT
FWIW, some recent tests suggest that whereas the pictures appear unlinked, a save/re-open displays a reconstituted link (with a *MERGEFORMATINET near the end), and the pictures are expected to be at the locaitons indicated in those links. Whether this is due to a change in Word I cannot tell. If anything has changed, it looks to be an attempt to allow some relative path addressing in the Relationship records that Word creates inside the .docx.
Some observations...
Make sure paths have doubled-up backslashes, e.g.
c:\\mypath\\blank1.png . This is usually necessary for any paths
hard-coded into fields. For paths that come in via nested field
codes, please check.
As a general point, it is easier to work with INCLUDEPICTURE fields
when the document is a .doc, not .docx, and to ensure that
File->Options->Advanced->General->Web options->Files->"Update links
on save" is checked. Otherwise, Word is more likely to replace
INCLUDEPICTURE fields with a result that cannot be redisplayed as a
field using Alt-F9
When you want to treat the comparands in an IF field as strings, it
is advisable to surround them with double-quotes, as I have done.
Otherwise, a { MERGEFIELD } field that resolves to the name of a
bookmark may not behave as you would hope. Otherwise, spacing and
quoting is largely a matter of personal choice.
So far, none of these field constructions will deal with the situation where you have path names for pictures that may or may not exist. If that is what you need, please modify your original question.
Step by step guide:
bibadia's answer works, but word does not tell you when you make mistakes, so it is very hard to get it right. So I hope this step by step answer helps.
Step 1: Add a Picture
In Word 2013 docx (no idea about other versions) add
{ INCLUDEPICTURE "C:\\picture.png" }
Note: Use CTRL+F9 to add { } , don't ever type them in, as they will not work.
Use \\ and not \
Run the mail merge, do Ctrl+A then F9 to show the picture.
Step 2: Auto Show it
To change the mail merge document use (CTRL+A Shift+F9). Change it to
{ SET picname "C:\\picture.png" }
{ INCLUDEPICTURE "{ IF TRUE { picname } }" \d }
Run the mail merge - the picture should show up, no need for Ctrl+A then F9
Step 3: Unlink it
Remove the \d
This will let you email the doc. As the \d causes the document to create a link to the image file, rather than include it.
Step 4: add an IF
Use bibadia's solution, i.e.
{ SET picname "{ IF "{ MERGEFIELD condition }" = "expression" "picture.png" "blank1.png" }" }
Another option that I've tested works is to use an If statement to check an expression (In my example check if the entry is not null), and if not then display the image, if not display some custom text (If you don't want text just have empty quotation marks i.e. ""):
{IF {MERGEFIELD my_photo_variable_name} <> "" {INCLUDEPICTURE "{IF TRUE {MERGEFIELD my_photo_variable_name}}" \d} "Text to display if no picture available"}
Which translates as:
If there is no value for the image my_photo_variable_name, include the image in the mail merge.
If there is no value i.e no image, then display custom text Text to display if no picture available.
I am calling $("#from_date").datetimepicker("getDate") and no matter what the hours/minutes scrollers are set to, the returned value has its time part set to 0.
What am I doing wrong / missing ?
I had the same problem. I modified the script like this:
//if (date && tp_inst._parseTime($(target).val(), tp_inst.timeOnly))
try
{
date.setHours(tp_inst.hour, tp_inst.minute, tp_inst.second, tp_inst.millisec);
}
catch (err)
{
window.console && console.log(err)
}
I didn't investigate in depth but I think the problem is, that I use an altField and the parsed $(target).val() is the date instead of the time. I also tried with the time but the parser didn't handle it. So I just add the values to the date and this works for me.