dynamic view by date arguments in drupal 7 - drupal-7

I'm new to drupal, currently im stuck with this problem.
I want to create a article view ("tip of the day") wherein the content of the will automatically changed accordingly. The default value would be the current date.
For example:
http://localhost/test
this will get the current date as the default filter. If no items found it will give no results found.
when i go to http://localhost/test/20111220 the view will automatically get the value of the date parameter in the url and output the content on that date.
How can I achieved this?
Any thoughts or ideas there?
Thank you.

If you are using Views then try the following.
Add Contextual filters in Advanced fieldset and select Date: Date (node) filter.
In it's settings in When the filter value is NOT in the URL fieldset tick Provide default value and leave Current date
Add multiple value identifier set to No
Dates to compare set to Only this field
Between Date field(s) checkboxes select Content: Post date
Method set to OR
After that, by accessing your /test page you would have the last added content and by accessing /test/%date% you would have content for that date. For example /test/2011-12-23
%date% should be as ISO date/period format (i.e. YYYY, YYYY-MM, YYYY-MM-DD, YYYY-W99, YYYY-MM-DD--P3M, P90D, etc).
EDITED: 19-01-2012
Follow this path http://yoursite/admin/structure/views/import in your site and put the following data to code textfield:
$view = new view;
$view->name = 'test';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'test';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'test';
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['query']['options']['query_comment'] = FALSE;
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'some';
$handler->display->display_options['pager']['options']['items_per_page'] = '10';
$handler->display->display_options['style_plugin'] = 'list';
$handler->display->display_options['row_plugin'] = 'fields';
$handler->display->display_options['row_options']['hide_empty'] = 1;
$handler->display->display_options['row_options']['default_field_elements'] = 0;
/* No results behavior: Global: Text area */
$handler->display->display_options['empty']['area']['id'] = 'area';
$handler->display->display_options['empty']['area']['table'] = 'views';
$handler->display->display_options['empty']['area']['field'] = 'area';
$handler->display->display_options['empty']['area']['label'] = 'No results';
$handler->display->display_options['empty']['area']['empty'] = FALSE;
$handler->display->display_options['empty']['area']['content'] = 'No articles found.';
$handler->display->display_options['empty']['area']['format'] = 'full_html';
$handler->display->display_options['empty']['area']['tokenize'] = 0;
/* Field: Content: Title */
$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'node';
$handler->display->display_options['fields']['title']['field'] = 'title';
$handler->display->display_options['fields']['title']['label'] = '';
$handler->display->display_options['fields']['title']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['title']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['title']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['title']['alter']['word_boundary'] = 0;
$handler->display->display_options['fields']['title']['alter']['ellipsis'] = 0;
$handler->display->display_options['fields']['title']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['title']['alter']['trim'] = 0;
$handler->display->display_options['fields']['title']['alter']['html'] = 0;
$handler->display->display_options['fields']['title']['hide_empty'] = 0;
$handler->display->display_options['fields']['title']['empty_zero'] = 0;
$handler->display->display_options['fields']['title']['link_to_node'] = 1;
/* Sort criterion: Content: Post date */
$handler->display->display_options['sorts']['created']['id'] = 'created';
$handler->display->display_options['sorts']['created']['table'] = 'node';
$handler->display->display_options['sorts']['created']['field'] = 'created';
$handler->display->display_options['sorts']['created']['order'] = 'DESC';
/* Contextual filter: Date: Date (node) */
$handler->display->display_options['arguments']['date_argument']['id'] = 'date_argument';
$handler->display->display_options['arguments']['date_argument']['table'] = 'node';
$handler->display->display_options['arguments']['date_argument']['field'] = 'date_argument';
$handler->display->display_options['arguments']['date_argument']['default_action'] = 'default';
$handler->display->display_options['arguments']['date_argument']['default_argument_skip_url'] = 0;
$handler->display->display_options['arguments']['date_argument']['summary']['format'] = 'default_summary';
$handler->display->display_options['arguments']['date_argument']['use_fromto'] = 'no';
$handler->display->display_options['arguments']['date_argument']['date_fields'] = array(
'node.created' => 'node.created',
);
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 0;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Content: Type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'node';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
'page' => 'page',
);
/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'test';
Now by following to /test page you'll have date as today's date. You can also use your date type as 20120119.

Related

Relate a Docusign Envelope to a Salesforce Custom object

