I have 2 buttons with the same name. For design reasons only one of them is visible at the same time.
I want to click on any visible button.
If first button is hided this expression
$this->byCssSelector('[name="saveAndClose"]')->click()
returns
Element is not currently visible and so may not be interacted with
How to click on visible button?
I have written simple code to this.
public function clickOnDisplayedElementByName($name)
{
$elements = $this->elements($this->using('css selector')->value('[name="' . $name . '"]'));
foreach ($elements as $element)
{
if ($element->displayed())
{
$element->click();
return;
}
}
$this->fail('There is no visible elements with name ' . $name);
}
Related
I am adding a delete button to certain rows of a table (not all). The table is already inside a form, so my thinking is to do a check to see if my button was clicked inside the function that the form submits to and redirect to a delete function if it was. I can't figure out if it's possible to do that or what other options there are.
I tried using a link for the delete, but that doesn't get the information across. I tried using another form, but that's a form within a form. If there is another way to do this, I'm happy to learn it.
Here is parts of the form:
echo '<form action="#/job/addorupdate" id="jobform" name="jobform" method="post" accept-charset="utf-8">';
echo '<input type="hidden" name="id" value="228828">';
if ($id > 0) {
echo '<button style="color:red;border:none;font-weight:bold;cursor:pointer;" type="submit" value="' . $id . '" name="idOfRow">X</button>';
}
echo '</form>';
Here is the function it goes to:
public function addOrUpdate(){ // addOrUpdate a job - control
if ($this->input->post('idOfRow')) {
// This is where I want to do the redirect
}
.
.
}
The solution was to replace the button with a link and use that to call the delete function.
echo ' X';
public function addOrUpdate(){ // addOrUpdate a job - control
if ($this->input->post('idOfRow')) {
$delete=deleteRow($this->input->post('idOfRow'));
if($delete == 1){
header('location:location.php'); // add redirect location here.
}
}
}
//Create One New function
public function deleteRow($DeleteInput){
if ($DeleteInput) {
//add sql function to delete
if(delete == true){
return 1;
}
else {
return false;
}
}
}
I'm trying to get all link from this page. Actually there I almost achieve this result with this code:
public function getLinks()
{
$html = file_get_html("http://it.soccerway.com/national/italy/serie-a/20152016/regular-season/r31554/");
foreach($html->find("div.block_competition_left_tree-wrapper") as $div)
{
foreach ($div->find('a') as $li)
{
echo $li->href . "<br>";
}
}
}
this is the result:
/national/italy/serie-a/c13/
/national/italy/serie-a/20152016/s11663/
/national/italy/serie-b/c14/
/national/italy/serie-c1/c53/
/national/italy/serie-c2/c358/
/national/italy/serie-d/c659/
/national/italy/coppa-italia/c135/
/national/italy/super-cup/c171/
/national/italy/coppa-italia-serie-c/c684/
/national/italy/campionato-nazionale-primavera/c952/
/national/italy/coppa-italia-primavera/c1070/
/national/italy/super-coppa-primavera/c1171/
/national/italy/dante-berretti/c1092/
/national/italy/serie-a-women/c293/
/national/italy/serie-a2/c457/
/national/italy/coppa-italia-women/c852/
/national/italy/super-cup-women/c851/
/national/italy/club-friendlies/
the problem is that I need to scrape only the link in the list <li>, how you can see in the html there is different classes expanded | odd | even. Essentially I don't want get the link of the element displayed as Serie A - Serie B, etc... but the link inside it. In particular something like this should be the result:
/national/italy/serie-a/20152016/s11663/
/national/italy/serie-b/20152016/regular-season/r31798/
/national/italy/serie-c1/20152016/girone-c/r31861/
now if you see in the first result above there is only /national/italy/serie-a/20152016/s11663/ correct in my final example, this is 'cause in the html page the Serie A item have the class expanded and the code see the link. How can I fix my code to achieve this?
I hope, I have understood you as well. You need to get all links as you did, then open every link to get all links of the class.
An example:
public function getLinks()
{
$html = file_get_html("http://it.soccerway.com/national/italy/serie-a/20152016/regular-season/r31554/");
foreach($html->find("div.block_competition_left_tree-wrapper") as $div)
{
//get all links
foreach ($div->find('a') as $li)
{
$openLink = file_get_html("http://it.soccerway.com/".$li->href);
foreach($openLink->find("div.block_competition_left_tree-wrapper") as $divOfNewLink){
foreach ($divOfNewLink->find('li') as $liOfNewDiv){
if (preg_match("/expanded/i", $liOfNewDiv->class)) {
foreach ($liOfNewDiv->find('a') as $link)
{
echo $link->href . "<br>";
}
}else{
// do nothing
}
}
}
}
}
}
I added custom buttons to the panels ipe toolbar. The toolbar only shows when you have the right permissions, and when the page is a panelized page. I want to display the ipe toolbar also for other pages, containing that page his tabs (view/edit/devel/translate). Is it possible?
yes, this is possible. I had a similar need in one of my projects. What I did in a custom module was:
Use hook_page_alter to add the ipe toolbar when buttons DON'T exist (Panels IPE adds them when they do exist)
Use hook_theme_registry_alter to use my own template function instead of the one provided by Panels IPE.
Create a custom theme function that adds my custom buttons
In code it is something like this:
/**
* Implements of hook_page_alter()
*/
function MYMODULE_page_alter(&$page) {
// Check if Panels IPE is turned on.
if (!module_exists('panels_ipe'))
return;
// Let Panels IPE add the buttons if they exist > If there are no buttons
// then we'll still add the toolbar anyway.
$buttons = &drupal_static('panels_ipe_toolbar_buttons', array());
if (!empty($buttons)) {
return;
}
$output = theme('panels_ipe_toolbar', array('buttons' => $buttons));
$page['page_bottom']['panels_ipe'] = array(
'#markup' => $output,
);
}
/**
* Implements hook_theme_registry_alter().
*/
function MYMODULE_theme_registry_alter(&$theme_registry) {
// Check if Panels IPE is turned on.
if (!module_exists('panels_ipe'))
return;
// Inject our own theme function instead of the one from Panels IPE
$theme_registry['panels_ipe_toolbar']['function'] = 'theme_MYMODULE_panels_ipe_toolbar';
}
// This function is to be adjusted to add buttons and things.
function theme_MYMODULE_panels_ipe_toolbar($vars) {
$buttons = $vars['buttons'];
$output = "<div id='panels-ipe-control-container' class='clearfix'>";
foreach ($buttons as $key => $ipe_buttons) {
$output .= "<div id='panels-ipe-control-$key' class='panels-ipe-control'>";
// Controls in this container will appear when the IPE is not on.
$output .= '<div class="panels-ipe-button-container clearfix">';
foreach ($ipe_buttons as $button) {
$output .= is_string($button) ? $button : drupal_render($button);
}
$output .= '</div>';
// Controls in this container will appear when the IPE is on. It is usually
// filled via AJAX.
$output .= '<div class="panels-ipe-form-container clearfix"></div>';
$output .= '</div>';
}
$output .= "</div>";
return $output;
}
What I'm trying to achieve is using viewHelper to customize my element. I know that it's mostly done by decorators, but I would like to know is it possible to do it with viewHelper only?
Where I want to use viewHelper:
$defaults = $_GET;
$defaults['randomText'] = 'Something';
$defaults['something'] = 'placeholder';
$form = new Extension_Form();
$element = new Extension_Form_Element_Xhtml('randomText', $this->view->SpanAdder($defaults['randomText'])); // I'm creating Xhtml element, where I'm replacing content with viewHelper return. Want to get rid of $this->view->SpanAdder part :)
$element->setLabel('Label');
$form->addElement($element);
unset($defaults['randomText']); // I want to get rid of this line, but unfortunately I have to have it, otherwise SpanAdder result will be overwritten.
$form->setDefaults($defaults);
And I have viewHelper, which gives span around my value.
class View_Helper_SpanAdder extends Zend_View_Helper_Abstract {
public function SpanAdder($value) {
return '<span name="' . $value . '">' . $value . '</span>';
}
}
I have an array of people that is registered as online in a html file. I am using this so that each can have an image assigned to them. But when checking to see if using name is already in use the in_array function return false and allow the script to continue.
$user = "< img src='default.jpg' />John";
$explode = array("<img src='tress.jpg' />John");
if(in_array($user, $explode))
{
//show login script if user exists
}
else
{
//continue to script
}
Now the reason this is not working is because the john in the array is not identical to the john in $user. Is there anyway of checking that the name exists in the array? When responding please explain.
Instead of asking, "How do I solve this problem?", you need to start with, "Why am I having this problem?"
$user = "< img src='default.jpg' />John";
Is < img src='default.jpg' />John a user name? Why are you using it as one? I'm guessing there's some clever thought behind this like "Well, I always display a user's image with their name, so I'll just make the image part of their name. This is going to cause far more problems than it solves. This comes back to a big concept in computer science called separation of concerns. An image is not logically a part of a user name, so don't store it as one. If you always display them together, you can use functions to display a user's information in a standard way without making the image part of the user name.
So first off, remove the image from the name. There are several ways to store this separately.
I would suggest using a class:
class User {
public $name;
public $imageSource;
// The following functions are optional, but show how a class
// can be useful.
/**
* Create a user with the given name and URL to their image
*/
function __construct($name, $imageSource) {
$this->name = $name;
$this->imageSource = $imageSource;
}
/**
* Gets the HTML to display a user's image
*/
function image() {
return "<img src='". $this->imageSource ."' />";
}
/**
* Gets HTML to display to identify a user (including image)
*/
function display() {
return $this->image() . $this->name;
}
}
$user = new User("john", "default.jpg");
// or without the constructor defined
//$user = new User();
//$user->name = "john";
//$user->imageSource = "default.jpg";
echo $user->display();
You can use an "array" if you want to be a little lazier, but I don't recommend it in the general case, since you lose the cool features of classes (like those functions):
$user = array(
name => "john",
image => "<img src='default.jpg' />";
);
echo $user["image"] . $user["name"];
In your database (if you're using one), make them separate columns and then use one of the above data structures.
Now that you have this, it's easy to see if a user name is in a given list using a foreach loop:
function userNameInList($user, $users) {
for($users as $current) {
if($user->name == $current) {
return true;
}
}
return false;
}
$newUser = new User("John", "john.jpg");
$currentUsers = array("John", "Mary", "Bob");
if(userNameInList($newUser, $currentUsers) {
echo "Sorry, user name " . $newUser->name . " is already in use!";
}
If you're new to PHP, the normal for loop may be easier to understand:
function userNameInList($user, $users) {
for($i = 0; $i < count($users); ++i) {
$current = $users[$i];
if($user->name == $current) {
return true;
}
}
return false;
}
Let me know if any of this doesn't run, I don't write PHP very often anymore..