private function destroy_views($plugin, $name) { $plugin_app_path = $this->get_plugin_app_path($plugin); $name_tableized = MvcInflector::tableize($name); $public_directory = $plugin_app_path . 'views/' . $name_tableized; $admin_directory = $plugin_app_path . 'views/admin/' . $name_tableized; $directory = new MvcDirectory(); $directory->delete($public_directory); $directory->delete($admin_directory); }
public function filter_post_link($permalink, $post) { if (substr($post->post_type, 0, 4) == 'mvc_') { $model_name = substr($post->post_type, 4); $controller = MvcInflector::tableize($model_name); $model_name = MvcInflector::camelize($model_name); $model = MvcModelRegistry::get_model($model_name); $object = $model->find_one_by_post_id($post->ID); if ($object) { $url = MvcRouter::public_url(array('object' => $object)); if ($url) { return $url; } } } return $permalink; }
private function generate_views($plugin, $name) { $plugin_app_path = $this->get_plugin_app_path($plugin); $name_tableized = MvcInflector::tableize($name); $name_titleized = MvcInflector::titleize($name); $name_titleized_pluralized = MvcInflector::pluralize($name_titleized); $name_underscored = MvcInflector::underscore($name); $directory = new MvcDirectory(); $public_directory = $plugin_app_path . 'views/' . $name_tableized . '/'; $directory->create($public_directory); $admin_directory = $plugin_app_path . 'views/admin/' . $name_tableized . '/'; $directory->create($admin_directory); $vars = array('name_tableized' => $name_tableized, 'name_titleized' => $name_titleized, 'name_titleized_pluralized' => $name_titleized_pluralized, 'name_underscored' => $name_underscored); $this->templater->create('views/_item', $public_directory . '_item.php', $vars); $this->templater->create('views/index', $public_directory . 'index.php', $vars); $this->templater->create('views/show', $public_directory . 'show.php', $vars); $this->templater->create('views/admin/add', $admin_directory . '/add.php', $vars); $this->templater->create('views/admin/edit', $admin_directory . '/edit.php', $vars); }
private function set_meta() { $model = get_class($this); $model = preg_replace('/Controller$/', '', $model); $this->name = MvcInflector::underscore($model); $this->views_path = ''; if (preg_match('/^Admin[A-Z]/', $model)) { $this->views_path = 'admin/'; $model = preg_replace('/^Admin/', '', $model); } $model = MvcInflector::singularize($model); $this->views_path .= MvcInflector::tableize($model) . '/'; $this->model_name = $model; // To do: remove the necessity of this redundancy if (class_exists($model)) { $model_instance = new $model(); $this->model = $model_instance; $this->{$model} = $model_instance; } }
public function admin_page_param($options = array()) { if (is_string($options)) { $options = array('model' => $options); } if (!empty($options['model'])) { return 'mvc_' . MvcInflector::tableize($options['model']); } return false; }
public function admin_actions_cell($model, $object) { $links = array(); $object_name = empty($object->__name) ? 'Item #' . $object->__id : $object->__name; $encoded_object_name = $this->esc_attr($object_name); $controller = MvcInflector::tableize($model->name); $links[] = '<a href="' . MvcRouter::admin_url(array('controller' => $controller, 'action' => 'edit', 'object' => $object)) . '" title="Edit ' . $encoded_object_name . '">Edit</a>'; $links[] = '<a href="' . MvcRouter::public_url(array('controller' => $controller, 'action' => 'show', 'object' => $object)) . '" title="View ' . $encoded_object_name . '">View</a>'; $links[] = '<a href="' . MvcRouter::admin_url(array('controller' => $controller, 'action' => 'delete', 'object' => $object)) . '" title="Delete ' . $encoded_object_name . '" onclick="return confirm('Are you sure you want to delete ' . $encoded_object_name . '?');">Delete</a>'; $html = implode(' | ', $links); return '<td>' . $html . '</td>'; }
private function process_objects($objects, $options = array()) { if (!is_array($objects) && !is_object($objects)) { return null; } $single_object = false; if (is_object($objects)) { $objects = array($objects); $single_object = true; } $includes = array_key_exists('includes', $options) ? $options['includes'] : $this->includes; if (is_string($includes)) { $includes = array($includes); } if (!empty($includes)) { // Instantiate associated models, so that they don't need to be instantiated multiple times in the subsequent for loop $models = array(); foreach ($includes as $key => $include) { $model_name = is_string($include) ? $include : $key; $models[$model_name] = new $model_name(); } } $recursive = isset($options['recursive']) ? $options['recursive'] - 1 : 2; foreach ($objects as $key => $object) { if (!empty($this->primary_key)) { $object->__id = $object->{$this->primary_key}; } if (!empty($includes) && $recursive != 0) { foreach ($includes as $include_key => $include) { if (is_string($include)) { $model_name = $include; $association = $this->associations[$model_name]; } else { $model_name = $include_key; $association = $include; if (!empty($this->associations[$model_name])) { if (!empty($association['selects'])) { if (is_string($association['selects'])) { $association['selects'] = array($association['selects']); } $association['fields'] = $association['selects']; } $association = array_merge($this->associations[$model_name], $association); } } if (empty($association['fields'])) { $association['fields'] = array($association['name'] . '.*'); } $model = $models[$model_name]; switch ($association['type']) { case 'belongs_to': $associated_object = $model->find_by_id($object->{$association['foreign_key']}, array('recursive' => $recursive)); $object->{MvcInflector::underscore($model_name)} = $associated_object; break; case 'has_many': $associated_objects = $model->find(array('selects' => $association['fields'], 'conditions' => array($association['foreign_key'] => $object->__id), 'recursive' => $recursive)); $object->{MvcInflector::tableize($model_name)} = $associated_objects; break; case 'has_and_belongs_to_many': $join_alias = 'JoinTable'; $associated_objects = $model->find(array('selects' => $association['fields'], 'joins' => array('table' => $this->process_table_name($association['join_table']), 'on' => $join_alias . '.' . $association['association_foreign_key'] . ' = ' . $model_name . '.' . $model->primary_key, 'alias' => $join_alias), 'conditions' => array($join_alias . '.' . $association['foreign_key'] => $object->__id), 'recursive' => $recursive)); $object->{MvcInflector::tableize($model_name)} = $associated_objects; break; } } } if (method_exists($this, 'after_find')) { $this->after_find($object); } // Set this after after_find, in case after_find sets this field if (!empty($this->display_field)) { $object->__name = empty($object->{$this->display_field}) ? null : $object->{$this->display_field}; } $objects[$key] = $object; } if ($single_object) { return $objects[0]; } return $objects; }
public function has_many_dropdown($model_name, $select_options, $options = array()) { $defaults = array('select_id' => $this->model_name . '_' . $model_name . '_select', 'select_name' => $this->model_name . '_' . $model_name . '_select', 'list_id' => $this->model_name . '_' . $model_name . '_list', 'ids_input_name' => 'data[' . $this->model_name . '][' . $model_name . '][ids]', 'label' => MvcInflector::pluralize(MvcInflector::titleize($model_name)), 'options' => $select_options); $options = array_merge($defaults, $options); $select_options = $options; $select_options['id'] = $select_options['select_id']; $html = $this->before_input($options['select_name'], $select_options); $html .= $this->select_tag($options['select_name'], $select_options); $associated_objects = empty($this->object->{MvcInflector::tableize($model_name)}) ? array() : $this->object->{MvcInflector::tableize($model_name)}; // An empty value is necessary to ensure that data with name $options['ids_input_name'] is submitted; otherwise, // if no association objects were selected the save() method wouldn't know that this association data is being // updated and that it should, as a result, delete existing association data. $html .= '<input type="hidden" name="' . $options['ids_input_name'] . '[]" value="" />'; $html .= '<ul id="' . $options['list_id'] . '">'; foreach ($associated_objects as $associated_object) { $html .= ' <li> ' . $associated_object->__name . ' <a href="#" class="remove-item">Remove</a> <input type="hidden" name="' . $options['ids_input_name'] . '[]" value="' . $associated_object->__id . '" /> </li>'; } $html .= '</ul>'; $html .= ' <script type="text/javascript"> jQuery(document).ready(function(){ jQuery("#' . $options['select_id'] . '").change(function() { var option = jQuery(this).find("option:selected"); var id = option.attr("value"); if (id) { var name = option.text(); var list_item = \'<li><input type="hidden" name="' . $options['ids_input_name'] . '[]" value="\'+id+\'" />\'+name+\' <a href="#" class="remove-item">Remove</a></li>\'; jQuery("#' . $options['list_id'] . '").append(list_item); jQuery(this).val(\'\'); } return false; }); jQuery(".remove-item").live("click", function() { jQuery(this).parents("li:first").remove(); return false; }); }); </script> '; $html .= $this->after_input($options['select_name'], $select_options); return $html; }
protected function init_properties() { $this->properties = array(); foreach ($this->associations as $association_name => $association) { $property_name = null; if ($association['type'] == 'belongs_to') { $property_name = MvcInflector::underscore($association_name); } else { if (in_array($association['type'], array('has_many', 'has_and_belongs_to_many'))) { $property_name = MvcInflector::tableize($association_name); } } if ($property_name) { $this->properties[$property_name] = array('type' => 'association', 'association' => $association); } } }
public function add_menu_pages() { global $_registered_pages; $menu_position = 12; $menu_position = apply_filters('mvc_menu_position', $menu_position); foreach ($this->model_names as $model_name) { $model = MvcModelRegistry::get_model($model_name); if (!$model->hide_menu) { $tableized = MvcInflector::tableize($model_name); $pluralized = MvcInflector::pluralize($model_name); $titleized = MvcInflector::titleize($model_name); $pluralize_titleized = MvcInflector::pluralize_titleize($model_name); $controller_name = 'admin_' . $tableized; $top_level_handle = 'mvc_' . $tableized; $admin_pages = $model->admin_pages; $method = $controller_name . '_index'; $this->dispatcher->{$method} = create_function('', 'MvcDispatcher::dispatch(array("controller" => "' . $controller_name . '", "action" => "index"));'); add_menu_page($pluralize_titleized, $pluralize_titleized, 'administrator', $top_level_handle, array($this->dispatcher, $method), null, $menu_position); foreach ($admin_pages as $key => $admin_page) { $method = $controller_name . '_' . $admin_page['action']; if (!method_exists($this->dispatcher, $method)) { $this->dispatcher->{$method} = create_function('', 'MvcDispatcher::dispatch(array("controller" => "' . $controller_name . '", "action" => "' . $admin_page['action'] . '"));'); } $page_handle = $top_level_handle . '-' . $key; if ($admin_page['in_menu']) { add_submenu_page($top_level_handle, $admin_page['label'] . ' ‹ ' . $pluralize_titleized, $admin_page['label'], $admin_page['capability'], $page_handle, array($this->dispatcher, $method)); } else { // It looks like there isn't a more native way of creating an admin page without // having it show up in the menu, but if there is, it should be implemented here. // To do: set up capability handling and page title handling for these pages that aren't in the menu $hookname = get_plugin_page_hookname($page_handle, ''); if (!empty($hookname)) { add_action($hookname, array($this->dispatcher, $method)); } $_registered_pages[$hookname] = true; } } $menu_position++; } } }