I have a Template created in DS which contains custom tags which are mapped to Salesforce fields. The template works well when used via the JS button example code provided by DS and the fields appear as expected.
I am now trying to automate the process using the Docusign SOAPAPI. When creating the envelope the custom fields aren't populated; even the signer fields.
Below is my code:-
DocusignAPI.ArrayOfRecipient1 recipients = new DocusignAPI.ArrayOfRecipient1();
recipients.Recipient = new list<DocusignAPI.Recipient>();
DocusignAPI.Recipient recipient = new DocusignAPI.Recipient();
recipient.Email = signer_email;
recipient.UserName = signer_name;
recipient.ID = 1;
recipient.Type_x = 'Signer';
recipient.RoutingOrder = 1;
recipients.Recipient.add(recipient);
DocusignAPI.ArrayOfTemplateReference templateReferences = new DocusignAPI.ArrayOfTemplateReference();
templateReferences.TemplateReference = new list<DocusignAPI.TemplateReference>();
DocusignAPI.TemplateReference templateReference = new DocusignAPI.TemplateReference();
TemplateReference.Template = '6bc2930f-6d46-4804-a9fc-69d1cf3ebe09';
templateReference.TemplateLocation = 'Server';
templateReferences.TemplateReference.add(templateReference);
DocusignAPI.EnvelopeInformation ei = new DocusignAPI.EnvelopeInformation();
ei.AccountId = account_id;
ei.Subject = 'Lorem Ipsum';
ei.EmailBlurb = 'More text...';
// Create an envelope and fill it in
DocusignAPI.CustomField field = new DocusignAPI.CustomField ();
field.Name = 'DSFSSourceObjectId';
field.Value = 'a1qW0000000vMCj';
field.Show = 'false';
field.CustomFieldType = 'Text';
DocusignAPI.ArrayOfCustomField arrayOfCustomFields = new DocusignAPI.ArrayOfCustomField();
arrayOfCustomFields.CustomField = new list<DocusignAPI.CustomField>();
arrayOfCustomFields.CustomField.add(field);
ei.CustomFields = arrayOfCustomFields;
try {
DocusignAPI.EnvelopeStatus result = api_sender.CreateEnvelopeFromTemplates(templateReferences, recipients, ei, true);
envelope_id = result.EnvelopeID;
System.debug('Returned successfully, envelope_id = ' + envelope_id );
} catch ( CalloutException e) {
System.debug('Exception - ' + e );
error_code = 'Problem: ' + e;
error_message = error_code;
}
All customtags are related to my custom object with id defined in the CustomField above.
Any help gratefully appreciated.
Turns out I need to add in the object API name to the Id. Solution is:-
DocusignAPI.CustomField field = new DocusignAPI.CustomField ();
field.Name = 'DSFSSourceObjectId';
field.Value = 'a1qW0000000vMCj~Property__c';
field.Show = 'false';
field.CustomFieldType = 'Text';

Codeigniter 3 Transaction

In my code the transaction is not rolling even when i have database error.... here is the code...
Even when insert in trial or register fails the login user inserts the data...
$this->db->trans_start();
$logindata = $this->_get_login_data();
$logindata['is_deleted'] = 0;
$this->db->insert('user_login',$logindata);
//insert login data end
//insert register data begin
$registerdata = $this->_get_register_data();
$registerdata['is_active'] = 1;
$registerdata['user_login_id'] = $this->db->insert_id();
$registerdata['subscription_id'] = null;
$registerdata['login_other_accounts_id'] = null;
$registerdata['is_first_time_login'] = 1;
$registerdata['default_language_id'] = 1;
$registerdata['created_on'] = date('Y-m-d H:i:s');
$registerdata['is_deleted'] = 0;
//insert trial period begin
$trialdata['trial_period_day'] = 10;
$trialdata['is_publish'] = 0;
$trialdata['is_deleted'] = null;
$this->trial_period_model->insert('trial_period',$trialdata);
//insert trial period end
if($registerdata['is_company']==null)
$registerdata['is_company'] = 0;
$registerdata['trial_period_id'] = $this->user_model->get_last_inserted_id();
$this->upload_image();
$imagename = $this->upload->data();
$registerdata['photo_logo'] = $imagename['file_name'];
$this->user_model->insert('general_reg_info',$registerdata);
if ($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
echo "failed";
}
else
{
$this->db->trans_commit();
echo "success";
}
//insert register data end
Your database type might be 'MyISAM' change the database type to 'InnoDB'

Add database in codeigniter dynamically

