PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Adapto/Document/Writer.php

http://github.com/egeniq/adapto
PHP | 296 lines | 131 code | 42 blank | 123 comment | 22 complexity | 4905bd9a84fa2ad4bcb1f4b8b7c1ae85 MD5 | raw file
  1. <?php
  2. /**
  3. * Adapto_Document_Writer class file
  4. *
  5. * @package adapto
  6. * @subpackage document
  7. *
  8. * @author guido <guido@ibuildings.nl>
  9. *
  10. * @copyright (c) 2005 Ibuildings.nl BV
  11. * @license http://www.achievo.org/atk/licensing/ ATK open source license
  12. *
  13. */
  14. /**
  15. * General DocumentWriter framework class. Should be extended to support specific file formats.
  16. *
  17. * @author guido <guido@ibuildings.nl>
  18. * @package adapto
  19. * @subpackage document
  20. */
  21. class Adapto_Document_Writer
  22. {
  23. /**
  24. * Template vars array
  25. *
  26. * @access protected
  27. * @var array
  28. */
  29. public $m_tpl_vars = array(); // defaulted to public
  30. public $m_taglist = ""; // defaulted to public
  31. /**
  32. * Adapto_Document_Writer Constructor.
  33. *
  34. * Dont use this, use Adapto_Document_Writer::getInstance($format) instead to get a singleton instance for any format used
  35. */
  36. public function __construct()
  37. {
  38. }
  39. /**
  40. * Assigns values to template variables
  41. *
  42. * @param string|array $tpl_var Template variable name or array of variable name/value pairs
  43. * @param mixed $value Value to assign (only used if $tpl_var is a string)
  44. */
  45. function assign($tpl_var, $value = null)
  46. {
  47. if (is_array($tpl_var))
  48. {
  49. foreach ($tpl_var as $key => $val)
  50. {
  51. if ($key != '')
  52. {
  53. $this->m_tpl_vars[$key] = $val;
  54. }
  55. }
  56. }
  57. else
  58. {
  59. if ($tpl_var != '')
  60. $this->m_tpl_vars[$tpl_var] = $value;
  61. }
  62. }
  63. /**
  64. * Returns labels for all attributes of an entity
  65. *
  66. * @param atkEntity $entity Entity for which the labels should be retrieved
  67. * @return Array Associative array containing attributename=>label pairs
  68. */
  69. function getRecordLabels(&$entity)
  70. {
  71. // Initialize the result array
  72. $result = array();
  73. // Loop through the attributes in order to assign them all to the documentwriter
  74. foreach(array_keys($entity->m_attribList) as $key)
  75. {
  76. // Get a reference to the attribute
  77. $p_attrib = &$entity->m_attribList[$key];
  78. // Get the Label of the attribute (can be suppressed with AF_NOLABEL or AF_BLANKLABEL)
  79. if ($p_attrib->hasFlag(AF_NOLABEL) || $p_attrib->hasFlag(AF_BLANKLABEL))
  80. $result[$key] = "";
  81. else
  82. $result[$key] = $p_attrib->label(array());
  83. }
  84. // Return the array containing attributename=>label pairs
  85. return $result;
  86. }
  87. /**
  88. * Returns labels for all attributes of an entity
  89. *
  90. * @param atkEntity $entity Entity for which the displayvalues should be retrieved
  91. * @param Array $record Record for which the display values should be determined
  92. * @return Array Associative array containing attributename=>displayvalue pairs
  93. */
  94. function getRecordDisplayValues(&$entity, $record)
  95. {
  96. // Initialize the result array
  97. $result = array();
  98. // Loop through the attributes in order to assign them all to the documentwriter
  99. foreach(array_keys($entity->m_attribList) as $key)
  100. {
  101. // Get a reference to the attribute
  102. $p_attrib = &$entity->m_attribList[$key];
  103. // Get the display value by calling <attribute>_display().
  104. // An <attributename>_display function may be provided in a derived
  105. // class to display an attribute. If it exists we will use that method
  106. // else we will just use the attribute's display method.
  107. $funcname = $p_attrib->m_name."_display";
  108. if (method_exists($entity, $funcname))
  109. $result[$key] = $entity->$funcname($record, "plain");
  110. else
  111. $result[$key] = $p_attrib->display($record, "plain");
  112. }
  113. // Return the array containing attributename=>displayvalue pairs
  114. return $result;
  115. }
  116. /**
  117. * Assigns the labels for all attributes of an entity to the documentWriter
  118. *
  119. * @param atkEntity $entity Entity for which the labels should be retrieved
  120. * @param String $prefix Prefix to be used when assigning the variables (used to avoid conflicting names)
  121. */
  122. function _assignLabels(&$entity, $prefix)
  123. {
  124. // Get all labels for the given entity
  125. $labels = $this->getRecordLabels($entity);
  126. // Assign all labels to the documentwriter
  127. foreach($labels as $key => $label)
  128. $this->Assign($prefix . $key . "_label", $label);
  129. }
  130. /**
  131. * Enter description here...
  132. *
  133. * @param atkEntity $entity Entity to be used when displaying the records
  134. * @param Array $records Array of records that should be assigned to the documentwriter
  135. * @param String $prefix Prefix to be used when assigning the variables (used to avoid conflicting names)
  136. */
  137. function assignDocumentMultiRecord(&$entity, $records, $prefix = "")
  138. {
  139. // Assign all labels to the documentwriter
  140. $this->_assignLabels($entity, $prefix);
  141. // Initialize the displayvalues array
  142. $displayvalues = array();
  143. // Loop through all records and add the displayvalues to the array
  144. foreach($records as $record)
  145. $displayvalues[] = $this->getRecordDisplayValues($entity, $record);
  146. // Assign the displayvalues array to the documentwriter
  147. $this->Assign($prefix . $entity->m_type, $displayvalues);
  148. // Register the taglist
  149. $this->m_taglist .= sprintf("%s codes (all prefixed by %s%s.)\n", $entity->text($entity->m_type), $prefix, $entity->m_type);
  150. foreach(array_keys($entity->m_attribList) as $key)
  151. $this->m_taglist .= "[$prefix{$entity->m_type}.$key]\n";
  152. $this->m_taglist .= "You can use these tags in a table. More info: http://www.achievo.org/wiki/AtkDocumentWriter\n";
  153. $this->m_taglist .= "\n";
  154. }
  155. /**
  156. * Enter description here...
  157. *
  158. * @param atkEntity $entity Entity to be used when displaying the record
  159. * @param Array $record Record that should be assigned to the documentwriter
  160. * @param String $prefix Prefix to be used when assigning the variables (used to avoid conflicting names)
  161. */
  162. function assignDocumentSingleRecord(&$entity, $record, $prefix = "")
  163. {
  164. // Assign all labels to the documentwriter
  165. $this->_assignLabels($entity, $prefix);
  166. // Get all display values from the given record
  167. $displayvalues = $this->getRecordDisplayValues($entity, $record);
  168. // Loop through all display values and assign them to the documentwriter
  169. foreach($displayvalues as $key => $displayvalue)
  170. $this->Assign($prefix . $key, $displayvalue);
  171. // Register the taglist
  172. $this->m_taglist .= sprintf("%s codes%s\n", $entity->text($entity->m_type), empty($prefix)?"":" (all prefixed by $prefix)");
  173. foreach(array_keys($entity->m_attribList) as $key)
  174. $this->m_taglist .= "[$prefix$key]\n";
  175. $this->m_taglist .= "\n";
  176. }
  177. /**
  178. * Assigns data to the document based on only an entityname and a selector
  179. *
  180. * @param string $entityname Name (module.entity) for the entity
  181. * @param string $selector Selector containing a SQL expression
  182. * @param string $prefix Prefix to be used when assigning variables to the document
  183. * @return boolean True if a record is found and assigned, false if not
  184. */
  185. function assignRecordByEntityAndSelector($entityname, $selector, $prefix = "")
  186. {
  187. // Do not continue and return false if no selector given
  188. if ($selector == "")
  189. return false;
  190. // Assign the quotation owner to the document
  191. $entity = &atkGetEntity($entityname);
  192. // Get the record from the database
  193. $records = $entity->selectDb($selector, "", "", "", "", "view");
  194. // Do not continue and return false if no records were found
  195. if (count($records) == 0)
  196. return false;
  197. // Assign the record to the document.
  198. $this->assignDocumentSingleRecord($entity, $records[0], $prefix);
  199. // Return succesfully
  200. return true;
  201. }
  202. /**
  203. * Assigns commonly used variables to a documentWriter
  204. *
  205. * @param string $prefix Prefix to be used when assigning the variables (used to avoid conflicting names)
  206. */
  207. function assignDocumentGenericVars($prefix = "")
  208. {
  209. // Get the current date and a reference to an atkDateAttribute in order to format the current date
  210. $date = adodb_getdate();
  211. $dateattribute = new Adapto_DateAttribute("dummy");
  212. // Assign the date in short and long format as [shortdate] and [longdate]
  213. $this->Assign($prefix . "shortdate", $dateattribute->formatDate($date, "d-m-Y", 0));
  214. $this->Assign($prefix . "longdate", $dateattribute->formatDate($date, "j F Y", 0));
  215. // Assign the taglist
  216. $this->Assign($prefix . "taglist", $this->m_taglist);
  217. }
  218. /**
  219. * Get a singleton instance of the Adapto_Document_Writer class for any format used
  220. *
  221. * @param string $format Document format to be used (defaults to opendocument).
  222. * @return Adapto_Document_Writer Returns singleton instance of Adapto_Document_Writer descendant (depends on given format)
  223. */
  224. function &getInstance($format = "opendocument")
  225. {
  226. static $s_oo_instance = NULL;
  227. static $s_docx_instance = NULL;
  228. if ($format == "opendocument")
  229. {
  230. if ($s_oo_instance == NULL)
  231. {
  232. Adapto_Util_Debugger::debug("Creating a new Adapto_OpenDocumentWriter instance");
  233. $s_oo_instance = new Adapto_OpenDocumentWriter();
  234. }
  235. return $s_oo_instance;
  236. }
  237. else if ($format == "docx")
  238. {
  239. if ($s_docx_instance == NULL)
  240. {
  241. Adapto_Util_Debugger::debug("Creating a new Adapto_DocxWriter instance");
  242. $s_docx_instance = new Adapto_DocxWriter();
  243. }
  244. return $s_docx_instance;
  245. }
  246. else
  247. {
  248. Adapto_Util_Debugger::debug(sprintf("Failed to create Adapto_Document_Writer instance (unknown format: %s)", $format));
  249. }
  250. }
  251. }
  252. ?>