Are there any examples using the Zend/Code/Generator component - zend-framework

I use Symfony's console component together with the Zend_CodeGenerator component. There was very few examples but I got it work -- http://framework.zend.com/manual/1.12/en/zend.codegenerator.html. Of course it didn't handle namespaces and use statements. Today I figured I try Zend/Code/Generator. I found an old post here http://mwop.net/blog/261-Code-Generation-with-ZendCodeGenerator.html.
So using Composer I installed the component and went about updated my code (see below). I'm having issues with doc blocks not being generated (see new version). There's no resulting errors indicating the parameters are wrong just no docblocks. I quickly reviewed the source code, api and even some the unit tests. So before I give up on this and stick with ZF1 CodeGenerator I wondered if there are any examples out there using docblocks.
Old Version
<?php
$class = new Zend_CodeGenerator_Php_Class();
$docblock = new Zend_CodeGenerator_Php_Docblock(array(
'shortDescription' => 'Index controller',
'tags' => array(
array(
'name' => 'category',
'description' => 'Controller',
),
array(
'name' => 'package',
'description' => 'Application\Controller',
),
),
));
$methods = array(
array(
'name' => 'indexAction',
'docblock' => new Zend_CodeGenerator_Php_Docblock(array(
'shortDescription' => 'Index action',
'tags' => array(
new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
'datatype' => 'void',
)),
),
)),
),
);
$class->setName("Admin_IndexController")
// Additional step needed to add namespace and use statement
->setExtendedClass('Action')
->setDocblock($docblock)
->setMethods($methods);
$file = new Zend_CodeGenerator_Php_File(array(
'classes' => array($class),
));
$code = $file->generate();
// Remove all that extra blank lines
$code = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $code);
if (file_put_contents('application/modules/admin/controllers/IndexController.php', $code)) {
$this->output->writeln(
'Controller generated ' . realpath('application/modules/admin/controllers/IndexController.php')
);
}
New Version
<?php
$classGenerator = new ClassGenerator();
$classGenerator->setName('Admin_IndexController')
->setExtendedClass('Action')
->setDocBlock(
new DocBlockGenerator(
'Index controller',
'',
array(
new Tag(
array(
'name' => 'category',
'description' => 'Controller'
)
),
new Tag(
array(
'name' => 'package',
'description' => 'Application\Controller'
)
)
)
)
)
->addMethods(
array(
new MethodGenerator(
'indexAction',
array(),
null,
null,
new DocBlockGenerator(
'Index action'
)
)
)
);
$fileGenerator = new FileGenerator();
$fileGenerator->setUse('Application\Controller\Action')
->setClass($classGenerator);
$code = $fileGenerator->generate();
// ...

Related

How can I save dynamically created Gravity Form fields into entries content?

The code below successfully creates a dynamic field and for a Gravity Form, and on submission the dynamic field data is included in the email content sent. However the dynamic field isn't saved in the "entries" data for that form submission, only the fields created manually using the plugin are. Anyone know how to include this data in the entries data saved?
add_filter('gform_pre_render_5', 'populate_wines');
add_filter('gform_pre_validation_5', 'populate_wines');
function populate_wines($form) {
// create dynamic select field
$props = array(
'id' => 51,
'type' => 'select',
'label' => 'Dynamic field label',
'choices' => array(
array(
'text' => '',
'value' => '',
),
array(
'text' => '1',
'value' => '1',
),
array(
'text' => '2',
'value' => '2',
),
array(
'text' => '3',
'value' => '3',
),
)
);
$new_field = GF_Fields::create( $props );
$form['fields'][] = $new_field;
return $form;
}
Edit: Dave's solution below works great for the above function (thanks!!), but adding to the function to include the looped wine list (code below), it doesn't work, any ideas?
function populate_wines($form) {
// options for select lists
$select_choices = array(
array(
'text' => '',
'value' => '',
),
array(
'text' => '1',
'value' => '1',
),
array(
'text' => '2',
'value' => '2',
),
array(
'text' => '3',
'value' => '3',
),
);
// loop through wine list
if( have_rows('wine_options') ):
$wine_count = 50;
while( have_rows('wine_options') ) : the_row();
$wine_ID = get_sub_field('wine_option');
$wine_name = get_the_title($wine_ID);
// create wine select field
$props = array(
'id' => $wine_count,
'type' => 'select',
'label' => $wine_name,
'choices' => $select_choices
);
$new_field = GF_Fields::create( $props );
$form['fields'][] = $new_field;
$wine_count++;
endwhile;
endif;
if ( GFForms::get_page() !== 'form_editor' ) {
return $form;
}
}
You'll need to call the same function on the gform_admin_pre_render filter. My recommendation would be to also add a check to exclude it from being output in the form editor but you may want that. If you don't, it'd look something like this:
if ( GFForms::get_page() !== 'form_editor' ) {
return $form;
}

