Topic: [Class] CakeMODx : Some different MODx APIs....  (Read 12310 times)

Pages: [1] 2 3   Go Down

#1: 10-May-2009, 10:58 AM

Moderators

AHHP
Posts: 397

Salut capitan,

WWW
CakeMODx

Hello,
I've created some APIs for some routine actions while developing MODx Evo. These APIs are as methods of a class named CakeMODx(!)
This version is beta and in development so there is no WARRANTY for missing data.

These are methods which available to use:

Document management methods
  • newDocument: Creates a new MODx document.
  • updateDocument: Update/Edit a MODx document.
  • deleteDocument: Delete a MODx document with all details.
  • getDocField: Get fields from `site_content` table for one or more MODx document.
  • emptyTrash: Empty MODx recycle bin.
  • getChildrenCount: Get count of all sub-document for one or more parent documents.
  • getSiteDocumentCount: Get count of all documents of site.
  • getDocumentOutput: Print output of a document. New in 1.3!
  • updateCache: Renew the site cache.


Template Variable methods
  • setTV: Set a TV value for a document.
  • getTV: Get value of a TV.
  • unsetTV: Delete a TV value of one or more documents.
  • updateTV: Update/Edit a TV value.


Web user methods
  • newUser: Create new web user.
  • updateUserProfile: Update/Edit profile for one/more user(s).
  • unsetUser: Delete one/more user(s).
  • getUserField: Get data from one/more user(s).
  • getAssignedDocs: Get document which the user has been assigned to them in his/her webgroups.
  • getForbiddenDocs: Get document which the user has NOT been assigned to them in his/her webgroups.


Web/Doc group methods
  • newDocgroup: Create new Document Group.
  • renameDocgroup: Rename a Document Group.
  • unsetDocgroup: Delete a Document Group and ALL links to it's members.
  • joinDocgroup: Join document(s) to a Docgroup.
  • getDocgroupMembers: Get all members of a Docgroup.
  • newWebgroup: Create new Web Group.
  • renameWebgroup: Rename a Webument Group.
  • unsetWebgroup: Delete a Web Group and ALL links to it's members.
  • joinWebgroup: Join user(s) to a Webgroup.
  • getWebgroupMembers: Get all members of a Webgroup.
  • linkGroups: Assign a Webgroup to a Docgroup.
  • unlinkGroups: Unlink a Webgroup from a Docgroup.


Jot Comment methods
  • comment_byDoc: Get a comment by document container.
  • comment_byParent: Get a comment by parent of document containers.
  • comment_byWhere: Get a comment by SQL WHERE statement.
  • commentCount_byDoc: Get a comments count by document container.
  • commentCount_byParent: Get a comments count by parent of document containers.
  • commentCount_byWhere: Get a comments count by SQL WHERE statement.
  • comment_subscriptions_byDoc: Get a subscriptions by document container.
  • comment_subscriptions_byParent: Get a subscriptions by parent of document containers.
  • comment_subscriptions_byWhere: Get a subscriptions by SQL WHERE statement.
  • comment_new: Create a new comment. New in 1.3!
  • comment_move: Move comments to another document. New in 1.3!


Misc methods
  • doc_id2title: Get pagetitle by document ID.
  • TV_name2id: Get ID of TV by it's name.
  • user_id2fullname: Get fullname by User ID.
  • str_highlight: Highlight a string in text without corrupting HTML tags (by Aidan Lister).
  • escape: Escape strings to use in SQL commands (DBAPI::escape with magic_quotes checking).
  • arr_escape: Escape array values for SQL commands.
  • sendMail: Send email to some addresses.
  • PHPMailer: Get PHPMailer object.
  • stripModxTags: Strip MODx and/or HTML tags. New in 1.3!
  • getIP: Get IP. New in 1.3!


Usage Example
Code:
<?php

include "CakeModx.class.php";
$CakeModx = new CakeModx;

$fields = array(
"pagetitle" => "The Cake",
"content" => "Very good weather",
"published" => 1
);

$id $CakeModx->newDocument($fields);
if(
$id)
$CakeModx->updateCache();

?>

Note
This class is not related to MODx team and is a 3rd party.

Hint
In fact it is a complex of functions, it means you can cut methods you need and use as single functions after some tiny hacks. Many of methods are not depends on others.
for example you can use these two functions instead CakeMODx::comment_byDoc :
Code:
<?php
function comment_byDoc($doc$fields='content'$tagid='')
{
global $modx // ADDED
if(is_string($fields)) $fields = array($fields);
$fields "jot." join(', jot.' $fields);
$tagid_where = ($tagid == '') ? '' " AND jot.tagid='$tagid'";

/*
$sql = "
SELECT $fields , jfld.label AS fieldLabel, jfld.content AS fieldValue FROM " .$this->getFullTableName('jot_content'). " jot
LEFT JOIN " .$this->getFullTableName('jot_fields')." jfld ON jot.id=jfld.id WHERE jot.uparent=$doc $tagid_where
";
*/
$sql "
SELECT $fields , jfld.label AS fieldLabel, jfld.content AS fieldValue FROM " 
.$modx->getFullTableName('jot_content'). " jot
LEFT JOIN " 
.$modx->getFullTableName('jot_fields')." jfld ON jot.id=jfld.id WHERE jot.uparent=$doc $tagid_where
"
;

//return $this->comment_query($sql);
return comment_query($sql);
}

