Assigning custom variables to a document template: Difference between revisions
No edit summary |
No edit summary |
||
| Line 2: | Line 2: | ||
The standard AssignDocumentVars function which is loacted within the atkDocumentHandler class file is below: | The standard AssignDocumentVars function which is loacted within the atkDocumentHandler class file is below: | ||
<syntaxhighlight lang="php"> | |||
/** | /** | ||
* Default document assignment function (assigns the given record and | * Default document assignment function (assigns the given record and | ||
| Line 19: | Line 19: | ||
$documentWriter->assignDocumentGenericVars(); | $documentWriter->assignDocumentGenericVars(); | ||
} | } | ||
</syntaxhighlight> | |||
This is a wrapper function which performs three tasks. | This is a wrapper function which performs three tasks. | ||
1. Load the selected record within the current node from the database. | 1. Load the selected record within the current node from the database. | ||
| Line 25: | Line 26: | ||
In order to populate the document with data from a related node we need to perform steps 1 and 2 for the related node. Fortuneatly the following code within the action_document function of the document handler allows for an overide to the above function within the node class file. | In order to populate the document with data from a related node we need to perform steps 1 and 2 for the related node. Fortuneatly the following code within the action_document function of the document handler allows for an overide to the above function within the node class file. | ||
<syntaxhighlight lang="php"> | |||
// Assign the record variables to the OpenOffice.org DocumentWriter | // Assign the record variables to the OpenOffice.org DocumentWriter | ||
if (method_exists($this->m_node, "assignDocumentVars")) | if (method_exists($this->m_node, "assignDocumentVars")) | ||
| Line 31: | Line 32: | ||
else | else | ||
$this->assignDocumentVars($openDocumentWriter, $this->m_postvars["atkselector"]); | $this->assignDocumentVars($openDocumentWriter, $this->m_postvars["atkselector"]); | ||
</syntaxhighlight> | |||
I have included an example of an overide to this function below: | I have included an example of an overide to this function below: | ||
<syntaxhighlight lang="php"> | |||
function assignDocumentVars($node, &$documentWriter, $selector) | function assignDocumentVars($node, &$documentWriter, $selector) | ||
{ | { | ||
| Line 62: | Line 63: | ||
$documentWriter->assignDocumentGenericVars(); | $documentWriter->assignDocumentGenericVars(); | ||
} | } | ||
</syntaxhighlight> | |||
I will repeat the above in blocks and relevant comments on each section below: | I will repeat the above in blocks and relevant comments on each section below: | ||
<syntaxhighlight lang="php"> | |||
function assignDocumentVars($node, &$documentWriter, $selector) | function assignDocumentVars($node, &$documentWriter, $selector) | ||
{ | { | ||
</syntaxhighlight> | |||
Notice the function now has three parameters the first paramater is $node which references the node in which the function is being called. | Notice the function now has three parameters the first paramater is $node which references the node in which the function is being called. | ||
<syntaxhighlight lang="php"> | |||
// Load the selected record from the database | // Load the selected record from the database | ||
$record = $this->selectDb($selector); | $record = $this->selectDb($selector); | ||
$documentWriter->assignDocumentSingleRecord($this, $record[0]); | $documentWriter->assignDocumentSingleRecord($this, $record[0]); | ||
</syntaxhighlight> | |||
This is the same as the original function and assigns the variables for the current node as described in [[atkDocumentAttribute]] | This is the same as the original function and assigns the variables for the current node as described in [[atkDocumentAttribute]] | ||
<syntaxhighlight lang="php"> | |||
// Assign the referenced reportdetail - ManyToOne attribute | // Assign the referenced reportdetail - ManyToOne attribute | ||
$report = &atkGetNode("developmentprograms.dpreports"); | $report = &atkGetNode("developmentprograms.dpreports"); | ||
$reportrecord = $report->selectDb("ccm_dpreports.dp_report_id='".$record[0]["dp_report_id"]["dp_report_id"]."'"); | $reportrecord = $report->selectDb("ccm_dpreports.dp_report_id='".$record[0]["dp_report_id"]["dp_report_id"]."'"); | ||
$documentWriter->assignDocumentSingleRecord($report, $reportrecord[0], "report_"); | $documentWriter->assignDocumentSingleRecord($report, $reportrecord[0], "report_"); | ||
</syntaxhighlight> | |||
The above section of code is for a m2o relation attribute and uses the relation id held in $record to pull the related nodes data. It then uses assigns the document vars, notice the optional third paramater in assignDocumentSingleRecord this is a prefix for the template variables. Therefore in the above example you would write [report_attribute_name] in the template document where you wish to place the related attribute. | The above section of code is for a m2o relation attribute and uses the relation id held in $record to pull the related nodes data. It then uses assigns the document vars, notice the optional third paramater in assignDocumentSingleRecord this is a prefix for the template variables. Therefore in the above example you would write [report_attribute_name] in the template document where you wish to place the related attribute. | ||
<syntaxhighlight lang="php"> | |||
// Assign the referenced childdetail - ManyToOne attribute | |||
$child = &atkGetNode("nursery.children"); | $child = &atkGetNode("nursery.children"); | ||
$childrecord = $child->selectDb("ccm_children.child_id='".$record[0]["child_id"]["child_id"]."'"); | $childrecord = $child->selectDb("ccm_children.child_id='".$record[0]["child_id"]["child_id"]."'"); | ||
| Line 86: | Line 92: | ||
$kwrecord = $kw->selectDb("ccm_authuser.userid='".$record[0]["assigned_to"]["userid"]."'"); | $kwrecord = $kw->selectDb("ccm_authuser.userid='".$record[0]["assigned_to"]["userid"]."'"); | ||
$documentWriter->assignDocumentSingleRecord($kw, $kwrecord[0], "kw_"); | $documentWriter->assignDocumentSingleRecord($kw, $kwrecord[0], "kw_"); | ||
</syntaxhighlight> | |||
The above two code sections are a repeat of the functionality for two other m2o relations. | The above two code sections are a repeat of the functionality for two other m2o relations. | ||
<syntaxhighlight lang="php"> | |||
// Assign the referenced Sum Rep text detail - OneToMany attribute | |||
$sumrep = &atkGetNode("developmentprograms.children2dpsum"); | $sumrep = &atkGetNode("developmentprograms.children2dpsum"); | ||
$sumreprecords = $sumrep->selectDb("ccm_childrentodpsum.children_dp_report_id='".$record[0]["children_dp_report_id"]."'"); | $sumreprecords = $sumrep->selectDb("ccm_childrentodpsum.children_dp_report_id='".$record[0]["children_dp_report_id"]."'"); | ||
| Line 95: | Line 103: | ||
$documentWriter->assignDocumentSingleRecord($sumrep, $sumreprecord, $file_tag); | $documentWriter->assignDocumentSingleRecord($sumrep, $sumreprecord, $file_tag); | ||
} | } | ||
</syntaxhighlight> | |||
This is a more lenghty piece of code used for a o2m relationship. Because the selectdb method will pull back more than one record we have to iterate through the records to insert the variables. Also for this example within the related node their is a field I wish to use as the prefix (i.e file_tag). This is usefull to direct the exact placement of each of the "many" records. If "m" is defined/limited in your relation you may prefer to iterate through with a numeric prefix. | This is a more lenghty piece of code used for a o2m relationship. Because the selectdb method will pull back more than one record we have to iterate through the records to insert the variables. Also for this example within the related node their is a field I wish to use as the prefix (i.e file_tag). This is usefull to direct the exact placement of each of the "many" records. If "m" is defined/limited in your relation you may prefer to iterate through with a numeric prefix. | ||
<syntaxhighlight lang="php"> | |||
// Also assign the generic (date) vars tot the documentWriter | // Also assign the generic (date) vars tot the documentWriter | ||
$documentWriter->assignDocumentGenericVars(); | $documentWriter->assignDocumentGenericVars(); | ||
} | } | ||
</syntaxhighlight> | |||
This is the same generic assignment in the original function. | This is the same generic assignment in the original function. | ||
Revision as of 14:24, 13 January 2007
As previously discussed the standard implementation of atkDocumentAttribute works well at assigning the variables within the node that the document attribute has been created in, however it cannot reference related data in another node which is linked to the current node by a m2o or o2m relation. The standard AssignDocumentVars function which is loacted within the atkDocumentHandler class file is below:
/**
* Default document assignment function (assigns the given record and
* the generic vars)
*
* @param atkDocumentWriter $documentWriter DocumentWriter to which the variables should be assigned
* @param String $selector String containing the selector used to get the document from the database
*/
function assignDocumentVars(&$documentWriter, $selector)
{
// Load the selected record from the database
$record = $this->m_node->selectDb($selector, "", "", $this->m_viewExcludes, "", "document");
// Assign the record to the documentWriter
$documentWriter->assignDocumentSingleRecord($this->m_node, $record[0]);
// Also assign the generic (date) vars tot the documentWriter
$documentWriter->assignDocumentGenericVars();
}
This is a wrapper function which performs three tasks. 1. Load the selected record within the current node from the database. 2. Assign the record variables to the document using the docuement writer functions. 3. Assign generic variables to the document using the docuement writer functions.
In order to populate the document with data from a related node we need to perform steps 1 and 2 for the related node. Fortuneatly the following code within the action_document function of the document handler allows for an overide to the above function within the node class file.
// Assign the record variables to the OpenOffice.org DocumentWriter
if (method_exists($this->m_node, "assignDocumentVars"))
$this->m_node->assignDocumentVars($this, $openDocumentWriter, $this->m_postvars["atkselector"]);
else
$this->assignDocumentVars($openDocumentWriter, $this->m_postvars["atkselector"]);
I have included an example of an overide to this function below:
function assignDocumentVars($node, &$documentWriter, $selector)
{
// Load the selected record from the database
$record = $this->selectDb($selector);
$documentWriter->assignDocumentSingleRecord($this, $record[0]);
// Assign the referenced reportdetail - ManyToOne attribute
$report = &atkGetNode("developmentprograms.dpreports");
$reportrecord = $report->selectDb("ccm_dpreports.dp_report_id='".$record[0]["dp_report_id"]["dp_report_id"]."'");
$documentWriter->assignDocumentSingleRecord($report, $reportrecord[0], "report_");
// Assign the referenced childdetail - ManyToOne attribute
$child = &atkGetNode("nursery.children");
$childrecord = $child->selectDb("ccm_children.child_id='".$record[0]["child_id"]["child_id"]."'");
$documentWriter->assignDocumentSingleRecord($child, $childrecord[0], "child_");
// Assign the referenced keyworker detail - ManyToOne attribute
$kw = &atkGetNode("system.users");
$kwrecord = $kw->selectDb("ccm_authuser.userid='".$record[0]["assigned_to"]["userid"]."'");
$documentWriter->assignDocumentSingleRecord($kw, $kwrecord[0], "kw_");
// Assign the referenced Sum Rep text detail - OneToMany attribute
$sumrep = &atkGetNode("developmentprograms.children2dpsum");
$sumreprecords = $sumrep->selectDb("ccm_childrentodpsum.children_dp_report_id='".$record[0]["children_dp_report_id"]."'");
foreach ($sumreprecords as $sumreprecord)
{
$file_tag = $sumreprecord["dp_area_id"]["file_tag"]."_";
$documentWriter->assignDocumentSingleRecord($sumrep, $sumreprecord, $file_tag);
}
// Also assign the generic (date) vars tot the documentWriter
$documentWriter->assignDocumentGenericVars();
}
I will repeat the above in blocks and relevant comments on each section below:
function assignDocumentVars($node, &$documentWriter, $selector)
{
Notice the function now has three parameters the first paramater is $node which references the node in which the function is being called.
// Load the selected record from the database
$record = $this->selectDb($selector);
$documentWriter->assignDocumentSingleRecord($this, $record[0]);
This is the same as the original function and assigns the variables for the current node as described in atkDocumentAttribute
// Assign the referenced reportdetail - ManyToOne attribute
$report = &atkGetNode("developmentprograms.dpreports");
$reportrecord = $report->selectDb("ccm_dpreports.dp_report_id='".$record[0]["dp_report_id"]["dp_report_id"]."'");
$documentWriter->assignDocumentSingleRecord($report, $reportrecord[0], "report_");
The above section of code is for a m2o relation attribute and uses the relation id held in $record to pull the related nodes data. It then uses assigns the document vars, notice the optional third paramater in assignDocumentSingleRecord this is a prefix for the template variables. Therefore in the above example you would write [report_attribute_name] in the template document where you wish to place the related attribute.
// Assign the referenced childdetail - ManyToOne attribute
$child = &atkGetNode("nursery.children");
$childrecord = $child->selectDb("ccm_children.child_id='".$record[0]["child_id"]["child_id"]."'");
$documentWriter->assignDocumentSingleRecord($child, $childrecord[0], "child_");
// Assign the referenced keyworker detail - ManyToOne attribute
$kw = &atkGetNode("system.users");
$kwrecord = $kw->selectDb("ccm_authuser.userid='".$record[0]["assigned_to"]["userid"]."'");
$documentWriter->assignDocumentSingleRecord($kw, $kwrecord[0], "kw_");
The above two code sections are a repeat of the functionality for two other m2o relations.
// Assign the referenced Sum Rep text detail - OneToMany attribute
$sumrep = &atkGetNode("developmentprograms.children2dpsum");
$sumreprecords = $sumrep->selectDb("ccm_childrentodpsum.children_dp_report_id='".$record[0]["children_dp_report_id"]."'");
foreach ($sumreprecords as $sumreprecord)
{
$file_tag = $sumreprecord["dp_area_id"]["file_tag"]."_";
$documentWriter->assignDocumentSingleRecord($sumrep, $sumreprecord, $file_tag);
}
This is a more lenghty piece of code used for a o2m relationship. Because the selectdb method will pull back more than one record we have to iterate through the records to insert the variables. Also for this example within the related node their is a field I wish to use as the prefix (i.e file_tag). This is usefull to direct the exact placement of each of the "many" records. If "m" is defined/limited in your relation you may prefer to iterate through with a numeric prefix.
// Also assign the generic (date) vars tot the documentWriter
$documentWriter->assignDocumentGenericVars();
}
This is the same generic assignment in the original function.
Acknowledgement
All of the above was a result of discussions and help in the following thread ManyToOne Relations and OneToMany Relations with atkDocument in the atk Forums.
I would like to specifically thank Yury whose assistance through pm's helped me clinch the final part of this solution.