/plugins/postfields/branches/0.7-1.x/postfields.plugin.php

https://github.com/somefool/habari-extras · PHP · 187 lines · 122 code · 22 blank · 43 comment · 13 complexity · 27e940656aa35a1afdc4e352020e8dbe MD5 · raw file

  1. <?php
  2. /**
  3. * Post Fields - A plugin to display additional fields on the publish page
  4. **/
  5. class postfields extends Plugin
  6. {
  7. /**
  8. * Add actions to the plugin page for this plugin
  9. *
  10. * @param array $actions An array of actions that apply to this plugin
  11. * @param string $plugin_id The string id of a plugin, generated by the system
  12. * @return array The array of actions to attach to the specified $plugin_id
  13. */
  14. public function filter_plugin_config($actions, $plugin_id)
  15. {
  16. if ($plugin_id == $this->plugin_id()){
  17. $types = Post::list_active_post_types();
  18. unset($types['any']);
  19. foreach($types as $type => $id) {
  20. $actions['config-' . $id] = _t('%s fields', array($type));
  21. }
  22. $actions['plugin'] = _t('Build Plugin');
  23. }
  24. return $actions;
  25. }
  26. /**
  27. * Respond to the user selecting an action on the plugin page
  28. *
  29. * @param string $plugin_id The string id of the acted-upon plugin
  30. * @param string $action The action string supplied via the filter_plugin_config hook
  31. */
  32. public function action_plugin_ui($plugin_id, $action)
  33. {
  34. if ($plugin_id == $this->plugin_id()) {
  35. switch($action) {
  36. case 'plugin':
  37. $ui = new FormUI('postfields');
  38. $ui->append('static', 'typelabel', _t('Add this code to a plugin to implement the currently configured fields.'));
  39. $ui->append('textarea', 'plugincode', 'null:null', _t('Plugin code:'))->value = $this->get_code();
  40. $ui->out();
  41. break;
  42. default:
  43. $types = array_flip(Post::list_active_post_types());
  44. $key = substr($action, 7);
  45. $ui = new FormUI('postfields');
  46. $ui->append('static', 'typelabel', _t('Adding fields to the "%s" post type.', array($types[$key])));
  47. $ui->append('textmulti', 'fields', 'postfields__fields_' . $key, 'Additional Fields:');
  48. $ui->append('submit', 'submit', 'Submit');
  49. $ui->out();
  50. }
  51. }
  52. }
  53. /**
  54. * Add additional controls to the publish page tab
  55. *
  56. * @param FormUI $form The form that is used on the publish page
  57. * @param Post $post The post being edited
  58. **/
  59. public function action_form_publish($form, $post)
  60. {
  61. $fields = Options::get('postfields__fields_' . $post->content_type);
  62. if(!is_array($fields) || count($fields) == 0) {
  63. return;
  64. }
  65. $output = '';
  66. $control_id = 0;
  67. $postfields = $form->publish_controls->append('fieldset', 'postfields', 'Additional Fields');
  68. foreach($fields as $field) {
  69. $control_id = md5($field);
  70. $fieldname = "postfield_{$control_id}";
  71. $customfield = $postfields->append('text', $fieldname, 'null:null', $field);
  72. $customfield->value = isset($post->info->{$field}) ? $post->info->{$field} : '';
  73. $customfield->template = 'tabcontrol_text';
  74. }
  75. }
  76. /**
  77. * Modify a post before it is updated
  78. *
  79. * @param Post $post The post being saved, by reference
  80. * @param FormUI $form The form that was submitted on the publish page
  81. */
  82. public function action_publish_post($post, $form)
  83. {
  84. $fields = Options::get('postfields__fields_' . $post->content_type);
  85. if(!is_array($fields) || count($fields) == 0) {
  86. return;
  87. }
  88. foreach($fields as $field) {
  89. $control_id = md5($field);
  90. $fieldname = "postfield_{$control_id}";
  91. $customfield = $form->$fieldname;
  92. $post->info->{$field} = $customfield->value;
  93. }
  94. }
  95. public function get_code()
  96. {
  97. $cases_form = '';
  98. $types = Post::list_active_post_types();
  99. unset($types['any']);
  100. foreach($types as $type => $id) {
  101. $fields = Options::get('postfields__fields_' . $id);
  102. if(!is_array($fields) || count($fields) == 0) {
  103. continue;
  104. }
  105. $fieldlist = array();
  106. foreach($fields as $field) {
  107. $fieldlist[] = "'" . addslashes($field) . "'";
  108. }
  109. $fieldlist = implode(', ', $fieldlist);
  110. $cases_form .= "\t\t\tcase {$id}:\n\t\t\t\t\$fields = array({$fieldlist});\n\t\t\t\tbreak;\n";
  111. }
  112. $code = <<< PLUGIN_CODE_1
  113. /**
  114. * Add additional controls to the publish page tab
  115. *
  116. * @param FormUI \$form The form that is used on the publish page
  117. * @param Post \$post The post being edited
  118. **/
  119. public function action_form_publish(\$form, \$post)
  120. {
  121. switch(\$post->content_type) {
  122. {$cases_form}
  123. default:
  124. return;
  125. }
  126. foreach(\$fields as \$field) {
  127. \$control_id = md5(\$field);
  128. \$fieldname = "postfield_{\$control_id}";
  129. \$customfield = \$postfields->append('text', \$fieldname, 'null:null', \$field);
  130. \$customfield->value = isset(\$post->info->{\$field}) ? \$post->info->{\$field} : '';
  131. \$customfield->template = 'tabcontrol_text';
  132. }
  133. }
  134. /**
  135. * Modify a post before it is updated
  136. *
  137. * @param Post \$post The post being saved, by reference
  138. * @param FormUI \$form The form that was submitted on the publish page
  139. */
  140. public function action_publish_post(\$post, \$form)
  141. {
  142. switch(\$post->content_type) {
  143. {$cases_form}
  144. default:
  145. return;
  146. }
  147. foreach(\$fields as \$field) {
  148. \$control_id = md5(\$field);
  149. \$fieldname = "postfield_{\$control_id}";
  150. \$customfield = \$form->\$fieldname;
  151. \$post->info->{\$field} = \$customfield->value;
  152. }
  153. }
  154. PLUGIN_CODE_1;
  155. return $code;
  156. }
  157. /**
  158. * Add update beacon support
  159. **/
  160. public function action_update_check()
  161. {
  162. Update::add( 'Postfields', '228D6060-38F0-11DD-AE16-0800200C9A66', $this->info->version );
  163. }
  164. }
  165. ?>