I am working on codeigniter app to allow user to add edit and modify database.
I want to repeat this code by loop:
$db['db1']['hostname'] = 'localhost';
$db['db1']['username'] = 'ts4l_wp13';
$db['db1']['password'] = 'O&3D6c(0zD70.^9';
$db['db1']['database'] = 'ts4l_wp13';
$db['db1']['dbdriver'] = 'mysql';
$db['db1']['dbprefix'] = '';
$db['db1']['pconnect'] = FALSE;
$db['db1']['db_debug'] = TRUE;
$db['db1']['cache_on'] = FALSE;
$db['db1']['cachedir'] = '';
$db['db1']['char_set'] = 'utf8';
$db['db1']['dbcollat'] = 'utf8_general_ci';
$db['db1']['swap_pre'] = '';
$db['db1']['autoinit'] = TRUE;
$db['db1']['stricton'] = FALSE;
I do this:
#$get_data = mysql_query("SELECT * FROM `database_name`");
while($getdata = mysql_fetch_assoc($get_data)){
$id='db'.$getdata['id'];
$iid="$id";
$db["$iid"]['hostname'] = 'localhost';
$db["$iid"]['username'] = $getdata['username'];
$db["$iid"]['password'] = $getdata['password'];
$db["$iid"]['database'] = $getdata['name'];
$db["$iid"]['dbdriver'] = 'mysql';
$db["$iid"]['dbprefix'] = '';
$db["$iid"]['pconnect'] = FALSE;
$db["$iid"]['db_debug'] = TRUE;
$db["$iid"]['cache_on'] = FALSE;
$db["$iid"]['cachedir'] = '';
$db["$iid"]['char_set'] = 'utf8';
$db["$iid"]['dbcollat'] = 'utf8_general_ci';
$db["$iid"]['swap_pre'] = '';
$db["$iid"]['autoinit'] = TRUE;
$db["$iid"]['stricton'] = FALSE;
}
and this block.
// Loading db and running query.
$CI = &get_instance();
$this->db1 = $CI->load->database('db1', TRUE);
$this->db1->set($data);
$this->db1->insert($this->_table_name);
$id=$this->db1->insert_id();
I do this:
$this->load->model('mdb_m');
$dbms =$this->mdb_m->get();
if (count($dbms)) {
foreach ($dbms as $dbm) {
$mid=$dbm->id;
$dbc='db'.$mid;
$CI = &get_instance();
$this->db.$mid = $CI->load->database( "$dbc" , TRUE);
$this->db.$mid->set($data);
$this->db.$mid->insert($this->_table_name);
}
}
But it only insert at first database in the array then stop and retrive this error "You have specified an invalid database connection group."
you can get brief information of using multiple database from here.As you have mentioned.
https://ellislab.com/codeigniter/user-guide/database/connecting.html
for better approach you can make a loader file to load a database.I was in same problem and got the clear visualization from here
https://coderwall.com/p/_kyjvg/codeigniter-loading-up-multiple-databases
refer here and if still confused please revert back to me thankyou.

Accessing a database through codeigniter based on login

I have a functionality as follows:
When Any user logs in I want to connect to a particular DB based on login credentials
.
I tried with
$this->db->close();
and this->db->reconnect();
but it did not help
In your application/config/database.php you can define more than one database connection by doing
$db['database1']['hostname'] = 'localhost';
$db['database1']['username'] = 'db1_root';
$db['database1']['password'] = 'xxxxxx';
$db['database1']['database'] = 'database1_name';
$db['database1']['db_debug'] = false; //Important
$db['database2']['hostname'] = 'localhost';
$db['database2']['username'] = 'db2_root';
$db['database2']['password'] = 'xxxxxx';
$db['database2']['database'] = 'database2_name';
$db['database2']['db_debug'] = false; //Important
Then you can load specific databases by doing
$database1 = $this->load->database('database1', true);
$database2 = $this->load->database('database2', true);
Then rather than doing
$this->db->query();
You will need to do either
$database1->query();
$database2->query();
You can connect using a dynamic config in your controller/model/library/whatever by passing a config array to $this->load->database().
$config['hostname'] = "localhost";
$config['username'] = $user_name;
$config['password'] = $user_password;
$config['database'] = $user_database;
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";
$user_db = $this->load->database($config, true);
I figured out my answer.. Coded as follows:
in the database.php file i have added a new database config:
$db['db2']=$db['default'];
$db['db2']['database'] = 'new_db2';
and then in all the models constructor function i have put the following code:
$new_db = $this->load->database('db2', TRUE );
$this->db = $new_db;
So in this case i don't need to change all my queries. Hurray!!! :)

CodeIgniter: Multiple Databases - Accessing database config in a second database

