PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/sgl/assets/_core/php/examples/plugins/components.php

http://logisticsouth.googlecode.com/
PHP | 111 lines | 100 code | 9 blank | 2 comment | 1 complexity | f83c14c2c84b07b251dc8e90bf633719 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php require_once('../qcubed.inc.php'); ?>
  2. <?php require('../includes/header.inc.php'); ?>
  3. <div class="instructions" style="max-height: none">
  4. <h1 class="instruction_title">Make Your Own Plugin, Part 1: Plugin Components</h1>
  5. If you've written a component that you think may be broadly usable for
  6. the QCubed community, it would be amazing if you took a few minutes to
  7. wrap it up nicely as a plugin - this would help everyone install your
  8. stuff easily, and who doesn't love it when their code is used and
  9. appreciated? <br /><br />
  10. Writing a plugin is pretty simple. The first thing I'd do to learn about
  11. it is browse through the <a target="_blank"
  12. href="http://trac.qcu.be/projects/qcubed/browser/plugins">source code of
  13. one of the existing plugins</a> to learn how it's been put together.<br /><br />
  14. If you're ready to get started with your own plugin, create a
  15. directory somewhere on your computer; we'll refer to that directory as
  16. the "root" of your plugin. Add one file to the root: <b><?php echo
  17. QPluginInstaller::PLUGIN_CONFIG_GENERATION_FILE ?></b>. That
  18. configuration file will describe the steps that QCubed needs to take
  19. while installing, as well as uninstalling your plugin.<br /><br />
  20. Then, create a few folders under the root, and place the files that you
  21. want to be distributed with the plugin there - the structure is entirely
  22. up to you. You may want to put all your included PHP files under the includes
  23. directory, or you may not; you can put all images in a separate folder, or
  24. just keep them as siblings of the configuration file. <br /><br />
  25. A plugin is described through a <b>QPlugin</b> object - you can see all the
  26. properties of that object by inspecting the
  27. <b><?php echo substr(__QCUBED_CORE__, strlen(__DOCROOT__)) ?>/framework/QPluginInterface.class.php</b>
  28. file. <a href="javascript:ViewSource(<?php _p(Examples::GetCategoryId() . ',' . Examples::GetExampleId() . ',"__CORE_FRAMEWORK__QPluginInterface.class.php"'); ?>);">Take a look </a>at it now.<br /><br />
  29. To define the QPlugin object, we'll first set simple metadata on it:
  30. <div style="padding-left: 50px;">
  31. <code>
  32. $objPlugin = new QPlugin();<br />
  33. $objPlugin->strName = "MyCoolPlugin"; // no spaces allowed<br />
  34. $objPlugin->strDescription = 'A great little plugin that does this and that';<br />
  35. $objPlugin->strVersion = "0.1";<br />
  36. $objPlugin->strPlatformVersion = "1.1"; // version of QCubed that this plugin works well with<br />
  37. $objPlugin->strAuthorName = "Alex Weinstein, a.k.a. alex94040";<br />
  38. $objPlugin->strAuthorEmail ="alex94040 [at] yahoo [dot] com";
  39. </code>
  40. </div><br />
  41. Then, let's add <b>QPluginFile</b>'s to the plugin. Each of the files that you
  42. added to the root folder will need to be mentioned, along with some relevant
  43. metadata for it, in order for that file to be deployed. Some of the plugin
  44. components you need to be aware of are:
  45. <ul>
  46. <li><b>QPluginControlFile</b>: a class that extends QControl.</li>
  47. <li><b>QPluginMiscIncludedFile</b>: miscellaneous include file (non-web accessible).</li>
  48. <li><b>QPluginCssFile</b>, <b>QPluginJsFile</b>, <b>QPluginImageFile</b>: CSS, JavaScript, and image resources.</li>
  49. <li><b>QPluginExampleFile</b>: an example file for the plugin. Note that images, .tpl files, and other resources that
  50. are only used as a part of the example should all be declared as QPluginExampleFile's.</li>
  51. </ul>
  52. Let's now register several <b>QPluginFiles</b> with your <b>QPlugin</b>. Note that
  53. all paths are relative to the root of your plugin:
  54. <div style="padding-left: 50px;">
  55. <code>
  56. $files = array(); <br />
  57. $files[] = new QPluginControlFile("includes/QPhoneTextBox.class.php");<br />
  58. $files[] = new QPluginJsFile("js/phonetextbox.js");<br />
  59. $files[] = new QPluginExampleFile("example/phonetextbox.php");<br />
  60. $files[] = new QPluginExampleFile("example/phonetextbox.tpl.php");<br />
  61. $objPlugin->addComponents($files);
  62. </code>
  63. </div><br />
  64. After you've added all the files, it's time to declare any classfiles that need
  65. to be included when QCubed attempts to instantiate your plugin. You can do this
  66. by adding a <b>QPluginIncludedClass</b> component to your <b>QPlugin</b>:
  67. <div style="padding-left: 50px;">
  68. <code>
  69. $components = array(); <br />
  70. // First parameter is the name of the class, second - path to the file, <br />
  71. // relative to the root of your plugin. Note that the QFile for this included<br />
  72. // class should already be declared above! <br />
  73. $components[] = new QPluginIncludedClass("QPhoneTextBox", "includes/QPhoneTextBox.class.php");<br />
  74. $objPlugin->addComponents($components);<br />
  75. </code>
  76. </div><br />
  77. It's always a good idea to provide a few examples with your plugin. To do so,
  78. we will create <b>QPluginExample</b> components, and add them to our <b>QPlugin</b>:
  79. <div style="padding-left: 50px;">
  80. <code>
  81. $components = array(); <br />
  82. // First parameter is the path to the file, relative to the root of your plugin.<br />
  83. // Second parameter is the description of the example. <br />
  84. $components[] = new QPluginExample("example/phonetextbox.php", "Validate and format phone numbers");<br />
  85. $objPlugin->addComponents($components);<br />
  86. </code>
  87. </div><br />
  88. Now, add a magical line to the end of the configuration file...
  89. <div style="padding-left: 50px;">
  90. <code>
  91. $objPlugin->install();
  92. </code>
  93. </div><br />
  94. ..and you're done! <a href="packaging.php">Read the next chapter</a> to
  95. learn about ways to package and distribute your plugin. <br /><br />
  96. </div>
  97. <?php require('../includes/footer.inc.php'); ?>