Skip to content

Instantly share code, notes, and snippets.

@wshawn
Last active August 29, 2015 14:10
Show Gist options
  • Save wshawn/41c38d0ba69b51289ea0 to your computer and use it in GitHub Desktop.
Save wshawn/41c38d0ba69b51289ea0 to your computer and use it in GitHub Desktop.
Sample class file for use with xPDO
<?php
/**
* @package myclass
*/
/**
* MyClass xPDO Version
*
* This is the main file to include in your scripts to use MyClass xPDO Version and dynamically interacts with all aspects of the MyClass.
*
* Initially, this package will function as ......
*
* @author W. Shawn Wilkerson <shawn@sanityllc.com>
* @copyright Copyright 2014 by W. Shawn Wilkerson
*
* @license None. For use as is. No warrentees to functionality, suitability, or safety offered.
*/
class myclass {
/**
* Whether or not the class is in debug mode.
*
* @var bool DEBUG
*/
const DEBUG = true;
/**
* A reference to the modX object.
*
* @var modX $modx
*/
public $modx = null;
/**
* A reference to the current user object.
*
* @var modUser $user
*/
public $user = null;
/**
* Main constructor for the MyClass Class
*
* @param modX $modx
* A reference to the current modx Object
* @param array $config
* configuration settings which can be used to over ride default settings - typically unnecessary
*/
function __construct(modX &$modx, array $config = array()) {
try {
/**
* The MODX object.
*/
$this->modx = &$modx;
/**
* The current MODX Revolution User
*/
$this->user = &$this->modx->user;
/**
* Establish system paths which will be used by this class.
*/
$corePath = $this->modx->getOption ( __CLASS__ . '.core_path', $config, $this->modx->getOption ( 'core_path' ) . 'components/' . __CLASS__ . '/' );
$assetsUrl = $this->modx->getOption ( __CLASS__ . '.assets_url', $config, $this->modx->getOption ( 'assets_url' ) . 'components/' . __CLASS__ . '/' );
$this->config = array_merge ( array (
'assetsUrl' => $assetsUrl,
'cssUrl' => $assetsUrl . 'css/',
'jsUrl' => $assetsUrl . 'js/',
'imagesUrl' => $assetsUrl . 'images/',
'connectorUrl' => $assetsUrl . 'connector.php',
'corePath' => $corePath,
'modelPath' => $corePath . 'model/',
'chunksPath' => $corePath . 'elements/chunks/',
'controllersPath' => $corePath . 'controllers/',
'processorsPath' => $corePath . 'processors/',
'snippetsPath' => $corePath . 'elements/snippets/',
'package' => __CLASS__,
'prefix' => 'prfx_'
), $config );
$this->modx->addPackage ( $this->config ['package'], $this->config ['modelPath'], $this->config ['prefix'] );
} catch ( xPDOException $xe ) {
$this->modx->sendError ( 'unavailable', array (
'error_message' => $xe->getMessage ()
) );
} catch ( Exception $e ) {
$this->modx->sendError ( 'unavailable', array (
'error_message' => $e->getMessage ()
) );
}
}
/**
* Final destructor for MyClass.
*/
function __destruct() {
// TODO - Insert your code here
}
/**
* Retrieves an array of the object names defined in the schema.
* Note: this list must be manually updated and match schema definitions for
* all subsequent usage to function properly.
*
* @return array An Array of object names established in the schema file.
*/
private function _getSchemaOjects() {
return array (
'prfxObject1',
'prfxObject2',
'prfxObject3',
'prfxObject4',
'prfxLog'
);
}
/**
* Retrieves the current user's IP Address in IPv4 or IPv6
*
* @return string An IP Address or an empty string
*/
public function getClientIpAddress() {
$serverVariables = array (
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED',
'HTTP_X_CLUSTER_CLIENT_IP',
'HTTP_X_COMING_FROM',
'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED',
'HTTP_COMING_FROM',
'HTTP_CLIENT_IP',
'HTTP_FROM',
'HTTP_VIA',
'REMOTE_ADDR'
);
$out = '';
foreach ( $serverVariables as $serverVariable ) {
$value = '';
if (isset ( $_SERVER [$serverVariable] )) {
$value = $_SERVER [$serverVariable];
} elseif (getenv ( $serverVariable )) {
$value = getenv ( $serverVariable );
}
if (! empty ( $value )) {
if (filter_var ( trim ( $value ), FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE )) {
$out = $value;
break;
}
}
}
return $out;
}
/**
* Parses the schema and generates ORM files and the database structure, but will not overwrite preexisting files such as IonCube Encoded Versions or those previously generated.
*
* Schema file must be stored at /core/components/[package_name]/model/schema/[package_name].mysql.schema.xml.
* Schema file must be named [package_name].mysql.schema.xml.
*
* @return string
*/
public function initializeSchema() {
$out = 'failed';
$xpdoManager = $this->modx->getManager ();
if ($xpdoManager) {
$xpdoGenerator = $xpdoManager->getGenerator ();
if ($xpdoGenerator) {
$success = $xpdoGenerator->parseSchema ( $this->config ['modelPath'] . 'schema/' . $this->config ['package'] . '.mysql.schema.xml', $this->config ['modelPath'] );
if ($success) {
$schemaObjects = $this->_getSchemaOjects ();
foreach ( $schemaObjects as $t ) {
$success += $xpdoManager->createObjectContainer ( trim ( $t ) );
}
$out = ($success > count ( $schemaObjects )) ? 'Model Created at ' . $this->config ['modelPath'] . $this->config ['package'] . ' and all tables successfully created.</p>' : 'Operation Failed';
}
}
}
$this->logevent ( ($success > count ( $schemaObjects )) ? true : false, $out );
return $out;
}
/**
* Creates an Event log of myClass Activity.
*
* Additional information to placed in the log entry.
* Whether or not execution succeeded
*
* @param bool $status
* true on success || false on failure
* @param string $comment
* Text related to the action
*
* @return boolean true on success || false on failure
*/
public function logevent($status = '', $comment = '') {
$trace = $this->modx->getDebugBacktrace ();
$ipaddress = $this->getClientIpAddress ();
$event = $this->modx->newObject ( 'prfxLog', array (
'class' => __CLASS__,
'action' => $trace [2] ['function'],
'status' => $status,
'comment' => $comment,
'clientid' => $this->user->getPrimaryKey (),
'timestamp' => time (),
'ipaddress' => ($ipaddress) ? inet_pton ( $ipaddress ) : ''
) );
return $event->save ();
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment