How to print username as path in Drupal 7 - drupal-7

I'm trying to give every registered user a unique url shown on their user page which they can copy and paste...promoting their page.
I had this working using Pages and Tokens...but now I've ditched Panels/Pages for a custom user-profile.tpl.php
I've tried this:
print ($user->name);
But this returns the user name as Hillay Swag instead of hillary-swag
How can I print the url version of the user name instead of the human version?

$link = drupal_get_path_alias('user/' . $user->uid);
print $link;
That will print out users/admin. Further, if you only want the username part of the path (no users/) you could cut it with PHP's substr() function.

Related

When using router.query, it removes some characters, but in the url it is still there

This is how I use router.query to get hold of the token that I need so that the user can update one's password. But when I e.g. print it to my hidden, it removes e.g. + and / etc.
I have tried to use encodeURIComponent to be able to solve the problem but it seems that it does not help in any way.
Token in url: CfDJ8Pf5H7I0LCJKuxB2jm5JU0EGFK7KC45kBnwAkPgbAO2+kPijFxxb5CW6wyE/ft74if6V5ouwrHE8wK8Vz2ZTlc0s82XwwC9rZD4CvA5UQnv9eyL0UxdCqNEVjlusntVTn4d+41nLwlADsSqhLYajkRHwSHx8DhvJZa9OBGX9iYpR2EXnBOMa1EZcvvKDhEX+9+pgOtW8shPAo4p+F5nG0C+qnK4s5u/rO5vgA7SFhEkWS
When I get it into the content of the page, it looks like this. After it is displayed on the page.
CfDJ8Pf5H7I0LCJKuxB2jm5JU0EGFK7KC45kBnwAkPgbAO2 kPijFxxb5CW6wyE/ft74if6V5ouwrHE8wK8Vz2ZTlc0s82XwwC9rZD4CvA5UQnv9eyL0UxdCqNEVjlusntVTn4d 41nLwlADsSqhLYajkRHwSHx8DhvJZa9OBGX9iYpR2EXnBOMa1EZcvvKDhEX 9 pgOtW8shPAo4p F5nG0C qnK4s5u/rO5vgA7SFhEkWS
I have tried to do this:
encodeURIComponent(String(router.query["token"]))
And i have try
router.query["token"]
How can it be that you change the sign from + to between spaces or something completely different.
You can decode the encodded string before printing on the page.
const encoddedUri = encodeURI("https://example.com/asdasd asdasd");
console.log(`encoded URI: ${encoddedUri}`);
console.log(`Decoded URI: ${decodeURI(encoddedUri)}`);

Get Number from Slash Command and put it on a link

So, if a user says /GetProfile (EnterNumberHere)
The Number from that command should go at the end of a link
for example, i said /GetProfile 1 the number "1" should go at the end of this link
"http://cubestoria.ezyro.com/User/?id="
So, then the bot will respond with http://cubestoria.ezyro.com/User/?id=1
https://i.stack.imgur.com/dYCU0.png
I want to look like this but their is a option (like the diet in the photo) that you can put a number in.
Use ${variable} to put variable in string:
// ...
.addNumberOption(option => option.setName('diet').setDescription('...'))
const number = interaction.options.getNumber('diet')
interaction.reply(`http://cubestoria.ezyro.com/User/?id=${number}`)

Yii2 - mailer - send message to email rows in database

Greetings,
i need to send email to several recipients that are stored in a table named mail which has a field called email.
In my controller i created an action that Query the table mail for the emails.
Later i tried to use the implode() function separated by comma, but obviously it didn't work because of mailer policies.
It generated the wrong format -> "email1#mail.com, email2#mail.com, email3#mail.com".
Tried also a for each loop and the serialize() function without success.
The json_encode() function is close to what i need, separate an array of emails to something like -> "email1#mail.com", "email2#mail.com", "email3#mail.com".
But it appends the field name before the value and it is not accepted by mailer policies.
So far i'm stuck with the following code:
public function actionSucesso()
{
$query = new Query;
$query->select('email')
->from('mail');
$command = $query->createCommand();
$enderecos = $command->queryAll();
$enviar = json_encode($enderecos);
Yii::$app->mailer->compose()
->setFrom('atf#website.com')
->setTo($enviar)
->setSubject('Oferta de jogo no site da ATF.')
->setTextBody('Aceda em: http://atf.besaba.com/index.php?r=playschedule%2Findex2')
->send();
return $this->render('sucesso');
}
I think in order for the mailer to work and send the message the correct format needs to be: ->setTo("mail1#mail.com", "mail2#mail.com", "mail3#mail.com")
Is there a way of solving this problem?
Many thanks in advance.
To get array of email with numeric indexes call queryAll() method with $fetchMode parameter \PDO::FETCH_COLUMN, and then just pass returned array to mailer's setTo() method. Like this
$enderecos = $command->queryAll(\PDO::FETCH_COLUMN);
//$enviar = json_encode($enderecos); <- this line no needed
Yii::$app->mailer->compose()
->setFrom('atf#website.com')
->setTo($enderecos) //you pass an array of email addresses
->setSubject('Oferta de jogo no site da ATF.')
->setTextBody('Aceda em: http://atf.besaba.com/index.php?r=playschedule%2Findex2')
->send();
See queryAll() documentation and list of pdo constant including available fetch modes starting with PDO::FETCH_. Also assuming you are using yii2 default mailer, look for swiftmailer documentation about how to set recipients