Create custom module without bean to show up in menu

I am wondering how to create custom module in SugarCRM and show link into the menu bar. So my custom module will not be a Bean, but needs to be visible in menu bar.
My manifest.php file is as follows:
$manifest = array(
'acceptable_sugar_versions' => array(
"rege_matches" => array("5.1.*")
),
'acceptable_sugar_flavors' => array(
'CE'
),
'name' => 'CustomModule',
'version' => '1.0',
'description' => 'CustomModule for SugarCRM',
'author' => 'Community',
'published_date' => '2015/02/17',
'type' => 'module',
'icon' => 'icons/default/icon_CustomModule.gif',
'is_uninstallable' => 'true',
);
$installdefs = array(
'id'=> 'CustomModule',
'copy' => array (
array (
'from' => '<basepath>/package/CustomModule',
'to' => 'modules/CustomModule',
),
),
'language' => array(
array(
'language'=> 'en_us',
'from'=> '<basepath>/package/language/application/en_us.lang.php',
'to_module' => 'application',
'language' => 'en_us',
),
),
);
Thanks!

Exclude Category from Custom Post Type Loop / Archive

I've been reading through posts on how to do this but none seem to make sense or work. I have created a custom post type (research) in my functions.php file and a custom taxonomy (classifications). I also have a custom archive page (archive-research.php) for my post type.
I have a category (classification) called "oldresearch" that I would like to exclude from the custom archive template.
Below is my custom post type code. Could someone please help me and let me know where the exclusion code needs to go.
<?php
/* redirect users to front page after login */
function redirect_to_front_page() {
global $redirect_to;
if (!isset($_GET['redirect_to'])) {
$redirect_to = get_option('siteurl');
}
}
add_action('login_form', 'redirect_to_front_page');
if ( function_exists('register_sidebars') )
register_sidebars(3);
add_action('init', 'register_custom_menu');
function register_custom_menu() {
register_nav_menu('custom_menu', __('Custom Menu'));
}
/** Registering Custom Post Type: Research **/
// Register Taxonomy for Research
$labels = array(
'name' => 'Classifications',
'singular_name' => 'Classification',
'search_items' => 'Search Classifications',
'popular_items' => 'Popular Classifications',
'all_items' => 'All Classifications',
'parent_item' => 'Parent Classifications',
'edit_item' => 'Edit Classifications',
'update_item' => 'Update Classifications',
'add_new_item' => 'Add New Classification',
'new_item_name' => 'New Classifications',
'separate_items_with_commas' => 'Separate Classifications with commas',
'add_or_remove_items' => 'Add or remove Classifications',
'choose_from_most_used' => 'Choose from most used Classifications'
);
$args = array(
'label' => 'Classifications',
'labels' => $labels,
'public' => true,
'hierarchical' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'args' => array( 'orderby' => 'term_order' ),
'rewrite' => array( 'slug' => 'research/classifications', 'with_front' => false ),
'query_var' => true
);
register_taxonomy( 'Classifications', 'Research', $args );
// Register Classification Column
add_filter( 'manage_research_posts_columns', 'ilc_cpt_columns' );
add_action('manage_research_posts_custom_column', 'ilc_cpt_custom_column', 10, 2);
function ilc_cpt_columns($defaults) {
$defaults['Classifications'] = 'Classifications';
return $defaults;
}
function ilc_cpt_custom_column($column_name, $post_id) {
$taxonomy = $column_name;
$post_type = get_post_type($post_id);
$terms = get_the_terms($post_id, $taxonomy);
if ( !empty($terms) ) {
foreach ( $terms as $term )
$post_terms[] = "<a href='edit.php?post_type={$post_type}&{$taxonomy}={$term->slug}'> " . esc_html(sanitize_term_field('name', $term->name, $term->term_id, $taxonomy, 'edit')) . "</a>";
echo join( ', ', $post_terms );
}
else echo '<i>No terms.</i>';
}
// Register Custom Post Type
function research_post_type() {
$labels = array(
'name' => _x( 'Research', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Research', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Research', 'text_domain' ),
'parent_item_colon' => __( 'Parent Research', 'text_domain' ),
'all_items' => __( 'All Research', 'text_domain' ),
'view_item' => __( 'View Research', 'text_domain' ),
'add_new_item' => __( 'Add New Research', 'text_domain' ),
'add_new' => __( 'New Research', 'text_domain' ),
'edit_item' => __( 'Edit Research', 'text_domain' ),
'update_item' => __( 'Update Research', 'text_domain' ),
'search_items' => __( 'Search Research', 'text_domain' ),
'not_found' => __( 'No Research found', 'text_domain' ),
'not_found_in_trash' => __( 'No Research found in Trash', 'text_domain' ),
);
$rewrite = array(
'slug' => 'research',
'with_front' => true,
'rewrite_pages' => true,
'rewrite_feeds' => true,
);
$args = array(
'label' => __( 'research', 'text_domain' ),
'description' => __( 'Agri-Gro product research', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes', 'post-formats', ),
'taxonomies' => array( 'Classifications', 'post_tag' ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 20,
'menu_icon' => 'http://www.agrigro.com/news/wp-content/uploads/2013/01/Documents-icon.png',
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'page',
);
register_post_type( 'research', $args );
}
// Hook into the 'init' action
add_action( 'init', 'research_post_type', 0 );
?>
You can use the pre_get_posts filter in your functions.php in order to alter the query for a specific post_type. This is not the only way to achieve the task,but in my opinion it is quite easiest
UPDATE
You need to exclude a custom taxonomy term from your query so, we have to set a tax_query object, so:
Example:
add_action('pre_get_posts','custom_get_posts');
function custom_get_posts($query) {
// We are not displaying the posts on admin panel and this is the main query
if ( !is_admin() && $query->is_main_query() ) {
//Define the tax_query
$taxquery = array(
array(
'taxonomy' => 'Classifications', // also try lower case, remember a taxonomy name must be in lowercase
'field' => 'slug',
'terms' => array( 'oldresearch' ),
'operator' => 'NOT IN'
)
);
// We are displaying a custom posts type archive
if( $query->is_post_type_archive( 'research' ) ){
$query->set('tax_query', $taxquery );
}
}
}
Hope it helps! Let me know if you get stuck.

a link pass multiple parameter in zend framework

hi i am new in zend framework 2.2.0. i want to create the a link that go to delete page right now i am passing only id in the url so i want to pass another id in it.
Add to Trash
right now in this link message id is passing i also want to pass one more id named "did" in this link
Add to Trash
how can i get this ?
thanks in advance
You should use url view helper's third argument ($options) to pass your variables in the query string. Example:
$route = 'message';
$param = array('action' => 'delete');
$opts = array(
'query' => array(
'id' => $message->message_id,
'did' => $message->deliver_id
)
);
$this->url($route, $params, $opts);
You have to add "did" to your message route like this:
'router' => array(
'routes' => array(
'message' => array(
'type' => 'segment',
'options' => array(
'route' => '/:action/:id[/:did]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]+',
'id' => '[0-9]+',
'did' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Application\Controller\Index',
),
),
),
),
),
echo $this->url('message',array('action'=>'delete', 'id' => $message->message_id,'did'=>$message->deliver_id);
// output: /delete/1/2

Edit form in drupal module?

I have a problem making Drupal module .
I created a form for adding into database but i am having no luck with creating form to edit some record here is my problem.
The problem is when i load values into form load from database and change them and then click submit button form refresh before it submit new values. So it updates into database same thing as it was. Here is a code :
function edit_form($form, &$form_state) {
$query = db_select('activity', 'f')
->fields('f')
->condition('IDA', $_GET['edit']);
$thefile = $query->execute();
$title = "";
$desc = "";
$file = "";
$privacy = "";
while($record = $thefile->fetchAssoc())
{
$title = $record['title'];
$desc = $record['description'];ick submit button form refresh before it submit new values. So it updates into database same thing as it was. Here is a good :
function edit_form($form, &$form_state) {
$query = db_select('activity', 'f') ->fields('f') ->co
$file = $record['trainingresource'];
$privacy = $record['privacy'];
}
$form['activity'] = array(
'#type' => 'fieldset',
'#title' => t('Create a new activity'),
'#tree' => TRUE,
);
$form['activity']['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#description' => t('Please enter the title here.'),
'#value' => t($title),
);
$form['activity']['description'] = array(
'#type' => 'textarea',
'#title' => t('Enter Description'),
'#value' => t($desc),
'#description' => t('Please put description here.'),
);
/* $form['activity']['date'] = array(
'#type' => 'date',
'#title' => t('Enter activity date'),
'#description' => t('Please put activity date in here.'),
); */
$form['activity']['file'] = array(
'#type' => 'file',
'#title' => t('Submit activity file'),
'#value' => t($file),
'#description' => t('Please files in here.'),
);
$form['activity']['security'] = array(
'#type' => 'radios',
'#title' => t('Privacy'),
'#value' => t($privacy),
'#options' => array('True'=>t('True'),'False'=>t('False')),
);
// Description
$form['hidden'] = array('#type' => 'value', '#value' => 'is_it_here');
$form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
return $form;
}
And here is a submit form code:
function edit_form_submit($form, $form_state) {
$idt = $_GET['edit'];
$title = trim($form_state['values']['activity']['title']);
$desc = trim($form_state['values']['activity']['description']);
//$date = trim($form_state['values']['activity']['date']['year']."-".$form_state['values']['activity']['date']['month']."-".$form_state['values']['activity']['date']['day']);
$file = "file";
$privacy = trim($form_state['values']['activity']['security']['#value']);
$nid = db_update('activity') // Table name no longer needs {}
->fields(array(
'title' => $title,
'description' => $desc,
//'date' => $date,
'trainingresource' => $file,
'privacy' => $privacy,
))
->condition('IDA', $idt,'=')
->execute();
drupal_set_message($idt);
drupal_set_message("Added into database");
drupal_goto('activity', array('query'=>array(
'activ'=>$_GET['activ'],
)));
}
If someone have the same problem or know how to solve this problem , please help me .
Thanks in advance.
First of all i would like to point out your example code has been pasted wrongly. I see two declaration of same function edit_form.
Am assuming the first declaration was a wrong paste and continuing to answer this.
The major issue i have seeen in your form declaration is that you are using "#value" to store the a default value. Please use "#default_value".
If you use #value, it ignores the user submitted values.
Read more about use of #value.
Read more about use of #default_value
For example change,
$form['activity']['description'] = array(
'#type' => 'textarea',
'#title' => t('Enter Description'),
'#value' => t($desc),
'#description' => t('Please put description here.'),
);
to
$form['activity']['description'] = array(
'#type' => 'textarea',
'#title' => t('Enter Description'),
'#default_value' => t($desc),
'#description' => t('Please put description here.'),
);
Also i strongly recommend you to check this link which is a module that provides lots of examples to interact with Drupal.