function 
comment_query($sql)
{
global $modx // ADDED
//$select = $modx->db->query($sql);
$select $modx->db->query($sql);

//$counts = $this->db->getRecordCount($select);
$counts $modx->db->getRecordCount($select);

//if($counts == 1) return $this->db->getRow($select);
if($counts == 1) return $modx->db->getRow($select);

if($counts == 0) return "0";

$comments = array();

//while($Row = $this->db->getRow($select)) $comments[] = $Row;
while($Row $modx->db->getRow($select)) $comments[] = $Row;

return $comments;
}


Good luck
AHHP

* CakeMODx_1.3beta.zip (33.54 KB - downloaded 154 times.)
« Last Edit: 31-Oct-2009, 04:25 PM by AHHP »

#2: 10-May-2009, 05:45 PM


Andrei
Posts: 325

"404 - Attachment Not Found"

#3: 10-May-2009, 06:54 PM

Foundation

OpenGeek
MODx Co-Founder
Posts: 6,965

damn accurate caricatures...

WWW
"404 - Attachment Not Found"
Sorry folks, thanks to the efforts of the "skiddies", we are going to have to disable all new attachments (and avatars) until further notice.  I'll provide more information in an announcement thread as soon as is prudent.
Jason Coward
MODx Co-Founder
xPDO Founder
CTO @ Collabpad
work productively.
work intelligently.
work together.
Light is just a vibration of a note too. Everything is. You've got to keep that in mind.
  Frank Zappa

#4: 10-May-2009, 11:48 PM

Moderators

AHHP
Posts: 397

Salut capitan,

WWW
"404 - Attachment Not Found"
Sorry, you would download it now.....

#5: 11-May-2009, 08:12 AM

Foundation

rthrash
Posts: 11,351

WWW
There might be some confusion on this with the CakePHP project. Any relation whatsoever?

Great set of APIs indeed though. Thank you!
MODx is a content managmeent framework that allows web professionals to turn over sites to end-users for daily maintenance without worrying. Please help us help you when asking for assistance and read the wiki. Searching the forums from the top level helps, too.
Ryan Thrash
MODx Co-Founder
Principal @ Collabpad
work productively.
work intelligently.
work together.

#6: 11-May-2009, 10:10 AM

Fresh79
Posts: 27

WWW
This looks like a great set of extenders/handles but i agree with Ryan, when i saw the post title i thought a PHPcake framework/terminal mashup with MODx ! How about a biscuit instead of a cake??

#7: 11-May-2009, 12:25 PM

Moderators

AHHP
Posts: 397

Salut capitan,

WWW
biscuit?! This is my Cake in MODx and i liike it  Wink

#8: 12-May-2009, 10:45 AM


mrhaw
Posts: 1,931

modx == freedom

WWW
Cool! Makin your own API!  Cool

/Help! When downloading I get a file without extension and I can't edit in any editor!? 
« Last Edit: 12-May-2009, 12:10 PM by mrhaw »
My playground: http://4up2date.info | Twitter: mrhaw
---> Check out: ReadSpeaker webReader Plugin | Support/Comments Thread

--=[ MR. HAW ]=--

#9: 12-May-2009, 12:26 PM

kongondo
Posts: 1,390

Works for me...a zip file it is.....


/Help! When downloading I get a file without extension and I can't edit in any editor!? 

Works for me....a zip file it is...

cheers/k

ps: Välkommen tillbaka mrhaw o grattis på  bröllopet!

#10: 12-May-2009, 12:30 PM

Moderators

AHHP
Posts: 397

Salut capitan,

WWW
/Help! When downloading I get a file without extension and I can't edit in any editor!? 

The Zip file contains:
CakeMODx dir
    CakeMODx.class.php
    errorlog.html
    documention.html
    cakemodx.jpg
    index.html

what file do you talking about?
« Last Edit: 12-May-2009, 12:44 PM by AHHP »

#11: 12-May-2009, 03:57 PM


mrhaw
Posts: 1,931

modx == freedom

WWW
@Kongondo: TACK!!  Smiley
@AHHP:  Ah! Yes I had to add .zip, to the file inside the first zip, then it worked - AWESOME DOCUMENTATION!!

It seems as this could enable add/remove friends function for webusers!!! //Nice tut -> http://blog.themeforest.net/screencasts/diving-into-php-day-15/
« Last Edit: 12-May-2009, 04:26 PM by mrhaw »
My playground: http://4up2date.info | Twitter: mrhaw
---> Check out: ReadSpeaker webReader Plugin | Support/Comments Thread

--=[ MR. HAW ]=--

#12: 12-May-2009, 09:07 PM

Testers
rfoster
Posts: 137

WWW
Thanks! This has potential...

