Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Gajewski committed Sep 18, 2012
0 parents commit 24f4e85
Show file tree
Hide file tree
Showing 11 changed files with 429 additions and 0 deletions.
59 changes: 59 additions & 0 deletions Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace ContentEditable;

use ContentEditable\View\Helper\ContentEdit;

class Module
{
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}

public function getServiceConfig()
{
return array(
'factories' => array(
'ContentEditable' => 'ContentEditable\Service\Factory',
)
);
}

public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}

public function getViewHelperConfig()
{
return array(
'factories' => array(
'getContentEditableFile' => function ($sm) {
$locator = $sm->getServiceLocator();
$config = $locator->get('Configuration');
$params = $config['ContentEditable']['params'];

$viewHelper = new View\Helper\ContentEditable();
$viewHelper->setService($locator->get('ContentEditable'));
$viewHelper->setParams($params);

return $viewHelper;
},
),
);

}



}
75 changes: 75 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# ContentEditable module for Zend Framework 2

This module provides a way to make any HTML tag's inner content editable in TEXTAREA editor.


Requirements:

- PHP 5.3
- Zend Framework 2
- rwoverdijk/assetmanager @ [https://github.com/RWOverdijk/AssetManager](https://github.com/RWOverdijk/AssetManager)

See [https://github.com/artur-gajewski/ContentEditable](https://github.com/artur-gajewski/ContentEditable)

@Author: Artur Gajewski


## Installation with Composer

Go to your project directory and add the following line to "require" list in composer.json file:

```php
"artur-gajewski/content-editable": "dev-master"
```

Now run the Composer:

```php
php composer.phar install
```

Then add 'ContentEditable' into the Module array in APPLICATION_ROOT/config/application.config.php

```php
<?php
return array(
'modules' => array(
...
'ContentEditable',
...
),
);
```


## Adding JS and CSS files to your layout script

ContentEditable uses it's own Javascript and CSS files to generate dynamic edit capabilities within your view script.

In order to get ContentEditable working, you need to include these files where you include all your Javascript and CSS files.

```php
echo $this->getContentEditableFile('js');
echo $this->getContentEditableFile('css');
```


## Making your website editable

Now all you have to do, is to add a CSS style class to all tags you wish to become editable. You will also need to include a path in the data-url attribute
to where the content needs to be sent in order to save it in the database.

```php
<div class="editable" data-url="/article/update/1432">This is my content</div>
```

When you load the page and hover your mouse on top of the "This is my content" text, you will see the cursor changes to a pointer. Now if you click on this text, it will become a TEXTAREA with raw HTML code inside. Go ahead and edit the contents and then click somewhere outside the TEXTAREA. You will be requested to confirm data update.

Once you accept the modifications you have made, a request will be made to the given data-url with a parameter "editable_content", which ofcourse contains the contents of the edited data.


## Questions or comments?

Feel free to email me with any questions or comments about this module

[[email protected]](mailto:[email protected])
2 changes: 2 additions & 0 deletions autoload_classmap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
return array();
32 changes: 32 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "artur-gajewski/content-editable",
"description": "This module provides a way to make any HTML tag's inner content editable in TEXTAREA editor.",
"type": "module",
"keywords": [
"zf2",
"content",
"edit"
],
"homepage": "https://github.com/artur-gajewski/ContentEditable",
"authors": [
{
"name": "Artur Gajewski",
"email": "[email protected]",
"homepage": "https://github.com/artur-gajewski"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=5.3.3",
"zendframework/zendframework": "dev-master",
"rwoverdijk/assetmanager": "dev-master"
},
"autoload": {
"psr-0": {
"ContentEditable": "src/"
},
"classmap": [
"./Module.php"
]
}
}
19 changes: 19 additions & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace ContentEditable;

return array(
__NAMESPACE__ => array(
'params' => array(
'js_source_path' => '/js/ContentEditable.js',
'css_source_path' => '/css/ContentEditable.css'
),
),
'asset_manager' => array(
'resolver_configs' => array(
'paths' => array(
'ContentEdit' => __DIR__ . '/../public',
),
),
),
);
7 changes: 7 additions & 0 deletions public/css/ContentEditable.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
textarea.editabler {
width: 100%;
}

.editable {
cursor: pointer;
}
68 changes: 68 additions & 0 deletions public/js/ContentEditable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
$(document).ready(function() {

$(".editable").click(divClicked);

var attrs = [];
var attributes = [];

var attrsHtml = "";
var editableUrl = "";
var editableTag = "";
var editableOriginal = "";

function divClicked() {
var divHtml = $(this).html();
var height = $(this).height();

editableTag = this.nodeName.toLowerCase();
attributes = $(this).prop("attributes");

$.each(attributes, function() {
if (this.name == 'data-url') {
editableUrl = this.value;
}
attrs.push(this.name + '="' + this.value +'"');
});

if (!editableUrl) {
alert("Attribute data-url is missing!");
return;
}

attrsHtml = attrs.join(' ');

var editableText = $("<textarea class=\"editabler\" style=\"height: " + height + "px\"/>");
editableText.val(divHtml);
editableOriginal = divHtml;

$(this).replaceWith(editableText);
editableText.focus();
editableText.blur(editableTextBlurred);
}

function editableTextBlurred() {
var html = $(this).val();
var viewableText = $("<" + editableTag + " " + attrsHtml + ">");

var answer = confirm("Do you really want to update this content?")
if (answer){
viewableText.html(html);
$.ajax({
type: 'POST',
url: editableUrl,
data: "editable_content=" + html,
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Content has not been saved due to an error: " + errorThrown);
}
});
} else {
viewableText.html(editableOriginal);
}

$(this).replaceWith(viewableText);
viewableText.click(divClicked);
}

});


57 changes: 57 additions & 0 deletions src/ContentEditable/Manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace ContentEditable;

class Manager
{
/**
* @var Array
*/
protected $params;

/**
* @var array
*/
protected $cache;

/**
* Set the Module specific configuration parameters
*
* @param Array $params
*/
public function __construct($params) {
$this->params = $params;
}

public function get($type)
{
if ($type == 'css') {
return $this->getCss();
}
if ($type == 'js') {
return $this->getJavascript();
}
}

/**
* Generate JS inclusion HTML code
*/
public function getJavascript()
{
$link = '<script type="text/javascript" src="' . $this->params['js_source_path'] . '"></script>';
return $link;
}

/**
* Generate CSS inclusion HTML code
*
* @param string $media
* @return string
*/
public function getCss($media = 'screen')
{
$link = '<link href="' . $this->params['css_source_path'] . '" media="'. $media .'" rel="stylesheet" type="text/css" />';
return $link;
}

}
28 changes: 28 additions & 0 deletions src/ContentEditable/Service/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace ContentEditable\Service;

use Zend\ServiceManager\FactoryInterface,
Zend\ServiceManager\ServiceLocatorInterface,
ContentEditable\Manager;

/**
* ContentEdit service manager factory
*/
class Factory implements FactoryInterface
{
/**
* Factory method for ContentEditable Manager service
*
* @param ServiceLocatorInterface $serviceLocator
* @return ContentEditable\Manager
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$config = $serviceLocator->get('Configuration');
$params = $config['ContentEditable']['params'];

$manager = new Manager($params);
return $manager;
}
}
8 changes: 8 additions & 0 deletions src/ContentEditable/Version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace ContentEditable;

class Version
{
const VERSION = '0.0.1';
}
Loading

0 comments on commit 24f4e85

Please sign in to comment.