      1: <?php
      2:
      3: // include the config file
      4: if(!isset($config)) {
      5:     require_once '../util/AlphaConfig.inc';
      6:     $config = AlphaConfig::getInstance();
      7:
      8:     require_once $config->get('app.root').'alpha/util/AlphaAutoLoader.inc';
      9: }
     10:
     11: /**
     12:  *
     13:  * Controller used to edit TagObjects related to the BO indicated in the supplied
     14:  * GET vars (bo and oid).
     15:  *
     16:  * @package alpha::controller
     17:  * @since 1.0
     18:  * @author John Collins <dev@alphaframework.org>
     19:  * @version $Id: EditTags.php 1666 2013-09-12 20:20:52Z alphadevx $
     20:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     21:  * @copyright Copyright (c) 2013, John Collins (founder of Alpha Framework).
     22:  * All rights reserved.
     23:  *
     24:  * <pre>
     25:  * Redistribution and use in source and binary forms, with or
     26:  * without modification, are permitted provided that the
     27:  * following conditions are met:
     28:  *
     29:  * * Redistributions of source code must retain the above
     30:  *   copyright notice, this list of conditions and the
     31:  *   following disclaimer.
     32:  * * Redistributions in binary form must reproduce the above
     33:  *   copyright notice, this list of conditions and the
     34:  *   following disclaimer in the documentation and/or other
     35:  *   materials provided with the distribution.
     36:  * * Neither the name of the Alpha Framework nor the names
     37:  *   of its contributors may be used to endorse or promote
     38:  *   products derived from this software without specific
     39:  *   prior written permission.
     40:  *
     41:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
     42:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
     43:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     44:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     45:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     46:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     47:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     48:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     49:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     50:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     51:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     52:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     53:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     54:  * </pre>
     55:  *
     56:  */
     57: class EditTags extends Edit implements AlphaControllerInterface {
     58:     /**
     59:      * Trace logger
     60:      *
     61:      * @var Logger
     62:      * @since 1.0
     63:      */
     64:     private static $logger = null;
     65:
     66:     /**
     67:      * constructor to set up the object
     68:      *
     69:      * @since 1.0
     70:      */
     71:     public function __construct() {
     72:         self::$logger = new Logger('EditTags');
     73:         self::$logger->debug('>>__construct()');
     74:
     75:         // ensure that the super class constructor is called, indicating the rights group
     76:         parent::__construct('Admin');
     77:
     78:         // set up the title and meta details
     79:         $this->setTitle('Editing Tags');
     80:         $this->setDescription('Page to edit tags.');
     81:         $this->setKeywords('edit,tags');
     82:
     83:         $this->BO = new TagObject();
     84:
     85:         self::$logger->debug('<<__construct');
     86:     }
     87:
     88:     /**
     89:      * Handle GET requests
     90:      *
     91:      * @param array $params
     92:      * @throws IllegalArguementException
     93:      * @since 1.0
     94:      * @throws FileNotFoundException
     95:      */
     96:     public function doGET($params) {
     97:         self::$logger->debug('>>doGET($params=['.var_export($params, true).'])');
     98:
     99:         global $config;
    100:
    101:         echo AlphaView::displayPageHead($this);
    102:
    103:         // ensure that a bo is provided
    104:         if (isset($params['bo']))
    105:             $BOName = $params['bo'];
    106:         else
    107:             throw new IllegalArguementException('Could not load the tag objects as a bo was not supplied!');
    108:
    109:         // ensure that a OID is provided
    110:         if (isset($params['oid']))
    111:             $BOoid = $params['oid'];
    112:         else
    113:             throw new IllegalArguementException('Could not load the tag objects as an oid was not supplied!');
    114:
    115:         try {
    116:             AlphaDAO::loadClassDef($BOName);
    117:             $this->BO = new $BOName;
    118:             $this->BO->load($BOoid);
    119:
    120:             $tags = $this->BO->getPropObject('tags')->getRelatedObjects();
    121:
    122:             AlphaDAO::disconnect();
    123:
    124:             echo '<table cols="3" class="edit_view">';
    125:             echo '<form action="'.$_SERVER['REQUEST_URI'].'" method="POST">';
    126:             echo '<tr><td colspan="3"><h3>The following tags were found:</h3></td></tr>';
    127:
    128:             foreach($tags as $tag) {
    129:                 echo '<tr><td>';
    130:                 $labels = $tag->getDataLabels();
    131:                 echo $labels['content'];
    132:                 echo '</td><td>';
    133:
    134:                 $fieldname = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(AlphaSecurityUtils::encrypt('content_'.$tag->getID())) : 'content_'.$tag->getID());
    135:                 $temp = new StringBox($tag->getPropObject('content'), $labels['content'], $fieldname, '');
    136:                 echo $temp->render(false);
    137:                 echo '</td><td>';
    138:
    139:                 $js = "$('#dialogDiv').text('Are you sure you wish to delete this tag?');
    140:                             $('#dialogDiv').dialog({
    141:                             buttons: {
    142:                                 'OK': function(event, ui) {
    143:                                     $('[id=\"".($config->get('security.encrypt.http.fieldnames') ? base64_encode(AlphaSecurityUtils::encrypt('deleteOID')) : 'deleteOID')."\"]').attr('value', '".$this->BO->getOID()."');
    144:                                     $('#deleteForm').submit();
    145:                                 },
    146:                                 'Cancel': function(event, ui) {
    147:                                     $(this).dialog('close');
    148:                                 }
    149:                             }
    150:                         })
    151:                         $('#dialogDiv').dialog('open');
    152:                         return false;";
    153:                 $button = new Button($js, "Delete", "delete".$tag->getID()."But");
    154:                 echo $button->render().'</td></tr>';
    155:             }
    156:
    157:             echo '<tr><td colspan="3"><h3>Add a new tag:</h3></td></tr>';
    158:             echo '<tr><td>';
    159:             echo 'New tag';
    160:             echo '</td><td>';
    161:             $fieldname = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(AlphaSecurityUtils::encrypt('new_value')) : 'new_value');
    162:             $temp = new StringBox(new String(), 'New tag', $fieldname, '');
    163:             echo $temp->render(false);
    164:             echo '</td><td></td></tr>';
    165:
    166:             echo '<tr><td colspan="3">';
    167:
    168:             $fieldname = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(AlphaSecurityUtils::encrypt('saveBut')) : 'saveBut');
    169:             $temp = new Button('submit', 'Save', $fieldname);
    170:             echo $temp->render();
    171:             echo '&nbsp;&nbsp;';
    172:             $temp = new Button("document.location = '".FrontController::generateSecureURL('act=Edit&bo='.$params['bo'].'&oid='.$params['oid'])."'", 'Back to Object', 'cancelBut');
    173:             echo $temp->render();
    174:             echo '</td></tr>';
    175:
    176:             echo AlphaView::renderSecurityFields();
    177:
    178:             echo '</form></table>';
    179:
    180:             echo AlphaView::renderDeleteForm();
    181:
    182:         }catch(BONotFoundException $e) {
    183:             $msg = 'Unable to load the BO of id ['.$params['oid'].'], error was ['.$e->getMessage().']';
    184:             self::$logger->error($msg);
    185:             throw new FileNotFoundException($msg);
    186:         }
    187:
    188:         echo AlphaView::displayPageFoot($this);
    189:
    190:         self::$logger->debug('<<doGET');
    191:     }
    192:
    193:     /**
    194:      * Handle POST requests
    195:      *
    196:      * @param array $params
    197:      * @throws SecurityException
    198:      * @throws IllegalArguementException
    199:      * @since 1.0
    200:      */
    201:     public function doPOST($params) {
    202:         self::$logger->debug('>>doPOST($params=['.var_export($params, true).'])');
    203:
    204:         try {
    205:             // check the hidden security fields before accepting the form POST data
    206:             if(!$this->checkSecurityFields())
    207:                 throw new SecurityException('This page cannot accept post data from remote servers!');
    208:
    209:             // ensure that a bo is provided
    210:             if (isset($params['bo']))
    211:                 $BOName = $params['bo'];
    212:             else
    213:                 throw new IllegalArguementException('Could not load the tag objects as a bo was not supplied!');
    214:
    215:             // ensure that a OID is provided
    216:             if (isset($params['oid']))
    217:                 $BOoid = $params['oid'];
    218:             else
    219:                 throw new IllegalArguementException('Could not load the tag objects as a bo was not supplied!');
    220:
    221:             if (isset($params['saveBut'])) {
    222:                 try {
    223:                     AlphaDAO::loadClassDef($BOName);
    224:                     $this->BO = new $BOName;
    225:                     $this->BO->load($BOoid);
    226:
    227:                     $tags = $this->BO->getPropObject('tags')->getRelatedObjects();
    228:
    229:                     AlphaDAO::begin();
    230:
    231:                     foreach ($tags as $tag) {
    232:                         $tag->set('content', TagObject::cleanTagContent($params['content_'.$tag->getID()]));
    233:                         $tag->save();
    234:                         self::$logger->action('Saved tag '.$tag->get('content').' on '.$BOName.' instance with OID '.$BOoid);
    235:                     }
    236:
    237:                     // handle new tag if posted
    238:                     if(isset($params['new_value']) && trim($params['new_value']) != '') {
    239:                         $newTag = new TagObject();
    240:                         $newTag->set('content', TagObject::cleanTagContent($params['new_value']));
    241:                         $newTag->set('taggedOID', $BOoid);
    242:                         $newTag->set('taggedClass', $BOName);
    243:                         $newTag->save();
    244:                         self::$logger->action('Created a new tag '.$newTag->get('content').' on '.$BOName.' instance with OID '.$BOoid);
    245:                     }
    246:
    247:                     AlphaDAO::commit();
    248:
    249:                     $this->setStatusMessage(AlphaView::displayUpdateMessage('Tags on '.get_class($this->BO).' '.$this->BO->getID().' saved successfully.'));
    250:
    251:                     $this->doGET($params);
    252:                 }catch (ValidationException $e) {
    253:                     /*
    254:                      * The unique key has most-likely been violated because this BO is already tagged with this
    255:                      * value.
    256:                      */
    257:                     AlphaDAO::rollback();
    258:
    259:                     $this->setStatusMessage(AlphaView::displayErrorMessage('Tags on '.get_class($this->BO).' '.$this->BO->getID().' not saved due to duplicate tag values, please try again.'));
    260:
    261:                     $this->doGET($params);
    262:                 }catch (FailedSaveException $e) {
    263:                     self::$logger->error('Unable to save the tags of id ['.$params['oid'].'], error was ['.$e->getMessage().']');
    264:                     AlphaDAO::rollback();
    265:
    266:                     $this->setStatusMessage(AlphaView::displayErrorMessage('Tags on '.get_class($this->BO).' '.$this->BO->getID().' not saved, please check the application logs.'));
    267:
    268:                     $this->doGET($params);
    269:                 }
    270:
    271:                 AlphaDAO::disconnect();
    272:             }
    273:
    274:             if (!empty($params['deleteOID'])) {
    275:                 try {
    276:                     AlphaDAO::loadClassDef($BOName);
    277:                     $this->BO = new $BOName;
    278:                     $this->BO->load($BOoid);
    279:
    280:                     $tag = new TagObject();
    281:                     $tag->load($params['deleteOID']);
    282:                     $content = $tag->get('content');
    283:
    284:                     AlphaDAO::begin();
    285:
    286:                     $tag->delete();
    287:
    288:                     self::$logger->action('Deleted tag '.$content.' on '.$BOName.' instance with OID '.$BOoid);
    289:
    290:                     AlphaDAO::commit();
    291:
    292:                     $this->setStatusMessage(AlphaView::displayUpdateMessage('Tag <em>'.$content.'</em> on '.get_class($this->BO).' '.$this->BO->getID().' deleted successfully.'));
    293:
    294:                     $this->doGET($params);
    295:                 }catch(AlphaException $e) {
    296:                     self::$logger->error('Unable to delete the tag of id ['.$params['deleteOID'].'], error was ['.$e->getMessage().']');
    297:                     AlphaDAO::rollback();
    298:
    299:                     $this->setStatusMessage(AlphaView::displayErrorMessage('Tag <em>'.$content.'</em> on '.get_class($this->BO).' '.$this->BO->getID().' not deleted, please check the application logs.'));
    300:
    301:                     $this->doGET($params);
    302:                 }
    303:
    304:                 AlphaDAO::disconnect();
    305:             }
    306:         }catch(SecurityException $e) {
    307:
    308:             $this->setStatusMessage(AlphaView::displayErrorMessage($e->getMessage()));
    309:
    310:             self::$logger->warn($e->getMessage());
    311:         }catch(IllegalArguementException $e) {
    312:             self::$logger->error($e->getMessage());
    313:         }catch(BONotFoundException $e) {
    314:             self::$logger->warn($e->getMessage());
    315:
    316:             $this->setStatusMessage(AlphaView::displayErrorMessage('Failed to load the requested item from the database!'));
    317:         }
    318:
    319:         self::$logger->debug('<<doPOST');
    320:     }
    321:
    322:     /**
    323:      * Using this callback to blank the new_value field when the page loads, regardless of anything being posted
    324:      *
    325:      * @return string
    326:      * @since 1.0
    327:      */
    328:     public function during_displayPageHead_callback() {
    329:         $html = '';
    330:         $html .= '<script language="javascript">';
    331:         $html .= 'function clearNewField() {';
    332:         $html .= '  document.getElementById("new_value").value = "";';
    333:         $html .= '}';
    334:         $html .= 'addOnloadEvent(clearNewField);';
    335:         $html .= '</script>';
    336:         return $html;
    337:     }
    338: }
    339:
    340: // now build the new controller if this file is called directly
    341: if ('EditTags.php' == basename($_SERVER['PHP_SELF'])) {
    342:     $controller = new EditTags();
    343:
    344:     if(!empty($_POST)) {
    345:         $controller->doPOST($_REQUEST);
    346:     }else{
    347:         $controller->doGET($_GET);
    348:     }
    349: }
    350:
    351: ?>