1: <?php
2:
3:
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: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55:
56: class TagManager extends AlphaController implements AlphaControllerInterface {
57: 58: 59: 60: 61: 62:
63: private static $logger = null;
64:
65: 66: 67: 68: 69:
70: public function __construct() {
71: self::$logger = new Logger('TagManager');
72: self::$logger->debug('>>__construct()');
73:
74: global $config;
75:
76:
77: parent::__construct('Admin');
78:
79: $this->setTitle('Tag Manager');
80:
81: self::$logger->debug('<<__construct');
82: }
83:
84: 85: 86: 87: 88: 89:
90: public function doGET($params) {
91: self::$logger->debug('>>doGET($params=['.var_export($params, true).'])');
92:
93: global $config;
94:
95: echo AlphaView::displayPageHead($this);
96:
97: $message = $this->getStatusMessage();
98: if(!empty($message))
99: echo $message;
100:
101: echo '<h3>Listing business objects which are tagged</h3>';
102:
103: $BOs = AlphaDAO::getBOClassNames();
104:
105: foreach ($BOs as $BO) {
106: AlphaDAO::loadClassDef($BO);
107: $temp = new $BO;
108: if($temp->isTagged()) {
109: $tag = new TagObject();
110: $count = count($tag->loadAllByAttribute('taggedClass', $BO));
111: echo '<h4>'.$temp->getFriendlyClassName().' object is tagged ('.$count.' tags found)</h4>';
112:
113: $fieldname = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(AlphaSecurityUtils::encrypt('clearTaggedClass')) : 'clearTaggedClass');
114:
115: $js = "if(window.jQuery) {
116: BootstrapDialog.show({
117: title: 'Confirmation',
118: message: 'Are you sure you want to delete all tags attached to the ".$temp->getFriendlyClassName()." class, and have them re-created?',
119: buttons: [
120: {
121: icon: 'glyphicon glyphicon-remove',
122: label: 'Cancel',
123: cssClass: 'btn btn-default btn-xs',
124: action: function(dialogItself){
125: dialogItself.close();
126: }
127: },
128: {
129: icon: 'glyphicon glyphicon-ok',
130: label: 'Okay',
131: cssClass: 'btn btn-default btn-xs',
132: action: function(dialogItself) {
133: $('[id=\"".$fieldname."\"]').attr('value', '".$BO."');
134: $('#clearForm').submit();
135: dialogItself.close();
136: }
137: }
138: ]
139: });
140: }";
141: $button = new Button($js, "Re-create tags", "clearBut".$BO);
142:
143: echo $button->render();
144: }
145: }
146:
147: AlphaDAO::disconnect();
148:
149: echo '<form action="'.$_SERVER['REQUEST_URI'].'" method="POST" id="clearForm">';
150: echo '<input type="hidden" name="'.$fieldname.'" id="'.$fieldname.'"/>';
151: echo AlphaView::renderSecurityFields();
152: echo '</form>';
153:
154: echo AlphaView::displayPageFoot($this);
155:
156: self::$logger->debug('<<doGET');
157: }
158:
159: 160: 161: 162: 163: 164:
165: public function doPOST($params) {
166: self::$logger->debug('>>doPOST($params=['.var_export($params, true).'])');
167:
168: try {
169:
170: if(!$this->checkSecurityFields())
171: throw new SecurityException('This page cannot accept post data from remote servers!');
172:
173: if (isset($params['clearTaggedClass']) && $params['clearTaggedClass'] != '') {
174: try {
175: self::$logger->info('About to start rebuilding the tags for the class ['.$params['clearTaggedClass'].']');
176: $startTime = microtime(true);
177:
178: AlphaDAO::loadClassDef($params['clearTaggedClass']);
179: $temp = new $params['clearTaggedClass'];
180: $BOs = $temp->loadAll();
181:
182: self::$logger->info('Loaded all of the BOs (elapsed time ['.round(microtime(true)-$startTime, 5).'] seconds)');
183:
184: AlphaDAO::begin();
185:
186: $tag = new TagObject();
187: $tag->deleteAllByAttribute('taggedClass', $params['clearTaggedClass']);
188:
189: self::$logger->info('Deleted all of the old tags (elapsed time ['.round(microtime(true)-$startTime, 5).'] seconds)');
190:
191: $this->regenerateTagsOnBOs($BOs);
192:
193: self::$logger->info('Saved all of the new tags (elapsed time ['.round(microtime(true)-$startTime, 5).'] seconds)');
194:
195: self::$logger->action('Tags recreated on the ['.$params['clearTaggedClass'].'] class');
196:
197: AlphaDAO::commit();
198: $this->setStatusMessage(AlphaView::displayUpdateMessage('Tags recreated on the '.$temp->getFriendlyClassName().' class.'));
199:
200: self::$logger->info('Tags recreated on the ['.$params['clearTaggedClass'].'] class (time taken ['.round(microtime(true)-$startTime, 5).'] seconds).');
201: }catch (AlphaException $e) {
202: self::$logger->error($e->getMessage());
203: AlphaDAO::rollback();
204: }
205:
206: AlphaDAO::disconnect();
207: }
208:
209: return $this->doGET($params);
210: }catch(SecurityException $e) {
211: $this->setStatusMessage(AlphaView::displayErrorMessage($e->getMessage()));
212:
213: self::$logger->warn($e->getMessage());
214: }catch(IllegalArguementException $e) {
215: self::$logger->error($e->getMessage());
216: $this->setStatusMessage(AlphaView::displayErrorMessage($e->getMessage()));
217: }
218:
219: echo AlphaView::displayPageHead($this);
220:
221: $message = $this->getStatusMessage();
222: if(!empty($message))
223: echo $message;
224:
225: echo AlphaView::displayPageFoot($this);
226: self::$logger->debug('<<doPOST');
227: }
228:
229: 230: 231: 232: 233: 234:
235: private function regenerateTagsOnBOs($BOs) {
236: foreach ($BOs as $BO) {
237: foreach($BO->get('taggedAttributes') as $tagged) {
238:
239: $tags = TagObject::tokenize($BO->get($tagged), get_class($BO), $BO->getOID());
240:
241: foreach($tags as $tag) {
242: try {
243: $tag->save();
244: }catch(ValidationException $e){
245: 246: 247: 248:
249: }
250: }
251: }
252: }
253: }
254:
255: 256: 257: 258: 259:
260: public function after_displayPageHead_callback() {
261: $menu = AlphaView::loadTemplateFragment('html', 'adminmenu.phtml', array());
262:
263: return $menu;
264: }
265: }
266:
267:
268: if ('TagManager.php' == basename($_SERVER['PHP_SELF'])) {
269: $controller = new TagManager();
270:
271: if(!empty($_POST)) {
272: $controller->doPOST($_REQUEST);
273: }else{
274: $controller->doGET($_GET);
275: }
276: }
277:
278: ?>