I've been looking into using multiple databases with CodeIgniter. If I know what the databases are ahead of time, then I can set the information in the config file and then call whichever database group I need.
In my situation, however, I need to store that database information in another database. It is sort of a master database with general information about a customer including the database and credentials that the customer's data is stored in. This vendor can then add customers whenever they want and have each customer's data segregated in different databases.
How can I set the database and credentials based on the values I get back from the master database in CodeIgniter, or is there even a way to do that?
Can anyone point me in the right direction? Thanks in advance for any advice.
From the docs ( https://www.codeigniter.com/user_guide/database/connecting.html ) :
The first parameter of this function can optionally be used to specify
a particular database group from your config file, or you can even
submit connection values for a database that is not specified in your
config file.
So you would do something like this, replacing the values with values from the master database:
$config['hostname'] = "localhost";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "mydatabase";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";
$this->load->database($config);
If you need to maintain a connection to the master database and the customer database, then change the last line to:
$customer_db = $this->load->database($config, TRUE);
// to use the master database:
$this->db->query("SELECT * FROM my_table");
// to then use the customer database:
$customer_db->query("SELECT * FROM whatever");
Make the master a default database and the customer for second database
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = '';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['dbdriver'] = '';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
$db['secondDatabase']['hostname'] = '';
$db['secondDatabase']['username'] = '';
$db['secondDatabase']['password'] = '';
$db['secondDatabase']['dbdriver'] = '';
$db['secondDatabase']['dbprefix'] = '';
$db['secondDatabase']['pconnect'] = TRUE;
$db['secondDatabase']['db_debug'] = TRUE;
$db['secondDatabase']['cache_on'] = FALSE;
$db['secondDatabase']['cachedir'] = '';
$db['secondDatabase']['char_set'] = 'utf8';
$db['secondDatabase']['dbcollat'] = 'utf8_general_ci';
$db['secondDatabase']['swap_pre'] = '';
$db['secondDatabase']['autoinit'] = TRUE;
$db['secondDatabase']['stricton'] = FALSE;
you can load the second database in controller or in model by
$DB2 = $this->load->database('secondDatabase', TRUE);
/** config/database.php **/
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = '';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['dbdriver'] = '';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = (ENVIRONMENT !== 'production');
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
/** Your controller or model **/
//by default the master database will be loaded and you can directly access db using $this->db
$result = $this->db->query("SELECT * FROM `your_table`")->limit(1)->get()->result();
$config['dbxyz']['hostname'] = $result->hostname;
$config['dbxyz']['username'] = $result->username;
$config['dbxyz']['password'] = $result->password;
$config['dbxyz']['dbdriver'] = '';
$config['dbxyz']['dbprefix'] = '';
$config['dbxyz']['pconnect'] = TRUE;
$config['dbxyz']['db_debug'] = (ENVIRONMENT !== 'production');
$config['dbxyz']['cache_on'] = FALSE;
$config['dbxyz']['cachedir'] = '';
$config['dbxyz']['char_set'] = 'utf8';
$config['dbxyz']['dbcollat'] = 'utf8_general_ci';
$config['dbxyz']['swap_pre'] = '';
$config['dbxyz']['autoinit'] = TRUE;
$config['dbxyz']['stricton'] = FALSE;
//load database config
$this->config->load('database');
//Set database config dynamically
$this->config->set_item('dbxyz', $config);
//Now you can load the new database using
$this->dbxyz = $this->load->database('dbxyz');
NOTE: For more details, refer Config Class Codeigniter documentation
Add below line in application\config\database.php
$db['mydb2']['hostname'] = 'localhost';
$db['mydb2']['username'] = 'root';
$db['mydb2']['password'] = '';
$db['mydb2']['database'] = 'ci2';
$db['mydb2']['dbdriver'] = 'mysql';
$db['mydb2']['dbprefix'] = '';
$db['mydb2']['pconnect'] = TRUE;
$db['mydb2']['db_debug'] = TRUE;
$db['mydb2']['cache_on'] = FALSE;
$db['mydb2']['cachedir'] = '';
$db['mydb2']['char_set'] = 'utf8';
$db['mydb2']['dbcollat'] = 'utf8_general_ci';
$db['mydb2']['swap_pre'] = '';
$db['mydb2']['autoinit'] = TRUE;
$db['mydb2']['stricton'] = FALSE;
Now we use our second database in our Controller and model like below.
$CI = &get_instance();
$this->db2 = $CI->load->database('mydb2', TRUE);
$qry = $this->db2->query("SELECT * FROM employee");
print_r($qry->result());
I have taken reference from http://www.tutsway.com/use-multiple-db-connections-in-codeigniter.php .It's work for me.

Resources