in my ZF2 application I want to encrypt my parameters, so that when having a link like /product/update/1 will be displayed as /product/update/cdsk45kdi340kd0wlw0 or something similar. I need to encrypt and decrypt it in controllers and views. What is the best approach to this? Thank you for any help.
The point here is that you want to avoid users to guess the url. I would generate a random token per product. Store in your database this token together with the id of the product (and all other properties).
To generate a random string as a token, you can use Zend\Math\Rand: Rand::getString(10); gives you a random string of 10 characters. When you store the products in your database, generate a random string for every product. Then, in your controller you do not get the product based on the identifier (id), but based on the token.
make route look like this
'product' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/product/update[/:hashedid]',
'constraints' => array(
'hashedid' => '[a-zA-Z0-9-_\.]+',
),
'defaults' => array(
'controller' => 'Index',
'action' => 'index',
),
),
),
and in controller
$hashedid = $this->params()->fromRoute('hashedid', 0);
$id = $this->dehash($hashedid);
Related
I'm using MB Relationships plugin to create a relationship between two custom post types movies and reviews
Register relationship:
function register_movies_to_reviews_relationship()
{
MB_Relationships_API::register([
'id' => 'movies_to_reviews',
'from' => 'movies',
'to' => 'reviews',
]);
}
add_action('mb_relationships_init', 'register_movies_to_reviews_relationship');
=> It's working good
Fetch the reviews data by movie_id:
$reviews = MB_Relationships_API::get_connected([
'id' => 'movies_to_reviews',
'from' => $movie_id
]);
=> It's working good
But i don't know how i can fetch the movies data by review_id.
How can i do that? Somebody can help me. Thank you!!!
Shouldnt it be just replace from/to with the according id?
$reviews = MB_Relationships_API::get_connected([
'id' => 'movies_to_reviews',
'to' => $review_id
]);
Use case
I am learning Symfony2 and am creating a Table Tennis tracking app to learn the framework. I have configured my entities as follows.
Player 1..n Result n..1 Match
On my form I'd like to validate that the scores for a match are correct.
Implementation
Match has an ArrayCollection() of results.
My MatchType and ResultType forms contain the following.
// Form\MatchType
$builder->add('matchType', 'entity', array(
'class' => 'PingPongMatchesBundle:MatchType',
'property' => 'name',
)
)
->add('results', 'collection', array(
'type' => new ResultType(),
'allow_add' => true,
'by_reference' => false,
)
)
->add('notes');
// Form\ResultType
$builder->add('player', 'entity', array(
'class' => 'PingPongPlayerBundle:Player',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('p')
->orderBy('p.firstName', 'ASC');
},
))
->add('score');
Issue
I need to be able to validate the scores. However I'm not sure how to approach this type of validation as I need to compare two instances of my Result#score in order to know if they are valid or not.
Is anyone able to suggest a method or approach that I could use in order to be able to compare Result#score between the two different instances? Can I validate the ArrayCollection in the Match entity for example?
You could creat a custom validator constraint on Match entity.
http://symfony.com/doc/2.0/cookbook/validation/custom_constraint.html
Take a look in Callback constraint:
http://symfony.com/doc/2.1/reference/constraints/Callback.html
How can I retrieve all users registered in my WordPress blog having a particular meta data?
For example I have made an option to add a custom meta data for every registering users having meta key as parent_id. If I want to list all users having parent_id as 2 , then how can I do this?
Since WP v3.1 it's ridiculously easy to search for a user by his/her meta key.
Use the function
get_users($args)
(WP Documentation)
The function takes an array of parameters, in your case you need
get_users(array(
'meta_key' => 'parent_id',
'meta_value' => '42'
))
Simple way how to get one user by his metadata is:
$user = reset(
get_users(
array(
'meta_key' => $meta_key,
'meta_value' => $meta_value,
'number' => 1
)
)
);
Here is how you can get users based on a custom role and multiple metadata keys,
$available_drivers = get_users(
array(
'role' => 'driver',
'meta_query' => array(
array(
'key' => 'approved',
'value' => true,
'compare' => '=='
),
array(
'key' => 'available',
'value' => true,
'compare' => '=='
)
)
)
);
Explaining the above query, I want only those users who I assigned the role of driver, and they are approved and available. The approved and available are custom fields created using ACF as True/False fields.
If you have additional metadata to test, add another array element to the meta_query array.
Meanwhile checkout my open source at github.com/patrickingle
Here is the codex page from Wordpress detailing how to use the get_users($arg); function.
It contains examples how how to build custom functions to fetch various parts of user data. You'll have to naturally build and make some of your own changes to get it how you want.
Additionally here is a link to a function somebody built that will fetch user data based on roles within wordpress. You can configure it in many different ways with some tweeking, but this will allow you to filter your results in a more powerful manner.
In my custom module, I've got a table like this:
aid | int(10) unsigned
message | mediumtext
variables | mediumtext
This is similar to the schema for watchdog.
I want expose the message field to views (hook_views_data) but through a handler that translates it with the variables field. Something like this:
t($message, unserialize($variables))
Anyone know how to combine two fields and use a field handler to do this?
Here is my hook_views_data
/**
* Implementation of hook_views_data().
*/
function mymodule_views_data() {
$data['gccsi_activity']['aid'] = array(
'title' => t('Unique ID'),
'help' => t('The unique id'),
'sort' => array(
'handler' => 'views_handler_sort',
),
);
$data['gccsi_activity']['message'] = array(
'title' => t('Message'),
'help' => t('The message...'),
'sort' => array(
'handler' => 'views_handler_sort',
)
//here is where I want to create a handler that combines two fields
);
return $data;
}
Thanks
If you have implemented hook_views_data you can set the used handler.
$data['table']['column']['id']['field'] = array(
'handler' => 'yourmodule_handler_field_column',
);
Then you implement hook_views_handlers to register the used handler.
Then you write your handler and do the following steps. Let's assume you have one for message
a) in method construct you do
$this->additional_fields['variables'] = 'variables';
b) in method render do your previous stuff
t($values->{$this->field_alias}, unserialize($values->{$this->aliases['variables']}));
The views advanced help part is a good place to look up some general informations about viewsapi
as far as I know the handler class has to live in it's own file.
1) Where is the best place to populate a new database table when a module is first installed, enabled? I need to go and get some data from an external source and want to do it transparently when the user installs/enables my custom module.
I create the schema in {mymodule}_schema(), do drupal_install_schema({tablename}); in hook_install. Then I try to populate the table in hook_enable using drupal_write_record.
I confirmed the table was created, I get no errors when hook_enable executes, but when I query the new table, I get no rows back--it's empty.
Here's one variation of the code I've tried:
/**
* Implementation of hook_schema()
*/
function ncbi_subsites_schema() {
// we know it's MYSQL, so no need to check
$schema['ncbi_subsites_sites'] = array(
'description' => 'The base table for subsites',
'fields' => array(
'site_id' => array(
'description' => 'Primary id for site',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
), // end site_id
'title' => array(
'description' => 'The title of the subsite',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
), //end title field
'url' => array(
'description' => 'The URL of the subsite in Production',
'type' => 'varchar',
'length' => 255,
'default' => '',
), //end url field
), //end fields
'unique keys' => array(
'site_id'=> array('site_id'),
'title' => array('title'),
), //end unique keys
'primary_key' => array('site_id'),
); // end schema
return $schema;
}
Here's hook_install:
function ncbi_subsites_install() {
drupal_install_schema('ncbi_subsites');
}
Here's hook_enable:
function ncbi_subsites_enable() {
drupal_get_schema('ncbi_subsites_site');
// my helper function to get data for table (not shown)
$subsites = ncbi_subsites_get_subsites();
foreach( $subsites as $name=>$attrs ) {
$record = new stdClass();
$record->title = $name;
$record->url = $attrs['homepage'];
drupal_write_record( 'ncbi_subsites_sites', $record );
}
}
Can someone tell me what I'm missing?
If ncbi_subsites_get_subsites() is not in the .install file, you need to include whatever file its in with your module. Otherwise, it's returning nothing, in which case try dumping $subsites and exiting.
I think the answer is that drupal_write_record is not meant for install or enable hooks. I think when enabling or installing, you have to write SQL. That is the impression I am getting from reading some posts that mention that the schema is not available in these hooks.
First of all (assuming Drupal 6), drupal_write_record() cannot be called from hook_install() because Drupal would not find the database schema defined from the module, which is still going to be installed, and enabled.
Instead you need to use db_query() function. (the comments are speaking of a way to include default data by prviding it to hook_schema() serialized, but i've found no documentation on this.)
However, would you be using (the development version of) Drupal 7, you want to look at the db_insert() function instead.