#13: 13-May-2009, 07:40 AM


therebechips
Posts: 386

WWW
This is great  Smiley

Any chance we could get a few more user management related methods, eg:

* loginUser
* logoutUser
* autoLoginUser
* setUserAttribute (...using a webloginpe style custom user attributes table linked by a foreign key to the user id)
* getUserAttribute
* activateUser
* resetPassword
* emailPassword
* emailWebgroup
* emailUser
* pruneUsers
« Last Edit: 13-May-2009, 07:44 AM by therebechips »
Snippets: GoogleMap | FileDetails | Related Plugin: SSL

#14: 17-May-2009, 04:18 AM

Moderators

AHHP
Posts: 397

Salut capitan,

WWW
There was some bugs in escape methods. If you have used it, please downlaod new version.

Thanks

#15: 15-Jun-2009, 07:19 AM

namemy
Posts: 3

BUG FOUND in joinDocgroup method:
/**
     * Join document(s) to a Docgroup.
     *
     * @param string $groupname Name for Document Group.
     * @param integer _unlimited_ ID of documents to join as member.
     * @return boolean TRUE on success and FALSE on failure.
     */
function joinDocgroup($groupname)

After executing that method I've got TWO(instead of one) documents - first Right one
, Secont - Wrong one. Empty documents in Root
folder with no content and category (absolutely empty doc).

CakemMODx version: 1.1beta dated: 12.05.2009.
MODx version: 0.9.6.3

Example of bug in that snipnet:

function CreateNewArticle(&$fields){
include 'assets/libs/CakeModx/CakeModx.class.php';
$Article = new CakeModx;
$params = array(
"parent" => "26",
"template" => "6",
"hidemenu" => "1",
"searchable" => "0",
"createdon" => time(),
"pagetitle" => $fields['pagetitle'],
"content" => $fields['content'],
"published" => 1
);
$id = $Article->newDocument($params);
if($id)
       {
       $Article->joinDocgroup('DocsForRegistered',$id);
       $Article->updateCache();
       }
}

#16: 15-Jun-2009, 07:36 AM

namemy
Posts: 3

Addition BUG:
As I told early there where some bug, but as well I forgot to tell that there is one more bug; As you could see
in my preview reply I've got array of params:
$params = array(
"parent" => "26",
"template" => "6",
...
Template in number instead of word. If I type "template" => "mainTheme" - that parametr will no matter for newDocument method; And after execute that code ("template" => "mainTheme") - template of doc will be - "blank", on ther side "template" => "6" -  will place mainTheme template for new document.

#17: 15-Jun-2009, 07:49 AM

Coding Team

sottwell
Posts: 10,530

WWW
It's not unusual to require the ID for a document or template or other element, since the parser will use the ID. Most snippets such as Wayfinder and Ditto, for example, use the document ID. You can have duplicate element/resource names, as well as document titles. That would be a problem, so using the ID makes a lot of sense. Also, names can be changed, but the ID cannot.
sottwell.com has moved to a lovely Solaris 10 server!
Log in username guest, password guestuser.
Templates are now becoming available at http://sottwell.com/templates.html

#18: 15-Jun-2009, 08:10 AM

Moderators

AHHP
Posts: 397

Salut capitan,

WWW
Thank you for reporting,

There was a mistake in line 1435
Code:
if(! $this->db->insert(array("privateweb" => 1) , $sc, "id=$memId") )
Must be
Code:
if(! $this->db->update(array("privateweb" => 1) , $sc, "id=$memId") )

I updated the download link too.

----------------
About template problem, Susan is right, it is possible to have elements in same name in MODx, names can be changed in anytime  but ID is unique, unchangable (in backend) and safer.
but if your input is template name, you can easily get template ID by template name like below:
Code:
<?php
$params
['template'] = $modx->db->getVlaue($modx->db->select("id"$modx->getFullTableName("site_templates"), "templatename='$input_name'"));

Good luck
« Last Edit: 15-Jun-2009, 08:14 AM by AHHP »

#19: 8-Jul-2009, 04:47 PM

Moderators

AHHP
Posts: 397

Salut capitan,

WWW
hello,
I've upgraded the class with adding a few methods:
  • getDocumentOutput: Print output of a document.
  • comment_new: Create a new comment.
  • comment_move: Move comments to another document.
  • stripModxTags: Strip MODx and/or HTML tags.
  • getIP: Get IP.


Thanks
AHHP

#20: 9-Jul-2009, 12:18 AM

Coding Team

sottwell
Posts: 10,530

WWW
Quote
getDocumentOutput: Print output of a document.
I have often thought it was a pity that the document parser ends with an echo statement; it would be very useful sometimes if it just returned the documentOutput, and the index.php did the echo, so that one could use the parser to get the document's output for other purposes (besides in a plugin).
sottwell.com has moved to a lovely Solaris 10 server!
Log in username guest, password guestuser.
Templates are now becoming available at http://sottwell.com/templates.html
Pages: [1] 2 3   Go Up
0 Members and 1 Guest are viewing this topic.