Cakephp ghost file

I am facing a curious situation. I am using CakePHP 2.0 (locally), XAMPP and I wanted to add a simple hit counter in my homepage so I added the following code (very very simple)
<?php
$filename = 'hitcount.txt';
$handle = fopen($filename, 'r');
$hits = trim(fgets($handle)) + 1;
fclose($handle);
$handle = fopen($filename, 'w');
fwrite($handle, $hits);
fclose($handle);
echo $hits;
There is a text file named hitcount.txt which contains the number of hits (everytime I visit the page it should increase the number of hits). It works. The problem appeared when I tried to access the hitcount.txt file. It was empty but the echo of $hits returned the exact result! I deleted the file and it still shows me the expected result! I used a different browser, the same. I deleted CakePHP's cache, no change. I used the same piece of code in another page and it did not complain with some error, returning the expected result.
How is it possible for Cakephp to "see" a file that does not exist? Has it anything to do with Apache?
You probably view the file at the wrong location as CakePHP's. My guess is CakePHP's referring to the file at app/webroot/hitcount.txt.
You might want to define a full path for hitcount.txt so you can be sure that you and CakePHP are both referring to the same location.
<?php
$filename = TMP.'hitcount.txt';
This would locate the file at `app/tmp/hitcount.txt'.

Drupal 7 Views Contextual Filters Title Override

I have a view that returns search results via the search API. It performs this use case adequately and I am happy. To crown the deliverable, I need to add a title override of the form Showing search results for '%1' which looks easy enough initially but it isn't working entirely as planned.
For a URL = mysite.com/search/all?search=wombat, where the search value is gathered from an exposed form within a block, I am either getting:
Showing search results for 'Search for "all"'
or, if I enter %1 in the title override for subject not appearing in the URL, I get:
Showing search results for %1". My goal is to get "Showing search results for 'wombat'
The title override works in that it removes the Search for ... part but the substitution picks up on "all" as the exception value (or anything else that I set as the exception value) where I need to be able to pick up the value of the query string (search=wombat).
Can anyone shed some light here?
The problem is that the '%1' and '%2' that you can use to override the title refer to your path's first and second arguments (in Drupal terms) and that would be 'search' and 'all?search=wombat' in your case...
What you need instead is the 'wombat' as a path component in itself.
Perhaps you can achieve that by working that case you're talking about: the case of a "title override for subject not appearing in the URL". There is an option in the contextual filters section (I'm assuming that's where you're working) for providing a default value when one isn't present. Perhaps you can use the 'PHP code' option there, isolate your 'wombat' string and return that as a default contextual filter, and then you can get to it via the '%1'.
The php code to get that portion of the URL should look something like this:
return htmlentities($_GET['search']);
the $_GET() returns the value of that variable in the url, and the htmlentities() is just to keep it safe, since it's using a portion of the url, which is vulnerable to XSS.
See if that combo (1) setting a default argument when one isn't present and 2) using that newly set argument in your title printout) works!
I fixed this issue.
Using following two hooks we can change the defalut value of filter Programmatically.
<?php
/**
* hook_views_pre_view
* #param type $view
* #param type $display_id
* #param type $args
*/
function MODULE_NAME_views_pre_view(&$view, &$display_id, &$args) {
if ($view->name == 'VIEW_NAME') {
$filters = $view->display_handler->get_option('filters');
$view->display_handler->override_option('filters', $filters);
}
}
/**
* hook__views_pre_build
* #param type $view
* #return type
*/
function MODULE_NAME_views_pre_build($view) {
if ($view->name=='VIEW_NAME') {
$view->display['page']->handler->handlers['filter']['filter_field']->value['value'] = 8;
return $view;
}
}
?>
This code worked for me. I am using the drupal 7.

Resources