Jump to content

AdminCustomTable - My first module


blad
 Share

Recommended Posts

The concept

A custom table module process (requires Jquery Datatables module by @soma) with configuration. 

Features: 

- Custom selector to find the pages.

- Custom fields in columns.

- Custom fields search.

- Set the number of columns. 

- Create, View, edit, publish/unpublish pages.

I´m working on it. Please correct my mistakes is my first processwire´s module have mercy  :undecided:

Screenshots: 

post-2447-0-18432300-1408347869_thumb.pn

post-2447-0-47515200-1408347872_thumb.pn

post-2447-0-44289500-1408347870_thumb.pn

The code:

<?php

/**
 * Administrador de Noticias
 *
 * Luis Santiago (blad) 2014
 * ProcessWire 2.x 
 * Copyright (C) 2012 by Ryan Cramer 
 * Licensed under GNU/GPL v2, see LICENSE.TXT
 * 
 * http://processwire.com
 */
	class AdminCustomTable extends Process implements Module, ConfigurableModule {



	const adminPageName = 'AdminTable';

	public static function getModuleInfo() {
		return array(
			'title' => 'AdminCustomTable', 
			'summary' => 'Fully customizable administration table', 
			'version' => 110, 
			'permanent' => false, 
			'permission' => 'AdministrationTable',
			'requires' => array("JqueryDataTables")
			);
	}

	public function __construct() {

        $this->idparentnews = 'parent=1, include=all';
        $this->campo1 = 'title';
        $this->campo2 = 'body';

    }

 public static function getModuleConfigInputfields(array $data) {
        $inputfields = new InputfieldWrapper(); 

        $field = wire('modules')->get('InputfieldText');
        $field->name = 'idparentnews';
        $field->label = "Custom selector to find pages";
        //$field->label = "Selector para encontrar las noticias";
        //$field->notes = __('Selector para conseguir las noticias ejemplo: parent=1018, include=all .Se recomienda usar include=all para ver todas las páginas'); 
		$field->notes = __('This selector will be passed to a $pages->find("your selector") Example: parent=1018, template=product, sort=name, include=all  .Use include=all for publish/unpublish these pages.'); 

        if(isset($data['idparentnews'])) $field->value = $data['idparentnews'];
        $inputfields->add($field); 

        $field = wire('modules')->get('InputfieldText');
        $field->name = 'campo1';
        $field->label = "Custom field for first column";
        //$field->label = "Selector para encontrar las noticias";
        //$field->notes = __('Selector para conseguir las noticias ejemplo: parent=1018, include=all .Se recomienda usar include=all para ver todas las páginas'); 
		$field->notes = __('Example: title'); 

        if(isset($data['campo1'])) $field->value = $data['campo1'];
        $inputfields->add($field); 

        $field = wire('modules')->get('InputfieldText');
        $field->name = 'campo2';
        $field->label = "Custom field for second column";
        //$field->label = "Selector para encontrar las noticias";
        //$field->notes = __('Selector para conseguir las noticias ejemplo: parent=1018, include=all .Se recomienda usar include=all para ver todas las páginas'); 
		$field->notes = __('Example: body'); 
        if(isset($data['campo2'])) $field->value = $data['campo2'];
        $inputfields->add($field); 

        return $inputfields; 
    }

	/**
	 * Inicio del módulo
	 *
	 */
	public function init() {
	$this->config->scripts->add($this->config->urls->AdminCustomTable . 'prog.js'); 
	// $this->config->styles->add($this->config->urls->AdminCustomTable . 'style.css'); 
	$this->modules->JqueryDataTables;


			}
	
	/**
	 * Ejecución del módulo
	 *
	 */
	
	public function ___execute() {

		$this->modules->get("JqueryFancybox");
		$selector = $this->idparentnews;
		$campo1 = $this->campo1;
		$campo2 = $this->campo2;
		$noticias = wire('pages')->find("{$selector}");
		$out =	"<table id='mitabla' style='margin-top:20px; margin-bottom:20px;'>
    	<thead>
        <tr>
            <th style='width:40%'>{$noticias->fields->$campo1->label}</th>
            <th style='width:50%'>{$noticias->fields->$campo2->label}</th>
			<th class='sin-orden' style='width:10%'>Actions</th>
        </tr>
    	</thead>
   		 <tbody>
        ";

$adminUrl = $this->config->urls->admin; 

$noticias->fields->title->label;

	/**
	 * Nos deja buscar el id de la página
	 *
	*/

		//	if( $this->input->get->sSearch ) {
		//	  $q = $this->sanitizer->text($this->input->get->sSearch);
		//	  if (is_numeric($q)) {
		//	    $selector .= "id=$q,";
		//	  } else {
		//	    $selector .= "title|body%=$q,";
		//	  }
		//	}

	/**
	 * buscamos el selector
	 *
	*/



	foreach($noticias as $noticia) {

			$out .= "   <tr>
						<td>{$noticia->{$campo1}}</td>
						<td>{$noticia->{$campo2}}</td>
						<td>
								<a class='fancybox-iframe' href='{$adminUrl}page/edit/?id={$noticia->id}&modal=1'><i  class='fa fa-edit'></i></a></br>
								<a href='{$adminUrl}page/delete/?id={$noticia->id}'><i  class='fa fa-times'></i></a></br>" ;

	
	/**
	 * botón de publicar-despublicar
	 *
	 */

		if ($noticia->is(Page::statusUnpublished)) {
					$out .= "<i  style='color:red' class='fa fa-square-o'></i>";

			} else {
					$out .= "<i  style='color:green' class='fa fa-check-square-o'></i>";

			}

		$out .= "</td></tr>" ;
		       
			}

		$out .= "</tbody></table>" ;

	
			
		return $out; 
	}
	
	
	/**
	 * Instalación
	 *
 	*/

	public function ___install() {
		
			$admin = $this->pages->get($this->config->adminRootPageID); 
			$parent = $admin; 
			$page = new Page();
			$page->parent = $parent; 
			$page->template = $this->templates->get('admin');
			$page->name = "AdminCustomTable"; 
			$page->title = "AdminCustomTable";
			$page->process = $this; 
			$page->save();

		$this->message("You have installed AdminCustomTable");
		
		if (!$this->permissions->get('AdministrationTable')->id) {
			$permission = $this->permissions->add('AdministrationTable');
			$permission->title = 'AdminCustomTable permission.';
			$permission->save();
		}
		
		
		
	}
		
	/**
	 * Desinstalación
	 *
	*/
	 
	public function ___uninstall() {
		$page = $this->pages->get('template=admin, name=AdminCustomTable');
		$page->delete();
		$permission = $this->permissions->get('AdministrationTable');
		$permission->delete();

	}
	
	

}

  • Like 3
Link to comment
Share on other sites

hi blad,

thanks for sharing and congratulations on your first module! i hope i can also share my first module soon, but first i need a good use case / idea for it :)

i think it would be great to stick to english in your comments - at least for me reading spanish is a real problem  :undecided: but anyway sticking to english is a good practise i think!

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...